Search in sources :

Example 1 with DisjunctionMaxQuery

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

the class BlendedTermQueryTests method testDismaxQuery.

public void testDismaxQuery() throws IOException {
    Directory dir = newDirectory();
    IndexWriter w = new IndexWriter(dir, newIndexWriterConfig(new MockAnalyzer(random())));
    String[] username = new String[] { "foo fighters", "some cool fan", "cover band" };
    String[] song = new String[] { "generator", "foo fighers - generator", "foo fighters generator" };
    final boolean omitNorms = random().nextBoolean();
    FieldType ft = new FieldType(TextField.TYPE_NOT_STORED);
    ft.setIndexOptions(random().nextBoolean() ? IndexOptions.DOCS : IndexOptions.DOCS_AND_FREQS);
    ft.setOmitNorms(omitNorms);
    ft.freeze();
    FieldType ft1 = new FieldType(TextField.TYPE_NOT_STORED);
    ft1.setIndexOptions(random().nextBoolean() ? IndexOptions.DOCS : IndexOptions.DOCS_AND_FREQS);
    ft1.setOmitNorms(omitNorms);
    ft1.freeze();
    for (int i = 0; i < username.length; i++) {
        Document d = new Document();
        d.add(new TextField("id", Integer.toString(i), Field.Store.YES));
        d.add(new Field("username", username[i], ft));
        d.add(new Field("song", song[i], ft));
        w.addDocument(d);
    }
    int iters = scaledRandomIntBetween(25, 100);
    for (int j = 0; j < iters; j++) {
        Document d = new Document();
        d.add(new TextField("id", Integer.toString(username.length + j), Field.Store.YES));
        d.add(new Field("username", "foo fighters", ft1));
        d.add(new Field("song", "some bogus text to bump up IDF", ft1));
        w.addDocument(d);
    }
    w.commit();
    DirectoryReader reader = DirectoryReader.open(w);
    IndexSearcher searcher = setSimilarity(newSearcher(reader));
    {
        String[] fields = new String[] { "username", "song" };
        BooleanQuery.Builder query = new BooleanQuery.Builder();
        query.setDisableCoord(true);
        query.add(BlendedTermQuery.dismaxBlendedQuery(toTerms(fields, "foo"), 0.1f), BooleanClause.Occur.SHOULD);
        query.add(BlendedTermQuery.dismaxBlendedQuery(toTerms(fields, "fighters"), 0.1f), BooleanClause.Occur.SHOULD);
        query.add(BlendedTermQuery.dismaxBlendedQuery(toTerms(fields, "generator"), 0.1f), BooleanClause.Occur.SHOULD);
        TopDocs search = searcher.search(query.build(), 10);
        ScoreDoc[] scoreDocs = search.scoreDocs;
        assertEquals(Integer.toString(0), reader.document(scoreDocs[0].doc).getField("id").stringValue());
    }
    {
        BooleanQuery.Builder query = new BooleanQuery.Builder();
        query.setDisableCoord(true);
        DisjunctionMaxQuery uname = new DisjunctionMaxQuery(Arrays.asList(new TermQuery(new Term("username", "foo")), new TermQuery(new Term("song", "foo"))), 0.0f);
        DisjunctionMaxQuery s = new DisjunctionMaxQuery(Arrays.asList(new TermQuery(new Term("username", "fighers")), new TermQuery(new Term("song", "fighers"))), 0.0f);
        DisjunctionMaxQuery gen = new DisjunctionMaxQuery(Arrays.asList(new TermQuery(new Term("username", "generator")), new TermQuery(new Term("song", "generator"))), 0f);
        query.add(uname, BooleanClause.Occur.SHOULD);
        query.add(s, BooleanClause.Occur.SHOULD);
        query.add(gen, BooleanClause.Occur.SHOULD);
        TopDocs search = searcher.search(query.build(), 4);
        ScoreDoc[] scoreDocs = search.scoreDocs;
        assertEquals(Integer.toString(1), reader.document(scoreDocs[0].doc).getField("id").stringValue());
    }
    reader.close();
    w.close();
    dir.close();
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) BooleanQuery(org.apache.lucene.search.BooleanQuery) TermQuery(org.apache.lucene.search.TermQuery) DirectoryReader(org.apache.lucene.index.DirectoryReader) DisjunctionMaxQuery(org.apache.lucene.search.DisjunctionMaxQuery) Term(org.apache.lucene.index.Term) Document(org.apache.lucene.document.Document) FieldType(org.apache.lucene.document.FieldType) TopDocs(org.apache.lucene.search.TopDocs) Field(org.apache.lucene.document.Field) TextField(org.apache.lucene.document.TextField) MockAnalyzer(org.apache.lucene.analysis.MockAnalyzer) IndexWriter(org.apache.lucene.index.IndexWriter) TextField(org.apache.lucene.document.TextField) Directory(org.apache.lucene.store.Directory)

