@Override public List<Lookup.LookupResult> lookup(CharSequence key, Set<BytesRef> contexts, int num, boolean allTermsRequired, boolean doHighlight) throws IOException { // Don't * numFactor here since we do it down below, once, in the call chain: return super.lookup(key, contexts, num, allTermsRequired, doHighlight); }
/** Adds a new suggestion. Be sure to use {@link #update} * instead if you want to replace a previous suggestion. * After adding or updating a batch of new suggestions, * you must call {@link #refresh} in the end in order to * see the suggestions in {@link #lookup} */ public void add(BytesRef text, Set<BytesRef> contexts, long weight, BytesRef payload) throws IOException { ensureOpen(); writer.addDocument(buildDocument(text, contexts, weight, payload)); }
/** Retrieve suggestions, specifying whether all terms * must match ({@code allTermsRequired}) and whether the hits * should be highlighted ({@code doHighlight}). */ public List<LookupResult> lookup(CharSequence key, Map<BytesRef, BooleanClause.Occur> contextInfo, int num, boolean allTermsRequired, boolean doHighlight) throws IOException { return lookup(key, toQuery(contextInfo), num, allTermsRequired, doHighlight); }
public static List<Lookup.LookupResult> getSuggestions(AnalyzingInfixSuggester suggester, @Nullable SuggestQuery suggestQuery) { try { if (suggester != null && suggester.getCount() > 0) { return suggester.lookup(suggestQuery.getText(), 10, true, false); } else { return Collections.emptyList(); } } catch (Exception e) { throw new RuntimeException("could not handle Suggest query " + suggestQuery, e); } }
getIndexWriterConfig(getGramAnalyzer(), IndexWriterConfig.OpenMode.CREATE)); add(text, iter.contexts(), iter.weight(), payload); commit();
int endOffset = offsetAtt.endOffset(); if (upto < startOffset) { addNonMatch(sb, text.substring(upto, startOffset)); upto = startOffset; } else if (upto > startOffset) { addWholeMatch(sb, text.substring(startOffset, endOffset), token); upto = endOffset; } else if (prefixToken != null && token.startsWith(prefixToken)) { addPrefixMatch(sb, text.substring(startOffset, endOffset), token, prefixToken); upto = endOffset; int endOffset = offsetAtt.endOffset(); if (upto < endOffset) { addNonMatch(sb, text.substring(upto));
for (String text : texts) { try { this.suggester.add(new BytesRef(text), contexts, weight, payload); } catch (Exception ex) { LOGGER.error("Failed to add text for field {} to autosuggest {}", field, this._name);
private BooleanQuery toQuery(Map<BytesRef,BooleanClause.Occur> contextInfo) { if (contextInfo == null || contextInfo.isEmpty()) { return null; } BooleanQuery.Builder contextFilter = new BooleanQuery.Builder(); for (Map.Entry<BytesRef,BooleanClause.Occur> entry : contextInfo.entrySet()) { addContextToQuery(contextFilter, entry.getKey(), entry.getValue()); } return contextFilter.build(); }
AnalyzingInfixSuggester suggester = closer.register(getLookup(directory, analyzer, tempSubChild)); shouldCloseDirectory = false; suggester.build(dictionary);
private AutoSuggest(String name, Index index, Directory dir, Analyzer indexAnalyzer, Analyzer searchAnalyzer, boolean useTerms, int minChars) throws IndexException { this._name = name; this._index = index; this._useTerms = useTerms; try { this.suggester = new AnalyzingInfixSuggester(dir, indexAnalyzer, searchAnalyzer, minChars, true, true, true); } catch (IOException ex) { LOGGER.error("Failed to build autosuggest {}", this._name, ex); throw new IndexException("Failed to build autosuggest", ex); } }
/** Called while highlighting a single result, to append a * matched prefix token, to the provided fragments list. * @param sb The {@code StringBuilder} to append to * @param surface The fragment of the surface form * (indexed during {@link #build}, corresponding to * this match * @param analyzed The analyzed token that matched * @param prefixToken The prefix of the token that matched */ protected void addPrefixMatch(StringBuilder sb, String surface, String analyzed, String prefixToken) { // TODO: apps can try to invert their analysis logic // here, e.g. downcase the two before checking prefix: if (prefixToken.length() >= surface.length()) { addWholeMatch(sb, surface, analyzed); return; } sb.append("<b>"); sb.append(surface.substring(0, prefixToken.length())); sb.append("</b>"); sb.append(surface.substring(prefixToken.length())); }
public static List<Lookup.LookupResult> getSuggestions(AnalyzingInfixSuggester suggester, @Nullable SuggestQuery suggestQuery) { try { if (suggester != null && suggester.getCount() > 0) { return suggester.lookup(suggestQuery.getText(), 10, true, false); } else { return Collections.emptyList(); } } catch (Exception e) { throw new RuntimeException("could not handle Suggest query " + suggestQuery, e); } }
/** Lookup, with context but without booleans. Context booleans default to SHOULD, * so each suggestion must have at least one of the contexts. */ public List<LookupResult> lookup(CharSequence key, Set<BytesRef> contexts, int num, boolean allTermsRequired, boolean doHighlight) throws IOException { return lookup(key, toQuery(contexts), num, allTermsRequired, doHighlight); }
private BooleanQuery toQuery(Set<BytesRef> contextInfo) { if (contextInfo == null || contextInfo.isEmpty()) { return null; } BooleanQuery.Builder contextFilter = new BooleanQuery.Builder(); for (BytesRef context : contextInfo) { addContextToQuery(contextFilter, context, BooleanClause.Occur.SHOULD); } return contextFilter.build(); }
AnalyzingInfixSuggester suggester = closer.register(getLookup(directory, analyzer, tempSubChild)); shouldCloseDirectory = false; suggester.build(dictionary);
@Override public List<Lookup.LookupResult> lookup(CharSequence key, Set<BytesRef> contexts, boolean onlyMorePopular, int num) throws IOException { // Don't * numFactor here since we do it down below, once, in the call chain: return super.lookup(key, contexts, onlyMorePopular, num); }
/** Updates a previous suggestion, matching the exact same * text as before. Use this to change the weight or * payload of an already added suggestion. If you know * this text is not already present you can use {@link * #add} instead. After adding or updating a batch of * new suggestions, you must call {@link #refresh} in the * end in order to see the suggestions in {@link #lookup} */ public void update(BytesRef text, Set<BytesRef> contexts, long weight, BytesRef payload) throws IOException { ensureOpen(); writer.updateDocument(new Term(EXACT_TEXT_FIELD_NAME, text.utf8ToString()), buildDocument(text, contexts, weight, payload)); }
@Override public List<Lookup.LookupResult> lookup(CharSequence key, BooleanQuery contextQuery, int num, boolean allTermsRequired, boolean doHighlight) throws IOException { /** We need to do num * numFactor here only because it is the last call in the lookup chain*/ return super.lookup(key, contextQuery, num * numFactor, allTermsRequired, doHighlight); }
@Override public List<Lookup.LookupResult> lookup(CharSequence key, Map<BytesRef, BooleanClause.Occur> contextInfo, int num, boolean allTermsRequired, boolean doHighlight) throws IOException { // Don't * numFactor here since we do it down below, once, in the call chain: return super.lookup(key, contextInfo, num, allTermsRequired, doHighlight); }