@Override public SpanQuery getSpanQuery(Element e) throws ParserException { Element includeElem = DOMUtils.getChildByTagOrFail(e, "Include"); includeElem = DOMUtils.getFirstChildOrFail(includeElem); Element excludeElem = DOMUtils.getChildByTagOrFail(e, "Exclude"); excludeElem = DOMUtils.getFirstChildOrFail(excludeElem); SpanQuery include = factory.getSpanQuery(includeElem); SpanQuery exclude = factory.getSpanQuery(excludeElem); SpanNotQuery snq = new SpanNotQuery(include, exclude); snq.setBoost(DOMUtils.getAttribute(e, "boost", 1.0f)); return snq; }
public SpanQuery toFragmentQuery () throws QueryException { if (this.isNull || (this.inclusive.size() + this.exclusive.size() == 0)) { return (SpanQuery) null; } else if (this.inclusive.size() >= 1 && this.exclusive.size() >= 1) { return (SpanQuery) new SpanNotQuery( this._listToQuery(this.inclusive), this._listToOrQuery(this.exclusive)); } // These are now identical but may be negative else if (this.inclusive.size() == 0 && this.exclusive.size() >= 1) { return (SpanQuery) this._listToQuery(this.exclusive); } else if (this.inclusive.size() >= 1 && this.exclusive.size() == 0) { return (SpanQuery) this._listToQuery(this.inclusive); }; return (SpanQuery) null; }
/** * Rewrite a span-based NOT query. The procedure in this case is simple: * simply rewrite both the include and exclude clauses. * * @param nq The query to rewrite * @return Rewritten version, or 'nq' unchanged if no changed needed. */ protected Query rewrite(SpanNotQuery nq) { // Rewrite the sub-queries SpanQuery include = (SpanQuery)rewriteQuery(nq.getInclude()); SpanQuery exclude = (SpanQuery)rewriteQuery(nq.getExclude()); // If the sub-queries didn't change, then neither does this NOT. if (include == nq.getInclude() && exclude == nq.getExclude() && !forceRewrite(nq)) return nq; // Make a new NOT query Query newq = new SpanNotQuery(include, exclude); copyBoost(nq, newq); return newq; }
public void testExtractQueryMetadata_spanNotQuery() { SpanTermQuery spanTermQuery1 = new SpanTermQuery(new Term("_field", "_short_term")); SpanTermQuery spanTermQuery2 = new SpanTermQuery(new Term("_field", "_very_long_term")); SpanNotQuery spanNotQuery = new SpanNotQuery(spanTermQuery1, spanTermQuery2); Result result = analyze(spanNotQuery); assertThat(result.verified, is(false)); assertTermsEqual(result.terms, spanTermQuery1.getTerm()); }
@Override protected void doAssertLuceneQuery(SpanNotQueryBuilder queryBuilder, Query query, SearchContext context) throws IOException { assertThat(query, instanceOf(SpanNotQuery.class)); SpanNotQuery spanNotQuery = (SpanNotQuery) query; assertThat(spanNotQuery.getExclude(), equalTo(queryBuilder.excludeQuery().toQuery(context.getQueryShardContext()))); assertThat(spanNotQuery.getInclude(), equalTo(queryBuilder.includeQuery().toQuery(context.getQueryShardContext()))); }
private Query spanFilter(SpanQuery query) { if (query instanceof SpanNearQuery) { return spanNearFilter((SpanNearQuery) query); } else if (query instanceof SpanNotQuery) { return spanNotFilter((SpanNotQuery) query); } else if (query instanceof SpanOrQuery) { return spanOrFilter((SpanOrQuery) query); } else if (query instanceof SpanTermQuery) { return new TermQuery(((SpanTermQuery) query).getTerm()); } else if (query instanceof SpanMultiTermQueryWrapper) { return ((SpanMultiTermQueryWrapper) query).getWrappedQuery(); } else { return new QueryWrapperFilter(query); } }
public void testSpanNotQuery() throws Exception { SpanNearQuery quick_fox = new SpanNearQuery(new SpanQuery[]{quick, fox}, 1, true); assertBothFoxes(quick_fox); dumpSpans(quick_fox); SpanNotQuery quick_fox_dog = new SpanNotQuery(quick_fox, dog); assertBothFoxes(quick_fox_dog); dumpSpans(quick_fox_dog); SpanNotQuery no_quick_red_fox = new SpanNotQuery(quick_fox, red); assertOnlyBrownFox(no_quick_red_fox); dumpSpans(no_quick_red_fox); }
static Function<Query, Result> spanNotQuery() { return query -> { Result result = analyze(((SpanNotQuery) query).getInclude()); return new Result(false, result.terms); }; }
public void testDuelSpecificQueries() throws Exception { List<ParseContext.Document> documents = new ArrayList<>(); CommonTermsQuery commonTermsQuery = new CommonTermsQuery(BooleanClause.Occur.SHOULD, BooleanClause.Occur.SHOULD, 128); commonTermsQuery.add(new Term("field", "quick")); commonTermsQuery.add(new Term("field", "brown")); commonTermsQuery.add(new Term("field", "fox")); addQuery(commonTermsQuery, documents); BlendedTermQuery blendedTermQuery = BlendedTermQuery.booleanBlendedQuery(new Term[]{new Term("field", "quick"), new Term("field", "brown"), new Term("field", "fox")}, false); addQuery(blendedTermQuery, documents); SpanNearQuery spanNearQuery = new SpanNearQuery.Builder("field", true) .addClause(new SpanTermQuery(new Term("field", "quick"))) .addClause(new SpanTermQuery(new Term("field", "brown"))) .addClause(new SpanTermQuery(new Term("field", "fox"))) .build(); addQuery(spanNearQuery, documents); SpanNearQuery spanNearQuery2 = new SpanNearQuery.Builder("field", true) .addClause(new SpanTermQuery(new Term("field", "the"))) .addClause(new SpanTermQuery(new Term("field", "lazy"))) .addClause(new SpanTermQuery(new Term("field", "doc"))) .build(); SpanOrQuery spanOrQuery = new SpanOrQuery( spanNearQuery, spanNearQuery2 ); addQuery(spanOrQuery, documents); SpanNotQuery spanNotQuery = new SpanNotQuery(spanNearQuery, spanNearQuery); addQuery(spanNotQuery, documents); long lowerLong = randomIntBetween(0, 256); long upperLong = lowerLong + randomIntBetween(0, 32); addQuery(LongPoint.newRangeQuery("long_field", lowerLong, upperLong), documents); indexWriter.addDocuments(documents); indexWriter.close(); directoryReader = DirectoryReader.open(directory); IndexSearcher shardSearcher = newSearcher(directoryReader); // Disable query cache, because ControlQuery cannot be cached... shardSearcher.setQueryCache(null); Document document = new Document(); document.add(new TextField("field", "the quick brown fox jumps over the lazy dog", Field.Store.NO)); long randomLong = randomIntBetween((int) lowerLong, (int) upperLong); document.add(new LongPoint("long_field", randomLong)); MemoryIndex memoryIndex = MemoryIndex.fromDocument(document, new WhitespaceAnalyzer()); duelRun(queryStore, memoryIndex, shardSearcher); }
private void addComplexPhraseClause(List<SpanQuery> spanClauses, BooleanQuery qc) { ArrayList<SpanQuery> ors = new ArrayList<>(); ArrayList<SpanQuery> nots = new ArrayList<>(); BooleanClause[] bclauses = qc.getClauses(); // For all clauses e.g. one* two~ for (int i = 0; i < bclauses.length; i++) { Query childQuery = bclauses[i].getQuery(); // select the list to which we will add these options ArrayList<SpanQuery> chosenList = ors; if (bclauses[i].getOccur() == BooleanClause.Occur.MUST_NOT) { chosenList = nots; } if (childQuery instanceof TermQuery) { TermQuery tq = (TermQuery) childQuery; SpanTermQuery stq = new SpanTermQuery(tq.getTerm()); stq.setBoost(tq.getBoost()); chosenList.add(stq); } else if (childQuery instanceof BooleanQuery) { BooleanQuery cbq = (BooleanQuery) childQuery; addComplexPhraseClause(chosenList, cbq); } else { // TODO alternatively could call extract terms here? throw new IllegalArgumentException("Unknown query type:" + childQuery.getClass().getName()); } } if (ors.size() == 0) { return; } SpanOrQuery soq = new SpanOrQuery(ors .toArray(new SpanQuery[ors.size()])); if (nots.size() == 0) { spanClauses.add(soq); } else { SpanOrQuery snqs = new SpanOrQuery(nots .toArray(new SpanQuery[nots.size()])); SpanNotQuery snq = new SpanNotQuery(soq, snqs); spanClauses.add(snq); } }
private Query spanNotFilter(SpanNotQuery query) { return spanFilter(query.getInclude()); }
public void testSpanNot() throws Exception { Directory dir = newDirectory(); // use simpleanalyzer for more natural tokenization (else "test." is a token) final Analyzer analyzer = new MockAnalyzer(random(), MockTokenizer.SIMPLE, true); IndexWriterConfig iwc = newIndexWriterConfig(analyzer); iwc.setMergePolicy(newLogMergePolicy()); RandomIndexWriter iw = new RandomIndexWriter(random(), dir, iwc); FieldType offsetsType = new FieldType(TextField.TYPE_STORED); offsetsType.setIndexOptions(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS); Field body = new Field("body", "", offsetsType); Document doc = new Document(); doc.add(body); body.setStringValue("This is a test."); iw.addDocument(doc); body.setStringValue("Test a one sentence document."); iw.addDocument(doc); IndexReader ir = iw.getReader(); iw.close(); IndexSearcher searcher = newSearcher(ir); PostingsHighlighter highlighter = new PostingsHighlighter() { @Override protected Analyzer getIndexAnalyzer(String field) { return analyzer; } }; SpanQuery include = new SpanMultiTermQueryWrapper<>(new WildcardQuery(new Term("body", "te*"))); SpanQuery exclude = new SpanTermQuery(new Term("body", "bogus")); Query query = new SpanNotQuery(include, exclude); TopDocs topDocs = searcher.search(query, null, 10, Sort.INDEXORDER); assertEquals(2, topDocs.totalHits); String snippets[] = highlighter.highlight("body", query, searcher, topDocs); assertEquals(2, snippets.length); assertEquals("This is a <b>test</b>.", snippets[0]); assertEquals("<b>Test</b> a one sentence document.", snippets[1]); ir.close(); dir.close(); }
/** MACRO for SpanNotQuery */ public SpanNotQuery snot(SpanQuery i, SpanQuery e) { return new SpanNotQuery(i,e); }
private void addComplexPhraseClause(List<SpanQuery> spanClauses, BooleanQuery qc) { ArrayList<SpanQuery> ors = new ArrayList<SpanQuery>(); ArrayList<SpanQuery> nots = new ArrayList<SpanQuery>(); BooleanClause[] bclauses = qc.getClauses(); // For all clauses e.g. one* two~ for (int i = 0; i < bclauses.length; i++) { Query childQuery = bclauses[i].getQuery(); // select the list to which we will add these options ArrayList<SpanQuery> chosenList = ors; if (bclauses[i].getOccur() == BooleanClause.Occur.MUST_NOT) { chosenList = nots; } if (childQuery instanceof TermQuery) { TermQuery tq = (TermQuery) childQuery; SpanTermQuery stq = new SpanTermQuery(tq.getTerm()); stq.setBoost(tq.getBoost()); chosenList.add(stq); } else if (childQuery instanceof BooleanQuery) { BooleanQuery cbq = (BooleanQuery) childQuery; addComplexPhraseClause(chosenList, cbq); } else { // TODO alternatively could call extract terms here? throw new IllegalArgumentException("Unknown query type:" + childQuery.getClass().getName()); } } if (ors.size() == 0) { return; } SpanOrQuery soq = new SpanOrQuery(ors .toArray(new SpanQuery[ors.size()])); if (nots.size() == 0) { spanClauses.add(soq); } else { SpanOrQuery snqs = new SpanOrQuery(nots .toArray(new SpanQuery[nots.size()])); SpanNotQuery snq = new SpanNotQuery(soq, snqs); spanClauses.add(snq); } }
/** * Traverse a span-based NOT query. * * @param nq The query to traverse */ protected void traverse(SpanNotQuery nq) { traverseQuery(nq.getInclude()); traverseQuery(nq.getExclude()); }