Example 2 with DisjunctionMaxQuery

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

the class DisMaxQueryBuilderTests method testToQueryInnerPrefixQuery.

public void testToQueryInnerPrefixQuery() throws Exception {
    assumeTrue("test runs only when at least a type is registered", getCurrentTypes().length > 0);
    String queryAsString = "{\n" + "    \"dis_max\":{\n" + "        \"queries\":[\n" + "            {\n" + "                \"prefix\":{\n" + "                    \"" + STRING_FIELD_NAME + "\":{\n" + "                        \"value\":\"sh\",\n" + "                        \"boost\":1.2\n" + "                    }\n" + "                }\n" + "            }\n" + "        ]\n" + "    }\n" + "}";
    Query query = parseQuery(queryAsString).toQuery(createShardContext());
    assertThat(query, instanceOf(DisjunctionMaxQuery.class));
    DisjunctionMaxQuery disjunctionMaxQuery = (DisjunctionMaxQuery) query;
    List<Query> disjuncts = disjunctionMaxQuery.getDisjuncts();
    assertThat(disjuncts.size(), equalTo(1));
    assertThat(disjuncts.get(0), instanceOf(BoostQuery.class));
    BoostQuery boostQuery = (BoostQuery) disjuncts.get(0);
    assertThat((double) boostQuery.getBoost(), closeTo(1.2, 0.00001));
    assertThat(boostQuery.getQuery(), instanceOf(PrefixQuery.class));
    PrefixQuery firstQ = (PrefixQuery) boostQuery.getQuery();
    // since age is automatically registered in data, we encode it as numeric
    assertThat(firstQ.getPrefix(), equalTo(new Term(STRING_FIELD_NAME, "sh")));
}
Also used : Query(org.apache.lucene.search.Query) PrefixQuery(org.apache.lucene.search.PrefixQuery) DisjunctionMaxQuery(org.apache.lucene.search.DisjunctionMaxQuery) BoostQuery(org.apache.lucene.search.BoostQuery) PrefixQuery(org.apache.lucene.search.PrefixQuery) DisjunctionMaxQuery(org.apache.lucene.search.DisjunctionMaxQuery) Term(org.apache.lucene.index.Term) BoostQuery(org.apache.lucene.search.BoostQuery)

Example 3 with DisjunctionMaxQuery

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

the class DisMaxQueryBuilderTests method doAssertLuceneQuery.

@Override
protected void doAssertLuceneQuery(DisMaxQueryBuilder queryBuilder, Query query, SearchContext context) throws IOException {
    Collection<Query> queries = AbstractQueryBuilder.toQueries(queryBuilder.innerQueries(), context.getQueryShardContext());
    assertThat(query, instanceOf(DisjunctionMaxQuery.class));
    DisjunctionMaxQuery disjunctionMaxQuery = (DisjunctionMaxQuery) query;
    assertThat(disjunctionMaxQuery.getTieBreakerMultiplier(), equalTo(queryBuilder.tieBreaker()));
    assertThat(disjunctionMaxQuery.getDisjuncts().size(), equalTo(queries.size()));
    Iterator<Query> queryIterator = queries.iterator();
    for (int i = 0; i < disjunctionMaxQuery.getDisjuncts().size(); i++) {
        assertThat(disjunctionMaxQuery.getDisjuncts().get(i), equalTo(queryIterator.next()));
    }
}
Also used : Query(org.apache.lucene.search.Query) PrefixQuery(org.apache.lucene.search.PrefixQuery) DisjunctionMaxQuery(org.apache.lucene.search.DisjunctionMaxQuery) BoostQuery(org.apache.lucene.search.BoostQuery) DisjunctionMaxQuery(org.apache.lucene.search.DisjunctionMaxQuery)

