Skip to content

[DE-748] wildcard analyzer #546

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,6 @@ public enum AnalyzerType {
collation,
classification,
nearest_neighbors,
minhash
minhash,
wildcard
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@
@JsonSubTypes.Type(name = "collation", value = CollationAnalyzer.class),
@JsonSubTypes.Type(name = "classification", value = ClassificationAnalyzer.class),
@JsonSubTypes.Type(name = "nearest_neighbors", value = NearestNeighborsAnalyzer.class),
@JsonSubTypes.Type(name = "minhash", value = MinHashAnalyzer.class)
@JsonSubTypes.Type(name = "minhash", value = MinHashAnalyzer.class),
@JsonSubTypes.Type(name = "wildcard", value = WildcardAnalyzer.class)
})
public abstract class SearchAnalyzer {
private String name;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* DISCLAIMER
*
* Copyright 2016 ArangoDB GmbH, Cologne, Germany
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Copyright holder is ArangoDB GmbH, Cologne, Germany
*/

package com.arangodb.entity.arangosearch.analyzer;


import com.arangodb.entity.arangosearch.AnalyzerType;

import java.util.Objects;

/**
* An Analyzer that creates n-grams to enable fast partial matching for wildcard queries if you have large string
* values, especially if you want to search for suffixes or substrings in the middle of strings (infixes) as opposed to
* prefixes.
* It can apply an Analyzer of your choice before creating the n-grams, for example, to normalize text for
* case-insensitive and accent-insensitive search.
*
* @author Michele Rastelli
* @see <a href= "https://docs.arangodb.com/3.12/index-and-search/analyzers/#wildcard">API Documentation</a>
*/
public final class WildcardAnalyzer extends SearchAnalyzer {
private WildcardAnalyzerProperties properties;

public WildcardAnalyzer() {
setType(AnalyzerType.wildcard);
}

public WildcardAnalyzerProperties getProperties() {
return properties;
}

public void setProperties(WildcardAnalyzerProperties properties) {
this.properties = properties;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
if (!super.equals(o)) return false;
WildcardAnalyzer that = (WildcardAnalyzer) o;
return Objects.equals(properties, that.properties);
}

@Override
public int hashCode() {
return Objects.hash(super.hashCode(), properties);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* DISCLAIMER
*
* Copyright 2016 ArangoDB GmbH, Cologne, Germany
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Copyright holder is ArangoDB GmbH, Cologne, Germany
*/

package com.arangodb.entity.arangosearch.analyzer;


import java.util.Objects;

/**
* @author Michele Rastelli
*/
public final class WildcardAnalyzerProperties {

private Integer ngramSize;
private SearchAnalyzer analyzer;

/**
* @return unsigned integer for the n-gram length, needs to be at least 2
*/
public Integer getNgramSize() {
return ngramSize;
}

/**
* @param ngramSize unsigned integer for the n-gram length, needs to be at least 2
*/
public void setNgramSize(Integer ngramSize) {
this.ngramSize = ngramSize;
}

public SearchAnalyzer getAnalyzer() {
return analyzer;
}

public void setAnalyzer(SearchAnalyzer analyzer) {
this.analyzer = analyzer;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
WildcardAnalyzerProperties that = (WildcardAnalyzerProperties) o;
return Objects.equals(ngramSize, that.ngramSize) && Objects.equals(analyzer, that.analyzer);
}

@Override
public int hashCode() {
return Objects.hash(ngramSize, analyzer);
}
}
29 changes: 29 additions & 0 deletions driver/src/test/java/com/arangodb/ArangoSearchAsyncTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -1034,6 +1034,35 @@ void MinHashAnalyzer(ArangoDatabaseAsync db) throws ExecutionException, Interrup
createGetAndDeleteTypedAnalyzer(db, analyzer);
}

@ParameterizedTest
@MethodSource("asyncDbs")
void WildcardAnalyzer(ArangoDatabaseAsync db) throws ExecutionException, InterruptedException {
assumeTrue(isAtLeastVersion(3, 12));

NormAnalyzerProperties properties = new NormAnalyzerProperties();
properties.setLocale("ru");
properties.setAnalyzerCase(SearchAnalyzerCase.lower);
properties.setAccent(true);

NormAnalyzer normAnalyzer = new NormAnalyzer();
normAnalyzer.setProperties(properties);

WildcardAnalyzerProperties wildcardProperties = new WildcardAnalyzerProperties();
wildcardProperties.setNgramSize(3);
wildcardProperties.setAnalyzer(normAnalyzer);

Set<AnalyzerFeature> features = new HashSet<>();
features.add(AnalyzerFeature.frequency);
features.add(AnalyzerFeature.position);

WildcardAnalyzer wildcardAnalyzer = new WildcardAnalyzer();
wildcardAnalyzer.setName("test-" + UUID.randomUUID());
wildcardAnalyzer.setProperties(wildcardProperties);
wildcardAnalyzer.setFeatures(features);

createGetAndDeleteTypedAnalyzer(db, wildcardAnalyzer);
}

@ParameterizedTest
@MethodSource("asyncDbs")
void offsetFeature(ArangoDatabaseAsync db) throws ExecutionException, InterruptedException {
Expand Down
29 changes: 29 additions & 0 deletions driver/src/test/java/com/arangodb/ArangoSearchTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -1033,6 +1033,35 @@ void MinHashAnalyzer(ArangoDatabase db) {
createGetAndDeleteTypedAnalyzer(db, analyzer);
}

@ParameterizedTest
@MethodSource("dbs")
void WildcardAnalyzer(ArangoDatabase db) {
assumeTrue(isAtLeastVersion(3, 12));

NormAnalyzerProperties properties = new NormAnalyzerProperties();
properties.setLocale("ru");
properties.setAnalyzerCase(SearchAnalyzerCase.lower);
properties.setAccent(true);

NormAnalyzer normAnalyzer = new NormAnalyzer();
normAnalyzer.setProperties(properties);

WildcardAnalyzerProperties wildcardProperties = new WildcardAnalyzerProperties();
wildcardProperties.setNgramSize(3);
wildcardProperties.setAnalyzer(normAnalyzer);

Set<AnalyzerFeature> features = new HashSet<>();
features.add(AnalyzerFeature.frequency);
features.add(AnalyzerFeature.position);

WildcardAnalyzer wildcardAnalyzer = new WildcardAnalyzer();
wildcardAnalyzer.setName("test-" + UUID.randomUUID());
wildcardAnalyzer.setProperties(wildcardProperties);
wildcardAnalyzer.setFeatures(features);

createGetAndDeleteTypedAnalyzer(db, wildcardAnalyzer);
}

@ParameterizedTest
@MethodSource("dbs")
void offsetFeature(ArangoDatabase db) {
Expand Down