Search in sources :

Example 11 with TermInSetQuery

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

the class HasChildQueryBuilderTests method assertLateParsingQuery.

static void assertLateParsingQuery(Query query, String type, String id) throws IOException {
    assertThat(query, instanceOf(HasChildQueryBuilder.LateParsingQuery.class));
    HasChildQueryBuilder.LateParsingQuery lateParsingQuery = (HasChildQueryBuilder.LateParsingQuery) query;
    assertThat(lateParsingQuery.getInnerQuery(), instanceOf(BooleanQuery.class));
    BooleanQuery booleanQuery = (BooleanQuery) lateParsingQuery.getInnerQuery();
    assertThat(booleanQuery.clauses().size(), equalTo(2));
    //check the inner ids query, we have to call rewrite to get to check the type it's executed against
    assertThat(booleanQuery.clauses().get(0).getOccur(), equalTo(BooleanClause.Occur.MUST));
    assertThat(booleanQuery.clauses().get(0).getQuery(), instanceOf(TermInSetQuery.class));
    TermInSetQuery termsQuery = (TermInSetQuery) booleanQuery.clauses().get(0).getQuery();
    Query rewrittenTermsQuery = termsQuery.rewrite(null);
    assertThat(rewrittenTermsQuery, instanceOf(ConstantScoreQuery.class));
    ConstantScoreQuery constantScoreQuery = (ConstantScoreQuery) rewrittenTermsQuery;
    assertThat(constantScoreQuery.getQuery(), instanceOf(BooleanQuery.class));
    BooleanQuery booleanTermsQuery = (BooleanQuery) constantScoreQuery.getQuery();
    assertThat(booleanTermsQuery.clauses().toString(), booleanTermsQuery.clauses().size(), equalTo(1));
    assertThat(booleanTermsQuery.clauses().get(0).getOccur(), equalTo(BooleanClause.Occur.SHOULD));
    assertThat(booleanTermsQuery.clauses().get(0).getQuery(), instanceOf(TermQuery.class));
    TermQuery termQuery = (TermQuery) booleanTermsQuery.clauses().get(0).getQuery();
    assertThat(termQuery.getTerm().field(), equalTo(UidFieldMapper.NAME));
    //we want to make sure that the inner ids query gets executed against the child type rather than the main type we initially set to the context
    BytesRef[] ids = Uid.createUidsForTypesAndIds(Collections.singletonList(type), Collections.singletonList(id));
    assertThat(termQuery.getTerm().bytes(), equalTo(ids[0]));
    //check the type filter
    assertThat(booleanQuery.clauses().get(1).getOccur(), equalTo(BooleanClause.Occur.FILTER));
    assertEquals(new TypeFieldMapper.TypesQuery(new BytesRef(type)), booleanQuery.clauses().get(1).getQuery());
}
Also used : BooleanQuery(org.apache.lucene.search.BooleanQuery) TermQuery(org.apache.lucene.search.TermQuery) TypeFieldMapper(org.elasticsearch.index.mapper.TypeFieldMapper) Query(org.apache.lucene.search.Query) MatchNoDocsQuery(org.apache.lucene.search.MatchNoDocsQuery) ConstantScoreQuery(org.apache.lucene.search.ConstantScoreQuery) TermInSetQuery(org.apache.lucene.search.TermInSetQuery) TermQuery(org.apache.lucene.search.TermQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) TermInSetQuery(org.apache.lucene.search.TermInSetQuery) ConstantScoreQuery(org.apache.lucene.search.ConstantScoreQuery) BytesRef(org.apache.lucene.util.BytesRef)

Example 12 with TermInSetQuery

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

the class TextFieldTypeTests method testTermsQuery.

public void testTermsQuery() {
    MappedFieldType ft = createDefaultFieldType();
    ft.setName("field");
    ft.setIndexOptions(IndexOptions.DOCS);
    List<BytesRef> terms = new ArrayList<>();
    terms.add(new BytesRef("foo"));
    terms.add(new BytesRef("bar"));
    assertEquals(new TermInSetQuery("field", terms), ft.termsQuery(Arrays.asList("foo", "bar"), null));
    ft.setIndexOptions(IndexOptions.NONE);
    IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> ft.termsQuery(Arrays.asList("foo", "bar"), null));
    assertEquals("Cannot search on field [field] since it is not indexed.", e.getMessage());
}
Also used : TermInSetQuery(org.apache.lucene.search.TermInSetQuery) MappedFieldType(org.elasticsearch.index.mapper.MappedFieldType) ArrayList(java.util.ArrayList) BytesRef(org.apache.lucene.util.BytesRef)

Example 13 with TermInSetQuery

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

the class KeywordFieldTypeTests method testTermsQuery.