Example 4 with DisjunctionMaxQuery

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

the class MultiMatchQueryBuilderTests method testToQueryMultipleFieldsDisMaxQuery.

public void testToQueryMultipleFieldsDisMaxQuery() throws Exception {
    assumeTrue("test runs only when at least a type is registered", getCurrentTypes().length > 0);
    Query query = multiMatchQuery("test").field(STRING_FIELD_NAME).field(STRING_FIELD_NAME_2).useDisMax(true).toQuery(createShardContext());
    assertThat(query, instanceOf(DisjunctionMaxQuery.class));
    DisjunctionMaxQuery disMaxQuery = (DisjunctionMaxQuery) query;
    List<Query> disjuncts = disMaxQuery.getDisjuncts();
    assertThat(disjuncts.get(0), instanceOf(TermQuery.class));
    assertThat(((TermQuery) disjuncts.get(0)).getTerm(), equalTo(new Term(STRING_FIELD_NAME, "test")));
    assertThat(disjuncts.get(1), instanceOf(TermQuery.class));
    assertThat(((TermQuery) disjuncts.get(1)).getTerm(), equalTo(new Term(STRING_FIELD_NAME_2, "test")));
}
Also used : AllTermQuery(org.elasticsearch.common.lucene.all.AllTermQuery) TermQuery(org.apache.lucene.search.TermQuery) Query(org.apache.lucene.search.Query) MatchNoDocsQuery(org.apache.lucene.search.MatchNoDocsQuery) PhraseQuery(org.apache.lucene.search.PhraseQuery) MatchQuery(org.elasticsearch.index.search.MatchQuery) IndexOrDocValuesQuery(org.apache.lucene.search.IndexOrDocValuesQuery) AllTermQuery(org.elasticsearch.common.lucene.all.AllTermQuery) MultiPhrasePrefixQuery(org.elasticsearch.common.lucene.search.MultiPhrasePrefixQuery) ExtendedCommonTermsQuery(org.apache.lucene.queries.ExtendedCommonTermsQuery) LegacyNumericRangeQuery(org.apache.lucene.search.LegacyNumericRangeQuery) PointRangeQuery(org.apache.lucene.search.PointRangeQuery) FuzzyQuery(org.apache.lucene.search.FuzzyQuery) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) ElasticsearchAssertions.assertBooleanSubQuery(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertBooleanSubQuery) DisjunctionMaxQuery(org.apache.lucene.search.DisjunctionMaxQuery) TermQuery(org.apache.lucene.search.TermQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) BoostQuery(org.apache.lucene.search.BoostQuery) QueryBuilders.multiMatchQuery(org.elasticsearch.index.query.QueryBuilders.multiMatchQuery) DisjunctionMaxQuery(org.apache.lucene.search.DisjunctionMaxQuery) Term(org.apache.lucene.index.Term)

Example 5 with DisjunctionMaxQuery

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

the class QueryStringQueryBuilderTests method testToQueryPhraseQueryBoostAndSlop.

