From e23246f9f33103d09f012f4d8cb089ebc6f82698 Mon Sep 17 00:00:00 2001 From: Henning Baldersheim Date: Sun, 27 May 2018 20:02:42 +0200 Subject: Iterate over indexes for ngram and quoting searcher. --- .../yahoo/prelude/searcher/QuotingSearcher.java | 88 ++++++++-------------- 1 file changed, 33 insertions(+), 55 deletions(-) (limited to 'container-search/src/main/java/com/yahoo/prelude/searcher/QuotingSearcher.java') diff --git a/container-search/src/main/java/com/yahoo/prelude/searcher/QuotingSearcher.java b/container-search/src/main/java/com/yahoo/prelude/searcher/QuotingSearcher.java index d4cad7f1246..5dcc533fb1f 100644 --- a/container-search/src/main/java/com/yahoo/prelude/searcher/QuotingSearcher.java +++ b/container-search/src/main/java/com/yahoo/prelude/searcher/QuotingSearcher.java @@ -35,6 +35,7 @@ public class QuotingSearcher extends Searcher { } private static class QuoteTable { + private final int lowerUncachedBound; private final int upperUncachedBound; private final Map quoteMap; @@ -50,12 +51,10 @@ public class QuotingSearcher extends Searcher { boolean newIsEmpty = true; Map newQuoteMap = new HashMap<>(); for (Iterator i = config.character().iterator(); i.hasNext(); ) { - QrQuotetableConfig.Character character - = (QrQuotetableConfig.Character)i.next(); + QrQuotetableConfig.Character character = (QrQuotetableConfig.Character)i.next(); if (character.ordinal() > 256) { newIsEmpty = false; - newQuoteMap.put(new Character((char)character.ordinal()), - character.quoting()); + newQuoteMap.put(new Character((char)character.ordinal()), character.quoting()); newUseMap = true; if (minOrd == 0 || character.ordinal() < minOrd) minOrd = character.ordinal(); @@ -64,8 +63,7 @@ public class QuotingSearcher extends Searcher { } else { newIsEmpty = false; - newLowerTable[character.ordinal()] - = character.quoting(); + newLowerTable[character.ordinal()] = character.quoting(); } } lowerUncachedBound = minOrd; @@ -75,22 +73,19 @@ public class QuotingSearcher extends Searcher { isEmpty = newIsEmpty; lowerTable = newLowerTable; } + public String get(char c) { - if (isEmpty) - return null; + if (isEmpty) return null; + int ord = (int)c; if (ord < 256) { return lowerTable[ord]; } else { - if ((!useMap) || ord < lowerUncachedBound - || ord > upperUncachedBound) - { + if ((!useMap) || ord < lowerUncachedBound || ord > upperUncachedBound) return null; - } - else { + else return quoteMap.get(new Character(c)); - } } } public boolean isEmpty() { @@ -107,35 +102,29 @@ public class QuotingSearcher extends Searcher { Result result = execution.search(query); execution.fill(result); QuoteTable translations = getQuoteTable(); - if (translations == null || translations.isEmpty()) { - return result; - } + if (translations == null || translations.isEmpty()) return result; + for (Iterator i = result.hits().deepIterator(); i.hasNext(); ) { Hit h = i.next(); - if (h instanceof FastHit) { - quoteProperties((FastHit)h, translations); - } + if (h instanceof FastHit) + quoteFields((FastHit) h, translations); } return result; } - private void quoteProperties(FastHit hit, QuoteTable translations) { - for (Iterator i = ((Set) hit.fields().keySet()).iterator(); i.hasNext(); ) { - String propertyName = (String) i.next(); - Object entry = hit.getField(propertyName); - if (entry == null) { - continue; - } - Class propertyType = entry.getClass(); - if (propertyType.equals(HitField.class)) { - quoteField((HitField) entry, translations); - } else if (propertyType.equals(String.class)) { - quoteProperty(hit, propertyName, (String)entry, translations); + private void quoteFields(FastHit hit, QuoteTable translations) { + hit.forEachField((fieldName, fieldValue) -> { + if (fieldValue != null) { + Class fieldType = fieldValue.getClass(); + if (fieldType.equals(HitField.class)) + quoteField((HitField) fieldValue, translations); + else if (fieldType.equals(String.class)) + quoteField(hit, fieldName, (String) fieldValue, translations); } - } + }); } - private void quoteProperty(Hit hit, String fieldname, String toQuote, QuoteTable translations) { + private void quoteField(Hit hit, String fieldname, String toQuote, QuoteTable translations) { List l = translate(toQuote, translations, true); if (l != null) { HitField hf = new HitField(fieldname, toQuote); @@ -144,13 +133,11 @@ public class QuotingSearcher extends Searcher { } } - private void quoteField(HitField field, QuoteTable translations) { for (ListIterator i = field.listIterator(); i.hasNext(); ) { FieldPart f = i.next(); - if (!f.isFinal()) { - List newFieldParts = translate(f.getContent(), translations, - f.isToken()); + if ( ! f.isFinal()) { + List newFieldParts = translate(f.getContent(), translations, f.isToken()); if (newFieldParts != null) { i.remove(); for (Iterator j = newFieldParts.iterator(); j.hasNext(); ) { @@ -161,33 +148,24 @@ public class QuotingSearcher extends Searcher { } } - private List translate(String toQuote, QuoteTable translations, - boolean isToken) { + private List translate(String toQuote, QuoteTable translations, boolean isToken) { List newFieldParts = null; int lastIdx = 0; for (int i = 0; i < toQuote.length(); i++) { String quote = translations.get(toQuote.charAt(i)); if (quote != null) { - if (newFieldParts == null) { + if (newFieldParts == null) newFieldParts = new ArrayList<>(); - } - if (lastIdx != i) { - newFieldParts.add( - new StringFieldPart(toQuote.substring(lastIdx, i), - isToken)); - } + if (lastIdx != i) + newFieldParts.add(new StringFieldPart(toQuote.substring(lastIdx, i), isToken)); String initContent = Character.toString(toQuote.charAt(i)); - newFieldParts.add(new ImmutableFieldPart(initContent, - quote, - isToken)); + newFieldParts.add(new ImmutableFieldPart(initContent, quote, isToken)); lastIdx = i+1; } } - if (lastIdx > 0 && lastIdx < toQuote.length()) { - newFieldParts.add( - new StringFieldPart(toQuote.substring(lastIdx), - isToken)); - } + if (lastIdx > 0 && lastIdx < toQuote.length()) + newFieldParts.add(new StringFieldPart(toQuote.substring(lastIdx), isToken)); return newFieldParts; } + } -- cgit v1.2.3