Search in sources :

Example 16 with Result

use of org.elasticsearch.percolator.QueryAnalyzer.Result in project elasticsearch by elastic.

the class QueryAnalyzerTests method testExactMatch_booleanQuery.

public void testExactMatch_booleanQuery() {
    BooleanQuery.Builder builder = new BooleanQuery.Builder();
    TermQuery termQuery1 = new TermQuery(new Term("_field", "_term1"));
    builder.add(termQuery1, BooleanClause.Occur.SHOULD);
    TermQuery termQuery2 = new TermQuery(new Term("_field", "_term2"));
    builder.add(termQuery2, BooleanClause.Occur.SHOULD);
    Result result = analyze(builder.build());
    assertThat("All clauses are exact, so candidate matches are verified", result.verified, is(true));
    builder = new BooleanQuery.Builder();
    builder.add(termQuery1, BooleanClause.Occur.SHOULD);
    PhraseQuery phraseQuery1 = new PhraseQuery("_field", "_term1", "_term2");
    builder.add(phraseQuery1, BooleanClause.Occur.SHOULD);
    result = analyze(builder.build());
    assertThat("Clause isn't exact, so candidate matches are not verified", result.verified, is(false));
    builder = new BooleanQuery.Builder();
    builder.add(phraseQuery1, BooleanClause.Occur.SHOULD);
    PhraseQuery phraseQuery2 = new PhraseQuery("_field", "_term3", "_term4");
    builder.add(phraseQuery2, BooleanClause.Occur.SHOULD);
    result = analyze(builder.build());
    assertThat("No clause is exact, so candidate matches are not verified", result.verified, is(false));
    builder = new BooleanQuery.Builder();
    builder.add(termQuery1, BooleanClause.Occur.MUST_NOT);
    builder.add(termQuery2, BooleanClause.Occur.SHOULD);
    result = analyze(builder.build());
    assertThat("There is a must_not clause, so candidate matches are not verified", result.verified, is(false));
    builder = new BooleanQuery.Builder();
    builder.setMinimumNumberShouldMatch(randomIntBetween(2, 32));
    builder.add(termQuery1, BooleanClause.Occur.SHOULD);
    builder.add(termQuery2, BooleanClause.Occur.SHOULD);
    result = analyze(builder.build());
    assertThat("Minimum match is >= 1, so candidate matches are not verified", result.verified, is(false));
    builder = new BooleanQuery.Builder();
    builder.add(termQuery1, randomBoolean() ? BooleanClause.Occur.MUST : BooleanClause.Occur.FILTER);
    result = analyze(builder.build());
    assertThat("Single required clause, so candidate matches are verified", result.verified, is(false));
    builder = new BooleanQuery.Builder();
    builder.add(termQuery1, randomBoolean() ? BooleanClause.Occur.MUST : BooleanClause.Occur.FILTER);
    builder.add(termQuery2, randomBoolean() ? BooleanClause.Occur.MUST : BooleanClause.Occur.FILTER);
    result = analyze(builder.build());
    assertThat("Two or more required clauses, so candidate matches are not verified", result.verified, is(false));
    builder = new BooleanQuery.Builder();
    builder.add(termQuery1, randomBoolean() ? BooleanClause.Occur.MUST : BooleanClause.Occur.FILTER);
    builder.add(termQuery2, BooleanClause.Occur.MUST_NOT);
    result = analyze(builder.build());
    assertThat("Required and prohibited clauses, so candidate matches are not verified", result.verified, is(false));
}
Also used : BooleanQuery(org.apache.lucene.search.BooleanQuery) SpanTermQuery(org.apache.lucene.search.spans.SpanTermQuery) BlendedTermQuery(org.apache.lucene.queries.BlendedTermQuery) TermQuery(org.apache.lucene.search.TermQuery) PhraseQuery(org.apache.lucene.search.PhraseQuery) MultiPhraseQuery(org.apache.lucene.search.MultiPhraseQuery) Term(org.apache.lucene.index.Term) QueryAnalyzer.selectTermListWithTheLongestShortestTerm(org.elasticsearch.percolator.QueryAnalyzer.selectTermListWithTheLongestShortestTerm) Result(org.elasticsearch.percolator.QueryAnalyzer.Result)

Example 17 with Result

use of org.elasticsearch.percolator.QueryAnalyzer.Result in project elasticsearch by elastic.

the class QueryAnalyzerTests method testExtractQueryMetadata_spanTermQuery.

