Search in sources :

Example 56 with PhraseQuery

use of org.apache.lucene.search.PhraseQuery in project elasticsearch by elastic.

the class PercolatorFieldMapperTests method testExtractTermsAndRanges_partial.

public void testExtractTermsAndRanges_partial() throws Exception {
    addQueryMapping();
    PhraseQuery phraseQuery = new PhraseQuery("field", "term");
    DocumentMapper documentMapper = mapperService.documentMapper(typeName);
    PercolatorFieldMapper fieldMapper = (PercolatorFieldMapper) documentMapper.mappers().getMapper(fieldName);
    ParseContext.InternalParseContext parseContext = new ParseContext.InternalParseContext(Settings.EMPTY, mapperService.documentMapperParser(), documentMapper, null, null);
    fieldMapper.processQuery(phraseQuery, parseContext);
    ParseContext.Document document = parseContext.doc();
    PercolatorFieldMapper.FieldType fieldType = (PercolatorFieldMapper.FieldType) fieldMapper.fieldType();
    assertThat(document.getFields().size(), equalTo(2));
    assertThat(document.getFields().get(0).binaryValue().utf8ToString(), equalTo("fieldterm"));
    assertThat(document.getField(fieldType.extractionResultField.name()).stringValue(), equalTo(EXTRACTION_PARTIAL));
}
Also used : PhraseQuery(org.apache.lucene.search.PhraseQuery) QueryBuilders.matchPhraseQuery(org.elasticsearch.index.query.QueryBuilders.matchPhraseQuery) DocumentMapper(org.elasticsearch.index.mapper.DocumentMapper) ParseContext(org.elasticsearch.index.mapper.ParseContext) QueryParseContext(org.elasticsearch.index.query.QueryParseContext)

Example 57 with PhraseQuery

use of org.apache.lucene.search.PhraseQuery 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 58 with PhraseQuery

use of org.apache.lucene.search.PhraseQuery 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)

Example 59 with PhraseQuery

use of org.apache.lucene.search.PhraseQuery in project elasticsearch by elastic.

the class ESToParentBlockJoinQueryTests method testRewrite.

public void testRewrite() throws IOException {
    Query q = new ESToParentBlockJoinQuery(// rewrites to a TermQuery
    new PhraseQuery("body", "term"), new QueryBitSetProducer(new TermQuery(new Term("is", "parent"))), ScoreMode.Avg, "nested");
    Query expected = new ESToParentBlockJoinQuery(new TermQuery(new Term("body", "term")), new QueryBitSetProducer(new TermQuery(new Term("is", "parent"))), ScoreMode.Avg, "nested");
    Query rewritten = q.rewrite(new MultiReader());
    assertEquals(expected, rewritten);
}
Also used : TermQuery(org.apache.lucene.search.TermQuery) Query(org.apache.lucene.search.Query) TermQuery(org.apache.lucene.search.TermQuery) PhraseQuery(org.apache.lucene.search.PhraseQuery) PhraseQuery(org.apache.lucene.search.PhraseQuery) MultiReader(org.apache.lucene.index.MultiReader) QueryBitSetProducer(org.apache.lucene.search.join.QueryBitSetProducer) Term(org.apache.lucene.index.Term)

Example 60 with PhraseQuery

use of org.apache.lucene.search.PhraseQuery in project lucene-solr by apache.

the class TestBasics method testPhrase2.

public void testPhrase2() throws Exception {
    PhraseQuery query = new PhraseQuery("field", "seventish", "sevenon");
    checkHits(query, new int[] {});
}
Also used : PhraseQuery(org.apache.lucene.search.PhraseQuery)

Aggregations

PhraseQuery (org.apache.lucene.search.PhraseQuery)105 Term (org.apache.lucene.index.Term)56 TermQuery (org.apache.lucene.search.TermQuery)43 BooleanQuery (org.apache.lucene.search.BooleanQuery)39 MultiPhraseQuery (org.apache.lucene.search.MultiPhraseQuery)37 Document (org.apache.lucene.document.Document)36 Query (org.apache.lucene.search.Query)30 Directory (org.apache.lucene.store.Directory)26 MockAnalyzer (org.apache.lucene.analysis.MockAnalyzer)25 IndexSearcher (org.apache.lucene.search.IndexSearcher)22 IndexReader (org.apache.lucene.index.IndexReader)20 Field (org.apache.lucene.document.Field)17 TextField (org.apache.lucene.document.TextField)16 TopDocs (org.apache.lucene.search.TopDocs)16 SpanTermQuery (org.apache.lucene.search.spans.SpanTermQuery)16 TokenStream (org.apache.lucene.analysis.TokenStream)15 BoostQuery (org.apache.lucene.search.BoostQuery)14 IndexWriter (org.apache.lucene.index.IndexWriter)13 ArrayList (java.util.ArrayList)11 CannedTokenStream (org.apache.lucene.analysis.CannedTokenStream)10