public void testTermsQuery() {
    MappedFieldType ft = createDefaultFieldType();
    ft.setName("field");
    ft.setIndexOptions(IndexOptions.DOCS);
    List<BytesRef> terms = new ArrayList<>();
    terms.add(new BytesRef("foo"));
    terms.add(new BytesRef("bar"));
    assertEquals(new TermInSetQuery("field", terms), ft.termsQuery(Arrays.asList("foo", "bar"), null));
    ft.setIndexOptions(IndexOptions.NONE);
    IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> ft.termsQuery(Arrays.asList("foo", "bar"), null));
    assertEquals("Cannot search on field [field] since it is not indexed.", e.getMessage());
}
Also used : TermInSetQuery(org.apache.lucene.search.TermInSetQuery) ArrayList(java.util.ArrayList) BytesRef(org.apache.lucene.util.BytesRef)

Example 14 with TermInSetQuery

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

the class TestSolrQueryParser method testAutoTerms.

// automatically use TermsQuery when appropriate
@Test
public void testAutoTerms() throws Exception {
    SolrQueryRequest req = req();
    QParser qParser;
    Query q, qq;
    Map<String, String> sowFalseParamsMap = new HashMap<>();
    sowFalseParamsMap.put("sow", "false");
    Map<String, String> sowTrueParamsMap = new HashMap<>();
    sowTrueParamsMap.put("sow", "true");
    List<MapSolrParams> paramMaps = Arrays.asList(// no sow param (i.e. the default sow value) 
    new MapSolrParams(Collections.emptyMap()), new MapSolrParams(sowFalseParamsMap), new MapSolrParams(sowTrueParamsMap));
    for (MapSolrParams params : paramMaps) {
        // relevance query should not be a filter
        qParser = QParser.getParser("foo_s:(a b c)", req);
        qParser.setParams(params);
        q = qParser.getQuery();
        assertEquals(3, ((BooleanQuery) q).clauses().size());
        // small filter query should still use BooleanQuery
        if (QueryParser.TERMS_QUERY_THRESHOLD > 3) {
            qParser = QParser.getParser("foo_s:(a b c)", req);
            qParser.setParams(params);
            // this may change in the future
            qParser.setIsFilter(true);
            q = qParser.getQuery();
            assertEquals(3, ((BooleanQuery) q).clauses().size());
        }
        // large relevancy query should use BooleanQuery
        // TODO: we may decide that string fields shouldn't have relevance in the future... change to a text field w/o a stop filter if so
        qParser = QParser.getParser("foo_s:(a b c d e f g h i j k l m n o p q r s t u v w x y z)", req);
        qParser.setParams(params);
        q = qParser.getQuery();
        assertEquals(26, ((BooleanQuery) q).clauses().size());
        // large filter query should use TermsQuery
        qParser = QParser.getParser("foo_s:(a b c d e f g h i j k l m n o p q r s t u v w x y z)", req);
        // this may change in the future
        qParser.setIsFilter(true);
        qParser.setParams(params);
        q = qParser.getQuery();
        assertEquals(26, ((TermInSetQuery) q).getTermData().size());
        // large numeric filter query should use TermsQuery (for trie fields)
        qParser = QParser.getParser("foo_ti:(1 2 3 4 5 6 7 8 9 10 20 19 18 17 16 15 14 13 12 11)", req);
        // this may change in the future
        qParser.setIsFilter(true);
        qParser.setParams(params);
        q = qParser.getQuery();
        assertEquals(20, ((TermInSetQuery) q).getTermData().size());
        // for point fields large filter query should use PointInSetQuery
        qParser = QParser.getParser("foo_pi:(1 2 3 4 5 6 7 8 9 10 20 19 18 17 16 15 14 13 12 11)", req);
        // this may change in the future
        qParser.setIsFilter(true);
        qParser.setParams(params);
        q = qParser.getQuery();
        assertTrue(q instanceof PointInSetQuery);
        assertEquals(20, ((PointInSetQuery) q).getPackedPoints().size());
        // a filter() clause inside a relevancy query should be able to use a TermsQuery
        qParser = QParser.getParser("foo_s:aaa filter(foo_s:(a b c d e f g h i j k l m n o p q r s t u v w x y z))", req);
        qParser.setParams(params);
        q = qParser.getQuery();
        assertEquals(2, ((BooleanQuery) q).clauses().size());
        qq = ((BooleanQuery) q).clauses().get(0).getQuery();
        if (qq instanceof TermQuery) {
            qq = ((BooleanQuery) q).clauses().get(1).getQuery();
        }
        if (qq instanceof FilterQuery) {
            qq = ((FilterQuery) qq).getQuery();
        }
        assertEquals(26, ((TermInSetQuery) qq).getTermData().size());
        // test mixed boolean query, including quotes (which shouldn't matter)
        qParser = QParser.getParser("foo_s:(a +aaa b -bbb c d e f bar_s:(qqq www) g h i j k l m n o p q r s t u v w x y z)", req);
        // this may change in the future
        qParser.setIsFilter(true);
        qParser.setParams(params);
        q = qParser.getQuery();
        assertEquals(4, ((BooleanQuery) q).clauses().size());
        qq = null;
        for (BooleanClause clause : ((BooleanQuery) q).clauses()) {
            qq = clause.getQuery();
            if (qq instanceof TermInSetQuery)
                break;
        }
        assertEquals(26, ((TermInSetQuery) qq).getTermData().size());
        // test terms queries of two different fields (LUCENE-7637 changed to require all terms be in the same field)
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < 17; i++) {
            char letter = (char) ('a' + i);
            sb.append("foo_s:" + letter + " bar_s:" + letter + " ");
        }
        qParser = QParser.getParser(sb.toString(), req);
        // this may change in the future
        qParser.setIsFilter(true);
        qParser.setParams(params);
        q = qParser.getQuery();
        assertEquals(2, ((BooleanQuery) q).clauses().size());
        for (BooleanClause clause : ((BooleanQuery) q).clauses()) {
            qq = clause.getQuery();
            assertEquals(17, ((TermInSetQuery) qq).getTermData().size());
        }
    }
    req.close();
}
Also used : BooleanQuery(org.apache.lucene.search.BooleanQuery) TermQuery(org.apache.lucene.search.TermQuery) Query(org.apache.lucene.search.Query) PointInSetQuery(org.apache.lucene.search.PointInSetQuery) ConstantScoreQuery(org.apache.lucene.search.ConstantScoreQuery) TermInSetQuery(org.apache.lucene.search.TermInSetQuery) TermQuery(org.apache.lucene.search.TermQuery) FilterQuery(org.apache.solr.query.FilterQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) BoostQuery(org.apache.lucene.search.BoostQuery) HashMap(java.util.HashMap) PointInSetQuery(org.apache.lucene.search.PointInSetQuery) FilterQuery(org.apache.solr.query.FilterQuery) BooleanClause(org.apache.lucene.search.BooleanClause) SolrQueryRequest(org.apache.solr.request.SolrQueryRequest) MapSolrParams(org.apache.solr.common.params.MapSolrParams) TermInSetQuery(org.apache.lucene.search.TermInSetQuery) Test(org.junit.Test)