public void testExtractQueryMetadata_spanTermQuery() {
    // the following span queries aren't exposed in the query dsl and are therefor not supported:
    // 1) SpanPositionRangeQuery
    // 2) PayloadScoreQuery
    // 3) SpanBoostQuery
    // The following span queries can't be supported because of how these queries work:
    // 1) SpanMultiTermQueryWrapper, not supported, because there is no support for MTQ typed queries yet.
    // 2) SpanContainingQuery, is kind of range of spans and we don't know what is between the little and big terms
    // 3) SpanWithinQuery, same reason as SpanContainingQuery
    // 4) FieldMaskingSpanQuery is a tricky query so we shouldn't optimize this
    SpanTermQuery spanTermQuery1 = new SpanTermQuery(new Term("_field", "_short_term"));
    Result result = analyze(spanTermQuery1);
    assertThat(result.verified, is(true));
    assertTermsEqual(result.terms, spanTermQuery1.getTerm());
}
Also used : SpanTermQuery(org.apache.lucene.search.spans.SpanTermQuery) Term(org.apache.lucene.index.Term) QueryAnalyzer.selectTermListWithTheLongestShortestTerm(org.elasticsearch.percolator.QueryAnalyzer.selectTermListWithTheLongestShortestTerm) Result(org.elasticsearch.percolator.QueryAnalyzer.Result)

Example 18 with Result

use of org.elasticsearch.percolator.QueryAnalyzer.Result in project elasticsearch by elastic.

the class QueryAnalyzerTests method testExtractQueryMetadata_termQuery.

public void testExtractQueryMetadata_termQuery() {
    TermQuery termQuery = new TermQuery(new Term("_field", "_term"));
    Result result = analyze(termQuery);
    assertThat(result.verified, is(true));
    List<Term> terms = new ArrayList<>(result.terms);
    assertThat(terms.size(), equalTo(1));
    assertThat(terms.get(0).field(), equalTo(termQuery.getTerm().field()));
    assertThat(terms.get(0).bytes(), equalTo(termQuery.getTerm().bytes()));
}
Also used : SpanTermQuery(org.apache.lucene.search.spans.SpanTermQuery) BlendedTermQuery(org.apache.lucene.queries.BlendedTermQuery) TermQuery(org.apache.lucene.search.TermQuery) ArrayList(java.util.ArrayList) Term(org.apache.lucene.index.Term) QueryAnalyzer.selectTermListWithTheLongestShortestTerm(org.elasticsearch.percolator.QueryAnalyzer.selectTermListWithTheLongestShortestTerm) Result(org.elasticsearch.percolator.QueryAnalyzer.Result)

Example 19 with Result

use of org.elasticsearch.percolator.QueryAnalyzer.Result in project elasticsearch by elastic.

the class QueryAnalyzerTests method testExtractQueryMetadata_constantScoreQuery.

public void testExtractQueryMetadata_constantScoreQuery() {
    TermQuery termQuery1 = new TermQuery(new Term("_field", "_term"));
    ConstantScoreQuery constantScoreQuery = new ConstantScoreQuery(termQuery1);
    Result result = analyze(constantScoreQuery);
    assertThat(result.verified, is(true));
    List<Term> terms = new ArrayList<>(result.terms);
    assertThat(terms.size(), equalTo(1));
    assertThat(terms.get(0).field(), equalTo(termQuery1.getTerm().field()));
    assertThat(terms.get(0).bytes(), equalTo(termQuery1.getTerm().bytes()));
}
Also used : SpanTermQuery(org.apache.lucene.search.spans.SpanTermQuery) BlendedTermQuery(org.apache.lucene.queries.BlendedTermQuery) TermQuery(org.apache.lucene.search.TermQuery) ArrayList(java.util.ArrayList) ConstantScoreQuery(org.apache.lucene.search.ConstantScoreQuery) Term(org.apache.lucene.index.Term) QueryAnalyzer.selectTermListWithTheLongestShortestTerm(org.elasticsearch.percolator.QueryAnalyzer.selectTermListWithTheLongestShortestTerm) Result(org.elasticsearch.percolator.QueryAnalyzer.Result)

Example 20 with Result

use of org.elasticsearch.percolator.QueryAnalyzer.Result in project elasticsearch by elastic.

the class QueryAnalyzerTests method testExtractQueryMetadata_disjunctionMaxQuery.

