Search in sources :

Example 1 with FilterQuery

use of org.apache.solr.query.FilterQuery 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)

Aggregations

HashMap (java.util.HashMap)1 BooleanClause (org.apache.lucene.search.BooleanClause)1 BooleanQuery (org.apache.lucene.search.BooleanQuery)1 BoostQuery (org.apache.lucene.search.BoostQuery)1 ConstantScoreQuery (org.apache.lucene.search.ConstantScoreQuery)1 PointInSetQuery (org.apache.lucene.search.PointInSetQuery)1 Query (org.apache.lucene.search.Query)1 TermInSetQuery (org.apache.lucene.search.TermInSetQuery)1 TermQuery (org.apache.lucene.search.TermQuery)1 MapSolrParams (org.apache.solr.common.params.MapSolrParams)1 FilterQuery (org.apache.solr.query.FilterQuery)1 SolrQueryRequest (org.apache.solr.request.SolrQueryRequest)1 Test (org.junit.Test)1