summaryrefslogtreecommitdiffstats
path: root/container-search/src/main/java/com/yahoo/prelude/query
diff options
context:
space:
mode:
authorJon Bratseth <bratseth@gmail.com>2021-05-05 12:12:25 +0200
committerJon Bratseth <bratseth@gmail.com>2021-05-05 12:12:25 +0200
commit4b3e615954fa5cbaf761a55cddba786f51ab26f3 (patch)
tree31cc973d490bdc7b8257c664b31018ac8915efc4 /container-search/src/main/java/com/yahoo/prelude/query
parentf8c8b6ab958f930b318eadc92b760dd26f414e58 (diff)
Revert "Merge pull request #17746 from vespa-engine/revert-17738-revert-17737-revert-17736-bratseth/special-tokens"
This reverts commit 491856b396d003885e159345fe3f533f0fa35933, reversing changes made to 3720186303f4aef1d185525eaf61092097a64ec9.
Diffstat (limited to 'container-search/src/main/java/com/yahoo/prelude/query')
-rw-r--r--container-search/src/main/java/com/yahoo/prelude/query/parser/AbstractParser.java3
-rw-r--r--container-search/src/main/java/com/yahoo/prelude/query/parser/SpecialTokenRegistry.java137
-rw-r--r--container-search/src/main/java/com/yahoo/prelude/query/parser/SpecialTokens.java167
-rw-r--r--container-search/src/main/java/com/yahoo/prelude/query/parser/Tokenizer.java34
4 files changed, 22 insertions, 319 deletions
diff --git a/container-search/src/main/java/com/yahoo/prelude/query/parser/AbstractParser.java b/container-search/src/main/java/com/yahoo/prelude/query/parser/AbstractParser.java
index 902be7e15dd..732466748eb 100644
--- a/container-search/src/main/java/com/yahoo/prelude/query/parser/AbstractParser.java
+++ b/container-search/src/main/java/com/yahoo/prelude/query/parser/AbstractParser.java
@@ -19,7 +19,6 @@ import java.util.*;
* @author bratseth
* @author Steinar Knutsen
*/
-@SuppressWarnings("deprecation")
public abstract class AbstractParser implements CustomParser {
/** The current submodes of this parser */
@@ -48,7 +47,7 @@ public abstract class AbstractParser implements CustomParser {
* of these may be active at the same time. SubModes are activated or
* deactivated by specifying special indexes in the query.
*/
- final class Submodes {
+ static final class Submodes {
/**
* Url mode allows "_" and "-" as word characters. Default is false
diff --git a/container-search/src/main/java/com/yahoo/prelude/query/parser/SpecialTokenRegistry.java b/container-search/src/main/java/com/yahoo/prelude/query/parser/SpecialTokenRegistry.java
deleted file mode 100644
index be2d9f9f68b..00000000000
--- a/container-search/src/main/java/com/yahoo/prelude/query/parser/SpecialTokenRegistry.java
+++ /dev/null
@@ -1,137 +0,0 @@
-// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
-package com.yahoo.prelude.query.parser;
-
-import com.yahoo.config.subscription.ConfigGetter;
-import com.yahoo.config.subscription.ConfigSubscriber;
-import com.yahoo.vespa.configdefinition.SpecialtokensConfig;
-import com.yahoo.vespa.configdefinition.SpecialtokensConfig.Tokenlist;
-import com.yahoo.vespa.configdefinition.SpecialtokensConfig.Tokenlist.Tokens;
-
-import java.util.*;
-import java.util.logging.Logger;
-
-
-/**
- * A <i>registry</i> which is responsible for knowing the current
- * set of special tokens. The default registry returns empty token lists
- * for all names. Usage of this registry is multithread safe.
- *
- * @author bratseth
- */
-public class SpecialTokenRegistry {
-
- /** The log of this */
- private static final Logger log = Logger.getLogger(SpecialTokenRegistry.class.getName());
-
- private static final SpecialTokens nullSpecialTokens = new SpecialTokens();
-
- /**
- * The current authorative special token lists, indexed on name.
- * These lists are unmodifiable and used directly by clients of this
- */
- private Map<String,SpecialTokens> specialTokenMap = new HashMap<>();
-
- private boolean frozen = false;
-
- /**
- * Creates an empty special token registry which
- * does not subscribe to any configuration
- */
- public SpecialTokenRegistry() {}
-
- /**
- * Create a special token registry which subscribes to the specialtokens
- * configuration. Only used for testing.
- */
- public SpecialTokenRegistry(String configId) {
- try {
- build(new ConfigGetter<>(SpecialtokensConfig.class).getConfig(configId));
- } catch (Exception e) {
- log.config(
- "No special tokens are configured (" + e.getMessage() + ")");
- }
- }
-
- /**
- * Create a special token registry from a configuration object. This is the production code path.
- */
- public SpecialTokenRegistry(SpecialtokensConfig config) {
- if (config != null) {
- build(config);
- }
- freeze();
- }
-
- private void freeze() {
- frozen = true;
- }
-
- private void build(SpecialtokensConfig config) {
- List<SpecialTokens> list = new ArrayList<>();
- for (Iterator<Tokenlist> i = config.tokenlist().iterator(); i.hasNext();) {
- Tokenlist tokenList = i.next();
- SpecialTokens tokens = new SpecialTokens(tokenList.name());
-
- for (Iterator<Tokens> j = tokenList.tokens().iterator(); j.hasNext();) {
- Tokens token = j.next();
- tokens.addSpecialToken(token.token(), token.replace());
- }
- tokens.freeze();
- list.add(tokens);
- }
- addSpecialTokens(list);
- }
-
- /**
- * Adds a SpecialTokens instance to the registry. That is, add the
- * tokens contained for the name of the SpecialTokens instance
- * given.
- *
- * @param specialTokens the SpecialTokens object to add
- */
- public void addSpecialTokens(SpecialTokens specialTokens) {
- ensureNotFrozen();
- List<SpecialTokens> list = new ArrayList<>();
- list.add(specialTokens);
- addSpecialTokens(list);
-
- }
-
- private void ensureNotFrozen() {
- if (frozen) {
- throw new IllegalStateException("Tried to modify a frozen SpecialTokenRegistry instance.");
- }
- }
-
- private void addSpecialTokens(List<SpecialTokens> list) {
- HashMap<String,SpecialTokens> tokens = new HashMap<>(specialTokenMap);
- for(SpecialTokens t: list) {
- tokens.put(t.getName(),t);
- }
- specialTokenMap = tokens;
- }
-
-
- /**
- * Returns the currently authorative list of special tokens for
- * a given name.
- *
- * @param name the name of the special tokens to return
- * null, the empth string or the string "default" returns
- * the default ones
- * @return a read-only list of SpecialToken instances, an empty list if this name
- * has no special tokens
- */
- public SpecialTokens getSpecialTokens(String name) {
- if (name == null || name.trim().equals("")) {
- name = "default";
- }
- SpecialTokens specialTokens = specialTokenMap.get(name);
-
- if (specialTokens == null) {
- return nullSpecialTokens;
- }
- return specialTokens;
- }
-
-}
diff --git a/container-search/src/main/java/com/yahoo/prelude/query/parser/SpecialTokens.java b/container-search/src/main/java/com/yahoo/prelude/query/parser/SpecialTokens.java
deleted file mode 100644
index f45ecefefa6..00000000000
--- a/container-search/src/main/java/com/yahoo/prelude/query/parser/SpecialTokens.java
+++ /dev/null
@@ -1,167 +0,0 @@
-// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
-package com.yahoo.prelude.query.parser;
-
-import java.util.logging.Level;
-import com.yahoo.prelude.query.Substring;
-
-import java.util.*;
-import java.util.logging.Logger;
-
-import static com.yahoo.language.LinguisticsCase.toLowerCase;
-
-/**
- * A list of special tokens - string that should be treated as word
- * no matter what they contain. Special tokens are case insensitive.
- *
- * @author bratseth
- */
-public class SpecialTokens {
-
- private static final Logger log = Logger.getLogger(SpecialTokens.class.getName());
-
- private final String name;
-
- private final List<SpecialToken> specialTokens = new ArrayList<>();
-
- private boolean frozen = false;
-
- private int currentMaximumLength = 0;
-
- /** Creates a null list of special tokens */
- public SpecialTokens() {
- this.name = "(null)";
- }
-
- public SpecialTokens(String name) {
- this.name = name;
- }
-
- /** Returns the name of this special tokens list */
- public String getName() {
- return name;
- }
-
- /**
- * Adds a special token to this
- *
- * @param token the special token string to add
- * @param replace the token to replace instances of the special token with, or null to keep the token
- */
- public void addSpecialToken(String token, String replace) {
- ensureNotFrozen();
- if (!caseIndependentLength(token)) {
- return;
- }
- // TODO are special tokens correctly unicode normalized in reagards to query parsing?
- final SpecialToken specialTokenToAdd = new SpecialToken(token, replace);
- currentMaximumLength = Math.max(currentMaximumLength, specialTokenToAdd.token.length());
- specialTokens.add(specialTokenToAdd);
- Collections.sort(specialTokens);
- }
-
- private boolean caseIndependentLength(String token) {
- // XXX not fool proof length test, should test codepoint by codepoint for mixed case user input? not even that will necessarily be 100% robust...
- String asLow = toLowerCase(token);
- // TODO put along with the global toLowerCase
- String asHigh = token.toUpperCase(Locale.ENGLISH);
- if (asLow.length() != token.length() || asHigh.length() != token.length()) {
- log.log(Level.SEVERE, "Special token '" + token + "' has case sensitive length. Ignoring the token."
- + " Please report this message in a bug to the Vespa team.");
- return false;
- } else {
- return true;
- }
- }
-
- /**
- * Returns the special token starting at the start of the given string, or null if no
- * special token starts at this string
- *
- * @param string the string to search for a special token at the start position
- * @param substring true to allow the special token to be followed by a character which does not
- * mark the end of a token
- */
- public SpecialToken tokenize(String string, boolean substring) {
- // XXX detonator pattern token.length may be != the length of the
- // matching data in string, ref caseIndependentLength(String)
- final String input = toLowerCase(string.substring(0, Math.min(string.length(), currentMaximumLength)));
- for (Iterator<SpecialToken> i = specialTokens.iterator(); i.hasNext();) {
- SpecialTokens.SpecialToken special = i.next();
-
- if (input.startsWith(special.token())) {
- if (string.length() == special.token().length() || substring || tokenEndsAt(special.token().length(), string))
- return special;
- }
- }
- return null;
- }
-
- private boolean tokenEndsAt(int position,String string) {
- return !Character.isLetterOrDigit(string.charAt(position));
- }
-
- /** Returns the number of special tokens in this */
- public int size() {
- return specialTokens.size();
- }
-
- private void ensureNotFrozen() {
- if (frozen) {
- throw new IllegalStateException("Tried to modify a frozen SpecialTokens instance.");
- }
- }
-
- public void freeze() {
- frozen = true;
- }
-
- /** An immutable special token */
- public final static class SpecialToken implements Comparable<SpecialToken> {
-
- private String token;
-
- private String replace;
-
- public SpecialToken(String token, String replace) {
- this.token = toLowerCase(token);
- if (replace == null || replace.trim().equals("")) {
- this.replace = this.token;
- } else {
- this.replace = toLowerCase(replace);
- }
- }
-
- /** Returns the special token */
- public String token() {
- return token;
- }
-
- /** Returns the right replace value, never null or an empty string */
- public String replace() {
- return replace;
- }
-
- @Override
- public int compareTo(SpecialToken other) {
- if (this.token().length() < other.token().length()) return 1;
- if (this.token().length() == other.token().length()) return 0;
- return -1;
- }
-
- @Override
- public boolean equals(Object other) {
- if (other == this) return true;
- if ( ! (other instanceof SpecialToken)) return false;
- return Objects.equals(this.token, ((SpecialToken)other).token);
- }
-
- @Override
- public int hashCode() { return token.hashCode(); }
-
- public Token toToken(int start, String rawSource) {
- return new Token(Token.Kind.WORD, replace(), true, new Substring(start, start + token.length(), rawSource)); // XXX: Unsafe?
- }
-
- }
-
-}
diff --git a/container-search/src/main/java/com/yahoo/prelude/query/parser/Tokenizer.java b/container-search/src/main/java/com/yahoo/prelude/query/parser/Tokenizer.java
index 2dc2254df68..b71bd57539f 100644
--- a/container-search/src/main/java/com/yahoo/prelude/query/parser/Tokenizer.java
+++ b/container-search/src/main/java/com/yahoo/prelude/query/parser/Tokenizer.java
@@ -3,6 +3,7 @@ package com.yahoo.prelude.query.parser;
import com.yahoo.language.Linguistics;
import com.yahoo.language.process.CharacterClasses;
+import com.yahoo.language.process.SpecialTokens;
import com.yahoo.prelude.Index;
import com.yahoo.prelude.IndexFacts;
import com.yahoo.prelude.query.Substring;
@@ -200,7 +201,7 @@ public final class Tokenizer {
}
StringBuilder tmp = new StringBuilder();
for (int i = 0; i < tokencnt; i++) {
- Token useToken = tokens.get(backtrack+i);
+ Token useToken = tokens.get(backtrack + i);
tmp.append(useToken.image);
}
String indexName = tmp.toString();
@@ -216,20 +217,20 @@ public final class Tokenizer {
}
private int consumeSpecialToken(int start) {
- SpecialTokens.SpecialToken specialToken=getSpecialToken(start);
- if (specialToken==null) return start;
- tokens.add(specialToken.toToken(start,source));
- return start + specialToken.token().length();
+ SpecialTokens.Token token = getSpecialToken(start);
+ if (token == null) return start;
+ tokens.add(toToken(token, start, source));
+ return start + token.token().length();
}
- private SpecialTokens.SpecialToken getSpecialToken(int start) {
+ private SpecialTokens.Token getSpecialToken(int start) {
if (specialTokens == null) return null;
return specialTokens.tokenize(source.substring(start), substringSpecialTokens);
}
private int consumeExact(int start,Index index) {
if (index.getExactTerminator() == null) return consumeHeuristicExact(start);
- return consumeToTerminator(start,index.getExactTerminator());
+ return consumeToTerminator(start, index.getExactTerminator());
}
private boolean looksLikeExactEnd(int end) {
@@ -467,7 +468,7 @@ public final class Tokenizer {
/** Consumes a word or number <i>and/or possibly</i> a special token starting within this word or number */
private int consumeWordOrNumber(int start, Index currentIndex) {
int tokenEnd = start;
- SpecialTokens.SpecialToken substringSpecialToken = null;
+ SpecialTokens.Token substringToken = null;
boolean digitsOnly = true;
// int underscores = 0;
// boolean underscoresOnly = true;
@@ -475,8 +476,8 @@ public final class Tokenizer {
while (tokenEnd < source.length()) {
if (substringSpecialTokens) {
- substringSpecialToken = getSpecialToken(tokenEnd);
- if (substringSpecialToken != null) break;
+ substringToken = getSpecialToken(tokenEnd);
+ if (substringToken != null) break;
}
int c = source.codePointAt(tokenEnd);
@@ -524,11 +525,11 @@ public final class Tokenizer {
}
}
- if (substringSpecialToken == null)
+ if (substringToken == null)
return --tokenEnd;
// TODO: test the logic around tokenEnd with friends
- addToken(substringSpecialToken.toToken(tokenEnd, source));
- return --tokenEnd + substringSpecialToken.token().length();
+ addToken(toToken(substringToken, tokenEnd, source));
+ return --tokenEnd + substringToken.token().length();
}
private void addToken(Token.Kind kind, String word, int start, int end) {
@@ -539,4 +540,11 @@ public final class Tokenizer {
tokens.add(token);
}
+ public Token toToken(SpecialTokens.Token specialToken, int start, String rawSource) {
+ return new Token(Token.Kind.WORD,
+ specialToken.replacement(),
+ true,
+ new Substring(start, start + specialToken.token().length(), rawSource)); // XXX: Unsafe?
+ }
+
}