public void testExtractQueryMetadata_disjunctionMaxQuery() {
    TermQuery termQuery1 = new TermQuery(new Term("_field", "_term1"));
    TermQuery termQuery2 = new TermQuery(new Term("_field", "_term2"));
    TermQuery termQuery3 = new TermQuery(new Term("_field", "_term3"));
    TermQuery termQuery4 = new TermQuery(new Term("_field", "_term4"));
    DisjunctionMaxQuery disjunctionMaxQuery = new DisjunctionMaxQuery(Arrays.asList(termQuery1, termQuery2, termQuery3, termQuery4), 0.1f);
    Result result = analyze(disjunctionMaxQuery);
    assertThat(result.verified, is(true));
    List<Term> terms = new ArrayList<>(result.terms);
    Collections.sort(terms);
    assertThat(terms.size(), equalTo(4));
    assertThat(terms.get(0).field(), equalTo(termQuery1.getTerm().field()));
    assertThat(terms.get(0).bytes(), equalTo(termQuery1.getTerm().bytes()));
    assertThat(terms.get(1).field(), equalTo(termQuery2.getTerm().field()));
    assertThat(terms.get(1).bytes(), equalTo(termQuery2.getTerm().bytes()));
    assertThat(terms.get(2).field(), equalTo(termQuery3.getTerm().field()));
    assertThat(terms.get(2).bytes(), equalTo(termQuery3.getTerm().bytes()));
    assertThat(terms.get(3).field(), equalTo(termQuery4.getTerm().field()));
    assertThat(terms.get(3).bytes(), equalTo(termQuery4.getTerm().bytes()));
    disjunctionMaxQuery = new DisjunctionMaxQuery(Arrays.asList(termQuery1, termQuery2, termQuery3, new PhraseQuery("_field", "_term4")), 0.1f);
    result = analyze(disjunctionMaxQuery);
    assertThat(result.verified, is(false));
    terms = new ArrayList<>(result.terms);
    Collections.sort(terms);
    assertThat(terms.size(), equalTo(4));
    assertThat(terms.get(0).field(), equalTo(termQuery1.getTerm().field()));
    assertThat(terms.get(0).bytes(), equalTo(termQuery1.getTerm().bytes()));
    assertThat(terms.get(1).field(), equalTo(termQuery2.getTerm().field()));
    assertThat(terms.get(1).bytes(), equalTo(termQuery2.getTerm().bytes()));
    assertThat(terms.get(2).field(), equalTo(termQuery3.getTerm().field()));
    assertThat(terms.get(2).bytes(), equalTo(termQuery3.getTerm().bytes()));
    assertThat(terms.get(3).field(), equalTo(termQuery4.getTerm().field()));
    assertThat(terms.get(3).bytes(), equalTo(termQuery4.getTerm().bytes()));
}
Also used : SpanTermQuery(org.apache.lucene.search.spans.SpanTermQuery) BlendedTermQuery(org.apache.lucene.queries.BlendedTermQuery) TermQuery(org.apache.lucene.search.TermQuery) PhraseQuery(org.apache.lucene.search.PhraseQuery) MultiPhraseQuery(org.apache.lucene.search.MultiPhraseQuery) DisjunctionMaxQuery(org.apache.lucene.search.DisjunctionMaxQuery) ArrayList(java.util.ArrayList) Term(org.apache.lucene.index.Term) QueryAnalyzer.selectTermListWithTheLongestShortestTerm(org.elasticsearch.percolator.QueryAnalyzer.selectTermListWithTheLongestShortestTerm) Result(org.elasticsearch.percolator.QueryAnalyzer.Result)

Aggregations

Term (org.apache.lucene.index.Term)23 Result (org.elasticsearch.percolator.QueryAnalyzer.Result)23 QueryAnalyzer.selectTermListWithTheLongestShortestTerm (org.elasticsearch.percolator.QueryAnalyzer.selectTermListWithTheLongestShortestTerm)23 SpanTermQuery (org.apache.lucene.search.spans.SpanTermQuery)17 BlendedTermQuery (org.apache.lucene.queries.BlendedTermQuery)13 ArrayList (java.util.ArrayList)12 TermQuery (org.apache.lucene.search.TermQuery)12 BooleanQuery (org.apache.lucene.search.BooleanQuery)7 MultiPhraseQuery (org.apache.lucene.search.MultiPhraseQuery)6 PhraseQuery (org.apache.lucene.search.PhraseQuery)5 DisjunctionMaxQuery (org.apache.lucene.search.DisjunctionMaxQuery)2 CommonTermsQuery (org.apache.lucene.queries.CommonTermsQuery)1 BoostQuery (org.apache.lucene.search.BoostQuery)1 ConstantScoreQuery (org.apache.lucene.search.ConstantScoreQuery)1 MatchAllDocsQuery (org.apache.lucene.search.MatchAllDocsQuery)1 MatchNoDocsQuery (org.apache.lucene.search.MatchNoDocsQuery)1 SynonymQuery (org.apache.lucene.search.SynonymQuery)1 TermInSetQuery (org.apache.lucene.search.TermInSetQuery)1 TermRangeQuery (org.apache.lucene.search.TermRangeQuery)1 SpanFirstQuery (org.apache.lucene.search.spans.SpanFirstQuery)1