Example 15 with TermInSetQuery

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

the class ExpandComponent method getGroupQuery.

private Query getGroupQuery(String fname, int size, IntObjectHashMap<BytesRef> ordBytes) throws Exception {
    BytesRef[] bytesRefs = new BytesRef[size];
    int index = -1;
    Iterator<IntObjectCursor<BytesRef>> it = ordBytes.iterator();
    while (it.hasNext()) {
        IntObjectCursor<BytesRef> cursor = it.next();
        bytesRefs[++index] = cursor.value;
    }
    return new SolrConstantScoreQuery(new QueryWrapperFilter(new TermInSetQuery(fname, bytesRefs)));
}
Also used : IntObjectCursor(com.carrotsearch.hppc.cursors.IntObjectCursor) TermInSetQuery(org.apache.lucene.search.TermInSetQuery) SolrConstantScoreQuery(org.apache.solr.search.SolrConstantScoreQuery) QueryWrapperFilter(org.apache.solr.search.QueryWrapperFilter) BytesRef(org.apache.lucene.util.BytesRef)

Aggregations

TermInSetQuery (org.apache.lucene.search.TermInSetQuery)16 BytesRef (org.apache.lucene.util.BytesRef)12 ArrayList (java.util.ArrayList)9 BooleanQuery (org.apache.lucene.search.BooleanQuery)4 Query (org.apache.lucene.search.Query)4 TermQuery (org.apache.lucene.search.TermQuery)4 Term (org.apache.lucene.index.Term)3 ConstantScoreQuery (org.apache.lucene.search.ConstantScoreQuery)3 BytesRefBuilder (org.apache.lucene.util.BytesRefBuilder)3 HashSet (java.util.HashSet)2 IndexReader (org.apache.lucene.index.IndexReader)2 MatchNoDocsQuery (org.apache.lucene.search.MatchNoDocsQuery)2 Cell (org.apache.lucene.spatial.prefix.tree.Cell)2 CellIterator (org.apache.lucene.spatial.prefix.tree.CellIterator)2 Directory (org.apache.lucene.store.Directory)2 QueryWrapperFilter (org.apache.solr.search.QueryWrapperFilter)2 Shape (org.locationtech.spatial4j.shape.Shape)2 IntObjectCursor (com.carrotsearch.hppc.cursors.IntObjectCursor)1 LongCursor (com.carrotsearch.hppc.cursors.LongCursor)1 Repeat (com.carrotsearch.randomizedtesting.annotations.Repeat)1