public BooleanQuery clone(final BooleanParent newParent, final Occur occur, final boolean generated) { final BooleanQuery bq = new BooleanQuery(newParent, occur, generated); for (final BooleanClause clause : clauses) { bq.addClause(clause.clone(bq, generated)); } return bq; } }
@Override public Node visit(final BooleanQuery booleanQuery) { final BooleanQuery newBQ = new BooleanQuery((BooleanParent) getNewParent(), booleanQuery.occur, true); newParentStack.add(newBQ); for (final BooleanClause clause : booleanQuery.getClauses()) { newBQ.addClause((BooleanClause) clause.accept(this)); } newParentStack.removeLast(); return newBQ; }
@Override public BooleanQuery clone(final BooleanParent newParent, final boolean generated) { return clone(newParent, this.occur, generated); }
final List<BooleanClause> clauses = query.getClauses(); : new BooleanQuery(query.getParent(), query.getOccur(), true); newQuery.addClause(clause.clone(newQuery, MUST, true)); } else { newQuery.addClause(clause.clone(newQuery, true));
@Test public void testThatGeneratedIsPropagatedToClauses() throws Exception { BooleanQuery bq = new BooleanQuery(null, SHOULD, false); DisjunctionMaxQuery dmq = new DisjunctionMaxQuery(bq, Clause.Occur.SHOULD, false); bq.addClause(dmq); dmq.addClause(new Term(dmq, "Test", false)); final BooleanQuery clone = (BooleanQuery) bq.clone(null, MUST, true); assertThat(clone, bq(must(), dmq(should(), term("Test", true)))); assertTrue(clone.isGenerated()); assertTrue(clone.getClauses().get(0).isGenerated()); assertEquals(MUST, clone.getOccur()); } }
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); } } }
@Test public void testThatClonePreservesGeneratedAndOccur() throws Exception { BooleanQuery bq = new BooleanQuery(null, MUST_NOT, true); final BooleanClause clone = bq.clone(null); assertEquals(MUST_NOT, clone.getOccur()); assertTrue(clone.isGenerated()); }
@Override public ExpandedQuery rewrite(final ExpandedQuery query) { final QuerqyQuery<?> userQuery = query.getUserQuery(); if (userQuery instanceof Query){ previousTerms = new ArrayDeque<>(); termsToDelete = new ArrayDeque<>(); nodesToAdd = new LinkedList<>(); visit((Query) userQuery); // append nodesToAdd to parent query nodesToAdd.forEach(node -> { final Node parent = node.getParent(); // TODO: extend BooleanParent? interface so that we don't need this cast? if (parent instanceof DisjunctionMaxQuery) { ((DisjunctionMaxQuery) parent).addClause((DisjunctionMaxClause) node); } else if (parent instanceof BooleanQuery) { ((BooleanQuery) parent).addClause((BooleanClause) node); } else { throw new IllegalStateException("Unknown parent type " + parent.getClass().getName()); } }); termsToDelete.forEach(this::removeIfNotOnlyChild); } return query; }
@Override public T visit(final BooleanQuery booleanQuery) { for (final BooleanClause clause : booleanQuery.getClauses()) { clause.accept(this); } return null; }
@Override public Node visit(BooleanQuery booleanQuery) { if ((!sequencesStack.isEmpty()) && booleanQuery.getParent() instanceof BooleanQuery) { // left-hand siblings might be DMQs with Terms - terminate sequences sequencesStack.getLast().apply(); } // new Sequences object for child DMQ/Term objects sequencesStack.add(new Sequences(synonymMap)); super.visit(booleanQuery); sequencesStack.removeLast().apply(); return null; }
@Override public Node visit(BooleanQuery booleanQuery) { String indend = makeIndend(); writer.print(indend); writer.println(booleanQuery.getOccur() + "BQ: ("); depth++; super.visit(booleanQuery); depth--; writer.println(indend + ")"); return null; };
if (grandParent.getClauses().size() < 2) { continue; grandParent.removeClause(parentQuery);
@Override public Node visitTermQuery(TermQueryContext ctx) { BooleanQuery parent = booleanQueryStack.getLast(); DisjunctionMaxQuery dmq = new DisjunctionMaxQuery(parent, getOccur(), false); TermContext tc = ctx.getRuleContext(TermContext.class, 0); Token startToken = tc.getStart(); List<FieldNameContext> fieldNameContexts = ctx.getRuleContexts(FieldNameContext.class); if (fieldNameContexts != null && !fieldNameContexts.isEmpty()) { for (FieldNameContext fieldNameContext : fieldNameContexts) { String fieldName = fieldNameContext.getText(); dmq.addClause( new Term(dmq, fieldName, new SimpleComparableCharSequence(input, startToken.getStartIndex(), 1 + startToken.getStopIndex() - startToken.getStartIndex()))); } } else { dmq.addClause(new Term(dmq, new SimpleComparableCharSequence(input, startToken.getStartIndex(), 1 + startToken.getStopIndex() - startToken.getStartIndex()))); } parent.addClause(dmq); return dmq; }
@Override public Node visitNoopQuery(NoopQueryContext ctx) { BooleanQuery parent = booleanQueryStack.getLast(); if (parent != this.query) { BooleanQuery bq = new BooleanQuery(parent, getOccur(), false); parent.addClause(bq); operatorStack.add(Operator.NONE); booleanQueryStack.add(query); } super.visitNoopQuery(ctx); if (parent != this.query) { operatorStack.removeLast(); return booleanQueryStack.removeLast(); } else { return parent; } }
@Override public BooleanQuery clone(final DisjunctionMaxQuery newParent, boolean generated) { return clone((BooleanParent) newParent, generated); }
@Override public Node visitBooleanQuery(BooleanQueryContext ctx) { Operator op = Operator.NONE; List<OpAndContext> and = ctx.getRuleContexts(OpAndContext.class); if (and != null && !and.isEmpty()) { op = Operator.AND; } else { List<OpOrContext> or = ctx.getRuleContexts(OpOrContext.class); if (or != null && !or.isEmpty()) { op = Operator.OR; } } BooleanQuery parent = booleanQueryStack.getLast(); BooleanQuery query = new BooleanQuery(parent, getOccur(), false); parent.addClause(query); operatorStack.add(op); booleanQueryStack.add(query); super.visitBooleanQuery(ctx); operatorStack.removeLast(); return booleanQueryStack.removeLast(); }
@Override public BooleanClause clone(final BooleanQuery newParent) { return clone((BooleanParent) newParent); }
protected void decompound(final Term term) { // determine the nodesToAdd based on the term try { for (final SuggestWord[] decompounded : suggestWordbreaks(term)) { if (decompounded != null && decompounded.length > 0) { final BooleanQuery bq = new BooleanQuery(term.getParent(), Clause.Occur.SHOULD, true); for (final SuggestWord word : decompounded) { final DisjunctionMaxQuery dmq = new DisjunctionMaxQuery(bq, Clause.Occur.MUST, true); bq.addClause(dmq); dmq.addClause(new Term(dmq, term.getField(), word.string, true)); } nodesToAdd.add(bq); } } } catch (final IOException e) { // IO is broken, this looks serious -> throw as RTE throw new RuntimeException("Error decompounding " + term, e); } }
@Override public BooleanQuery clone(final BooleanParent newParent) { return clone(newParent, this.occur, this.generated); }
final BooleanQuery bq = new BooleanQuery(match.getParent(), Occur.SHOULD, true); match.getParent().addClause(bq); for (final querqy.rewrite.commonrules.model.Term synTerm: synonym) { final DisjunctionMaxQuery dmq = new DisjunctionMaxQuery(bq, Occur.MUST, true); bq.addClause(dmq); addSynonymTermToDisjunctionMaxQuery(dmq, synTerm, termMatches); addSynonymTermToDisjunctionMaxQuery(clauseDmq, synonym.get(0), termMatches); } else { final BooleanQuery bq = new BooleanQuery(clauseDmq, Occur.SHOULD, true); clauseDmq.addClause(bq); bq.addClause(dmq); addSynonymTermToDisjunctionMaxQuery(dmq, synTerm, termMatches);