Search in sources :

Example 11 with Result

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

the class QueryAnalyzerTests method testExtractQueryMetadata_booleanQueryWithMustNot.

public void testExtractQueryMetadata_booleanQueryWithMustNot() {
    BooleanQuery.Builder builder = new BooleanQuery.Builder();
    TermQuery termQuery1 = new TermQuery(new Term("_field", "_term"));
    builder.add(termQuery1, BooleanClause.Occur.MUST_NOT);
    PhraseQuery phraseQuery = new PhraseQuery("_field", "_term1", "term2");
    builder.add(phraseQuery, BooleanClause.Occur.SHOULD);
    BooleanQuery booleanQuery = builder.build();
    Result result = analyze(booleanQuery);
    assertThat(result.verified, is(false));
    List<Term> terms = new ArrayList<>(result.terms);
    assertThat(terms.size(), equalTo(1));
    assertThat(terms.get(0).field(), equalTo(phraseQuery.getTerms()[0].field()));
    assertThat(terms.get(0).bytes(), equalTo(phraseQuery.getTerms()[0].bytes()));
}
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) ArrayList(java.util.ArrayList) Term(org.apache.lucene.index.Term) QueryAnalyzer.selectTermListWithTheLongestShortestTerm(org.elasticsearch.percolator.QueryAnalyzer.selectTermListWithTheLongestShortestTerm) Result(org.elasticsearch.percolator.QueryAnalyzer.Result)

Example 12 with Result

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

the class QueryAnalyzerTests method testExtractQueryMetadata_spanNotQuery.

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());
}
Also used : SpanTermQuery(org.apache.lucene.search.spans.SpanTermQuery) Term(org.apache.lucene.index.Term) QueryAnalyzer.selectTermListWithTheLongestShortestTerm(org.elasticsearch.percolator.QueryAnalyzer.selectTermListWithTheLongestShortestTerm) SpanNotQuery(org.apache.lucene.search.spans.SpanNotQuery) Result(org.elasticsearch.percolator.QueryAnalyzer.Result)

Example 13 with Result

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

the class QueryAnalyzerTests method testExtractQueryMetadata_boostQuery.

public void testExtractQueryMetadata_boostQuery() {
    TermQuery termQuery1 = new TermQuery(new Term("_field", "_term"));
    BoostQuery constantScoreQuery = new BoostQuery(termQuery1, 1f);
    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) Term(org.apache.lucene.index.Term) QueryAnalyzer.selectTermListWithTheLongestShortestTerm(org.elasticsearch.percolator.QueryAnalyzer.selectTermListWithTheLongestShortestTerm) BoostQuery(org.apache.lucene.search.BoostQuery) Result(org.elasticsearch.percolator.QueryAnalyzer.Result)

Example 14 with Result

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

the class QueryAnalyzerTests method testExtractQueryMetadata_matchAllDocsQuery.

public void testExtractQueryMetadata_matchAllDocsQuery() {
    expectThrows(UnsupportedQueryException.class, () -> analyze(new MatchAllDocsQuery()));
    BooleanQuery.Builder builder = new BooleanQuery.Builder();
    builder.add(new TermQuery(new Term("field", "value")), BooleanClause.Occur.MUST);
    builder.add(new MatchAllDocsQuery(), BooleanClause.Occur.MUST);
    Result result = analyze(builder.build());
    assertThat(result.verified, is(false));
    assertTermsEqual(result.terms, new Term("field", "value"));
    builder = new BooleanQuery.Builder();
    builder.add(new MatchAllDocsQuery(), BooleanClause.Occur.MUST);
    builder.add(new MatchAllDocsQuery(), BooleanClause.Occur.MUST);
    builder.add(new MatchAllDocsQuery(), BooleanClause.Occur.MUST);
    BooleanQuery bq1 = builder.build();
    expectThrows(UnsupportedQueryException.class, () -> analyze(bq1));
    builder = new BooleanQuery.Builder();
    builder.add(new MatchAllDocsQuery(), BooleanClause.Occur.MUST_NOT);
    builder.add(new MatchAllDocsQuery(), BooleanClause.Occur.MUST);
    builder.add(new MatchAllDocsQuery(), BooleanClause.Occur.MUST);
    BooleanQuery bq2 = builder.build();
    expectThrows(UnsupportedQueryException.class, () -> analyze(bq2));
    builder = new BooleanQuery.Builder();
    builder.add(new MatchAllDocsQuery(), BooleanClause.Occur.SHOULD);
    builder.add(new MatchAllDocsQuery(), BooleanClause.Occur.SHOULD);
    builder.add(new MatchAllDocsQuery(), BooleanClause.Occur.SHOULD);
    BooleanQuery bq3 = builder.build();
    expectThrows(UnsupportedQueryException.class, () -> analyze(bq3));
    builder = new BooleanQuery.Builder();
    builder.add(new MatchAllDocsQuery(), BooleanClause.Occur.MUST_NOT);
    builder.add(new MatchAllDocsQuery(), BooleanClause.Occur.SHOULD);
    builder.add(new MatchAllDocsQuery(), BooleanClause.Occur.SHOULD);
    BooleanQuery bq4 = builder.build();
    expectThrows(UnsupportedQueryException.class, () -> analyze(bq4));
    builder = new BooleanQuery.Builder();
    builder.add(new TermQuery(new Term("field", "value")), BooleanClause.Occur.SHOULD);
    builder.add(new MatchAllDocsQuery(), BooleanClause.Occur.SHOULD);
    BooleanQuery bq5 = builder.build();
    expectThrows(UnsupportedQueryException.class, () -> analyze(bq5));
}
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) Term(org.apache.lucene.index.Term) QueryAnalyzer.selectTermListWithTheLongestShortestTerm(org.elasticsearch.percolator.QueryAnalyzer.selectTermListWithTheLongestShortestTerm) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) Result(org.elasticsearch.percolator.QueryAnalyzer.Result)

Example 15 with Result

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

the class QueryAnalyzerTests method testExtractQueryMetadata_booleanQuery_onlyShould.

public void testExtractQueryMetadata_booleanQuery_onlyShould() {
    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);
    BooleanQuery.Builder subBuilder = new BooleanQuery.Builder();
    TermQuery termQuery3 = new TermQuery(new Term("_field1", "_term"));
    subBuilder.add(termQuery3, BooleanClause.Occur.SHOULD);
    TermQuery termQuery4 = new TermQuery(new Term("_field3", "_long_term"));
    subBuilder.add(termQuery4, BooleanClause.Occur.SHOULD);
    builder.add(subBuilder.build(), BooleanClause.Occur.SHOULD);
    BooleanQuery booleanQuery = builder.build();
    Result result = analyze(booleanQuery);
    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()));
}
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) 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