public void testToQueryPhraseQueryBoostAndSlop() throws IOException {
    assumeTrue("test runs only when at least a type is registered", getCurrentTypes().length > 0);
    QueryStringQueryBuilder queryStringQueryBuilder = new QueryStringQueryBuilder("\"test phrase\"~2").field(STRING_FIELD_NAME, 5f);
    Query query = queryStringQueryBuilder.toQuery(createShardContext());
    assertThat(query, instanceOf(DisjunctionMaxQuery.class));
    DisjunctionMaxQuery disjunctionMaxQuery = (DisjunctionMaxQuery) query;
    assertThat(disjunctionMaxQuery.getDisjuncts().size(), equalTo(1));
    assertThat(disjunctionMaxQuery.getDisjuncts().get(0), instanceOf(BoostQuery.class));
    BoostQuery boostQuery = (BoostQuery) disjunctionMaxQuery.getDisjuncts().get(0);
    assertThat(boostQuery.getBoost(), equalTo(5f));
    assertThat(boostQuery.getQuery(), instanceOf(PhraseQuery.class));
    PhraseQuery phraseQuery = (PhraseQuery) boostQuery.getQuery();
    assertThat(phraseQuery.getSlop(), Matchers.equalTo(2));
    assertThat(phraseQuery.getTerms().length, equalTo(2));
}
Also used : Query(org.apache.lucene.search.Query) MatchNoDocsQuery(org.apache.lucene.search.MatchNoDocsQuery) PhraseQuery(org.apache.lucene.search.PhraseQuery) RegexpQuery(org.apache.lucene.search.RegexpQuery) MultiTermQuery(org.apache.lucene.search.MultiTermQuery) AllTermQuery(org.elasticsearch.common.lucene.all.AllTermQuery) PrefixQuery(org.apache.lucene.search.PrefixQuery) FuzzyQuery(org.apache.lucene.search.FuzzyQuery) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) WildcardQuery(org.apache.lucene.search.WildcardQuery) ElasticsearchAssertions.assertBooleanSubQuery(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertBooleanSubQuery) DisjunctionMaxQuery(org.apache.lucene.search.DisjunctionMaxQuery) SpanTermQuery(org.apache.lucene.search.spans.SpanTermQuery) ConstantScoreQuery(org.apache.lucene.search.ConstantScoreQuery) SpanNearQuery(org.apache.lucene.search.spans.SpanNearQuery) TermQuery(org.apache.lucene.search.TermQuery) SynonymQuery(org.apache.lucene.search.SynonymQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) QueryBuilders.queryStringQuery(org.elasticsearch.index.query.QueryBuilders.queryStringQuery) BoostQuery(org.apache.lucene.search.BoostQuery) TermRangeQuery(org.apache.lucene.search.TermRangeQuery) SpanOrQuery(org.apache.lucene.search.spans.SpanOrQuery) PhraseQuery(org.apache.lucene.search.PhraseQuery) DisjunctionMaxQuery(org.apache.lucene.search.DisjunctionMaxQuery) BoostQuery(org.apache.lucene.search.BoostQuery)

Aggregations

DisjunctionMaxQuery (org.apache.lucene.search.DisjunctionMaxQuery)49 Query (org.apache.lucene.search.Query)38 BooleanQuery (org.apache.lucene.search.BooleanQuery)34 BoostQuery (org.apache.lucene.search.BoostQuery)30 TermQuery (org.apache.lucene.search.TermQuery)26 Term (org.apache.lucene.index.Term)25 ArrayList (java.util.ArrayList)22 PhraseQuery (org.apache.lucene.search.PhraseQuery)20 SpanNearQuery (org.apache.lucene.search.spans.SpanNearQuery)17 MatchNoDocsQuery (org.apache.lucene.search.MatchNoDocsQuery)16 FuzzyQuery (org.apache.lucene.search.FuzzyQuery)15 SynonymQuery (org.apache.lucene.search.SynonymQuery)15 SpanOrQuery (org.apache.lucene.search.spans.SpanOrQuery)15 BooleanClause (org.apache.lucene.search.BooleanClause)13 SpanTermQuery (org.apache.lucene.search.spans.SpanTermQuery)13 SpanQuery (org.apache.lucene.search.spans.SpanQuery)12 MultiPhraseQuery (org.apache.lucene.search.MultiPhraseQuery)10 ConstantScoreQuery (org.apache.lucene.search.ConstantScoreQuery)9 MatchAllDocsQuery (org.apache.lucene.search.MatchAllDocsQuery)9 TopDocs (org.apache.lucene.search.TopDocs)9