public Term clone(final DisjunctionMaxQuery newParent, final boolean isGenerated) { return new Term(newParent, field, value, isGenerated); }
@Override public Node visit(final Term term) { final String value = term.getValue().toString(); final int pos = value.indexOf('$'); if ((pos < 0) || (pos == value.length() - 1) || !Character.isDigit(value.charAt(pos + 1))) { return term; } final querqy.rewrite.commonrules.model.Term charSequence = new querqy.rewrite.commonrules.model.Term( value.toCharArray(), 0, value.length(), term.getField() == null ? null : Collections.singletonList(term.getField())); return new Term(term.getParent(), term.getField(), charSequence, true); }
@Override public Term clone(final DisjunctionMaxQuery newParent) { return clone(newParent, isGenerated()); }
public boolean hasSearchField(String searchField, Term term) { String fieldname = term.getField(); if (fieldname != null) { return term.isGenerated() || (fieldname.equals(searchField) && queryFieldsAndBoostings.containsKey(fieldname)); } else { return term.isGenerated() ? generatedQueryFieldsAndBoostings.containsKey(searchField) : queryFieldsAndBoostings.containsKey(searchField); } }
@Override public Node visit(final Term term) { final ComparableCharSequence value = term.getValue(); if (value instanceof querqy.rewrite.commonrules.model.Term) { querqy.rewrite.commonrules.model.Term termValue = (querqy.rewrite.commonrules.model.Term) value; final ComparableCharSequence newValue = termValue.fillPlaceholders(termMatches); return new Term((DisjunctionMaxQuery) getNewParent(), term.getField(), newValue, true); } else { return term.clone((DisjunctionMaxQuery) getNewParent(), true); } }
final String fieldname = term.getField(); if (fieldBoost == null) { if (fieldname != null && !term.isGenerated() && !searchFieldsAndBoosting.hasSearchField(fieldname, term)) { final Term termWithFieldInValue = new Term(null, new CompoundCharSequence(":", fieldname, term.getValue())); fieldBoost = searchFieldsAndBoosting.getFieldBoost(termWithFieldInValue); if (fieldBoost != null) {
@Override protected boolean matchesSafely(Term item) { return item != null && // expectedField == null -> do not compare field name. (expectedField == null || expectedField.equals(item.getField())) && // Using toString() here to avoid incompatibilities between // equals() of different char sequences. CharSequenceUtil.equals(expectedValue, item.getValue()) && // expectedValue.equals(item.getValue().toString()) && ((expectedGenerated == null) || (expectedGenerated.booleanValue() == item.isGenerated())) ; } }
DisjunctionMaxQuery currentDmq = term.getParent(); DisjunctionMaxQuery newDmq = new DisjunctionMaxQuery(replaceSeq, Occur.MUST, true); newDmq.addClause( new Term(newDmq, new SimpleComparableCharSequence(scratchChars, start, i - start))); replaceSeq.addClause(newDmq); newDmq.addClause(new Term(newDmq, new SimpleComparableCharSequence(scratchChars, start, scratchChars.length - start))); replaceSeq.addClause(newDmq); replaceDmq.addClause(new Term(replaceDmq, new SimpleComparableCharSequence(scratchChars, 0, scratchChars.length))); add.addClause(replaceDmq); neqDmq.addClause(negTerm.clone(neqDmq, true)); neq.addClause(neqDmq);
@Override public Term next() { tryFillSlotIfEmpty(); if (slot == null || !eq(slot.getField(), field)) { throw new NoSuchElementException("No more terms"); } else { Term term = slot; slot = null; return term; } }
public CacheKey(String fieldname, Term term) { this.fieldname = fieldname; // this.term = term; value = term.getValue(); }
@Override public void registerTermSubQuery(String fieldname, TermSubQueryFactory termSubQueryFactory, Term sourceTerm) { if (sourceTerm.isGenerated()) { generatedFields.add(fieldname); } }
@Override public ExpandedQuery rewrite(final ExpandedQuery query) { final QuerqyQuery<?> userQuery = query.getUserQuery(); if (userQuery != null && userQuery instanceof Query){ previousTerm = null; termsToAdd = new LinkedList<>(); visit((Query) userQuery); for (Term term : termsToAdd) { term.getParent().addClause(term); } } return query; }
public TermMatch(querqy.model.Term queryTerm, boolean isPrefix, ComparableCharSequence wildcardMatch) { if (isPrefix) { if ((wildcardMatch == null) || (wildcardMatch.length() == 0)) { throw new IllegalArgumentException("Need a wildcard match if isPrefix for " + queryTerm.toString()); } } this.queryTerm = queryTerm; this.isPrefix = isPrefix; this.wildcardMatch = wildcardMatch; }
public Set<String> getSearchFields(Term term) { String fieldname = term.getField(); if (fieldname != null) { if (term.isGenerated() || queryFieldsAndBoostings.containsKey(fieldname)) { return new HashSet<>(Collections.singletonList(fieldname)); } else { return Collections.emptySet(); } } else { return term.isGenerated() ? generatedQueryFieldsAndBoostings.keySet() : queryFieldsAndBoostings.keySet(); } }
public boolean isPrefixOf(final querqy.model.Term other) { if (isPrefixOfCharSequence(other)) { final String otherFieldname = other.getField(); if (fieldNames == null) { return true; } else if (otherFieldname != null && fieldNames.contains(otherFieldname)){ return true; } } return false; }
@Override public String toString() { return ((field == null) ? "*" : field) + ":" + getValue(); }
@Override public Node visit(Term term) { if (!term.isGenerated()) { try { sequencesStack.getLast().putTerm(term); } catch (IOException e) { throw new RuntimeException(e); } } return super.visit(term); }
public void removeIfNotOnlyChild(final Term term) { // remove the term from its parent. If the parent doesn't have any further child, // remove the parent from the grand-parent. If this also hasn't any further child, // do not remove anything // TODO: go until top level? final DisjunctionMaxQuery parentQuery = term.getParent(); if (parentQuery.getClauses().size() > 1) { parentQuery.removeClause(term); } else { final BooleanQuery grandParent = parentQuery.getParent(); if (grandParent != null && grandParent.getClauses().size() > 1) { grandParent.removeClause(parentQuery); } } }
protected void preloadTerm(final IndexSearcher searcher, final TermSubQueryBuilder termSubQueryBuilder, final String field, final Term term, final boolean testForHits, final TermQueryCache cache) { try { // luceneQueryBuilder.termToFactory creates the query and caches it (without the boost) final TermSubQueryFactory termSubQueryFactory = termSubQueryBuilder.termToFactory(field, term, ConstantFieldBoost.NORM_BOOST); // test the query for hits and override the cache value with a factory that creates a query that never matches // --> this query will never be executed against the index again // no need to re-test for hits if we've seen this term before if (testForHits && (termSubQueryFactory != null) && (!termSubQueryFactory.isNeverMatchQuery())) { final Query query = termSubQueryFactory .createQuery(ConstantFieldBoost.NORM_BOOST, 0.01f, new LuceneTermQueryBuilder()); final TopDocs topDocs = searcher.search(query, 1); if (topDocs.totalHits < 1) { cache.put(new CacheKey(field, term), new TermQueryCacheValue(NeverMatchQueryFactory.FACTORY, PRMSQuery.NEVER_MATCH_PRMS_QUERY)); } } } catch (final IOException e) { LOG.error("Error preloading term " + term.toString(), e); } }