Search in sources :

Example 16 with FunctionQuery

use of org.apache.lucene.queries.function.FunctionQuery in project lucene-solr by apache.

the class TestTaxonomyFacetSumValueSource method testWithScore.

public void testWithScore() throws Exception {
    Directory indexDir = newDirectory();
    Directory taxoDir = newDirectory();
    DirectoryTaxonomyWriter taxoWriter = new DirectoryTaxonomyWriter(taxoDir);
    IndexWriter iw = new IndexWriter(indexDir, newIndexWriterConfig(new MockAnalyzer(random())));
    FacetsConfig config = new FacetsConfig();
    for (int i = 0; i < 4; i++) {
        Document doc = new Document();
        doc.add(new NumericDocValuesField("price", (i + 1)));
        doc.add(new FacetField("a", Integer.toString(i % 2)));
        iw.addDocument(config.build(taxoWriter, doc));
    }
    DirectoryReader r = DirectoryReader.open(iw);
    DirectoryTaxonomyReader taxoReader = new DirectoryTaxonomyReader(taxoWriter);
    FacetsCollector fc = new FacetsCollector(true);
    // score documents by their 'price' field - makes asserting the correct counts for the categories easier
    Query q = new FunctionQuery(new LongFieldSource("price"));
    FacetsCollector.search(newSearcher(r), q, 10, fc);
    Facets facets = new TaxonomyFacetSumValueSource(taxoReader, config, fc, DoubleValuesSource.SCORES);
    assertEquals("dim=a path=[] value=10.0 childCount=2\n  1 (6.0)\n  0 (4.0)\n", facets.getTopChildren(10, "a").toString());
    iw.close();
    IOUtils.close(taxoWriter, taxoReader, taxoDir, r, indexDir);
}
Also used : FacetsConfig(org.apache.lucene.facet.FacetsConfig) Query(org.apache.lucene.search.Query) FunctionQuery(org.apache.lucene.queries.function.FunctionQuery) ConstantScoreQuery(org.apache.lucene.search.ConstantScoreQuery) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) TermQuery(org.apache.lucene.search.TermQuery) BoostQuery(org.apache.lucene.search.BoostQuery) Facets(org.apache.lucene.facet.Facets) DirectoryReader(org.apache.lucene.index.DirectoryReader) LongFieldSource(org.apache.lucene.queries.function.valuesource.LongFieldSource) FacetField(org.apache.lucene.facet.FacetField) Document(org.apache.lucene.document.Document) FacetsCollector(org.apache.lucene.facet.FacetsCollector) DirectoryTaxonomyWriter(org.apache.lucene.facet.taxonomy.directory.DirectoryTaxonomyWriter) FunctionQuery(org.apache.lucene.queries.function.FunctionQuery) MockAnalyzer(org.apache.lucene.analysis.MockAnalyzer) NumericDocValuesField(org.apache.lucene.document.NumericDocValuesField) IndexWriter(org.apache.lucene.index.IndexWriter) RandomIndexWriter(org.apache.lucene.index.RandomIndexWriter) Directory(org.apache.lucene.store.Directory) DirectoryTaxonomyReader(org.apache.lucene.facet.taxonomy.directory.DirectoryTaxonomyReader)

Example 17 with FunctionQuery

use of org.apache.lucene.queries.function.FunctionQuery in project lucene-solr by apache.

the class StrategyTestCase method checkValueSource.

/** scores[] are in docId order */
protected void checkValueSource(ValueSource vs, float[] scores, float delta) throws IOException {
    FunctionQuery q = new FunctionQuery(vs);
    //    //TODO is there any point to this check?
    //    int expectedDocs[] = new int[scores.length];//fill with ascending 0....length-1
    //    for (int i = 0; i < expectedDocs.length; i++) {
    //      expectedDocs[i] = i;
    //    }
    //    CheckHits.checkHits(random(), q, "", indexSearcher, expectedDocs);
    //TopDocs is sorted but we actually don't care about the order
    //calculates the score
    TopDocs docs = indexSearcher.search(q, 1000);
    for (int i = 0; i < docs.scoreDocs.length; i++) {
        ScoreDoc gotSD = docs.scoreDocs[i];
        float expectedScore = scores[gotSD.doc];
        assertEquals("Not equal for doc " + gotSD.doc, expectedScore, gotSD.score, delta);
    }
    CheckHits.checkExplanations(q, "", indexSearcher);
}
Also used : TopDocs(org.apache.lucene.search.TopDocs) FunctionQuery(org.apache.lucene.queries.function.FunctionQuery) ScoreDoc(org.apache.lucene.search.ScoreDoc)

Example 18 with FunctionQuery

use of org.apache.lucene.queries.function.FunctionQuery in project lucene-solr by apache.

the class SolrReturnFields method add.

private void add(String fl, NamedList<String> rename, DocTransformers augmenters, SolrQueryRequest req) {
    if (fl == null) {
        return;
    }
    try {
        StrParser sp = new StrParser(fl);
        for (; ; ) {
            sp.opt(',');
            sp.eatws();
            if (sp.pos >= sp.end)
                break;
            int start = sp.pos;
            // short circuit test for a really simple field name
            String key = null;
            String field = getFieldName(sp);
            char ch = sp.ch();
            if (field != null) {
                if (sp.opt(':')) {
                    // this was a key, not a field name
                    key = field;
                    field = null;
                    sp.eatws();
                    start = sp.pos;
                } else {
                    if (Character.isWhitespace(ch) || ch == ',' || ch == 0) {
                        addField(field, key, augmenters, false);
                        continue;
                    }
                    // an invalid field name... reset the position pointer to retry
                    sp.pos = start;
                    field = null;
                }
            }
            if (key != null) {
                // we read "key : "
                field = sp.getId(null);
                ch = sp.ch();
                if (field != null && (Character.isWhitespace(ch) || ch == ',' || ch == 0)) {
                    rename.add(field, key);
                    addField(field, key, augmenters, false);
                    continue;
                }
                // an invalid field name... reset the position pointer to retry
                sp.pos = start;
                field = null;
            }
            if (field == null) {
                // We didn't find a simple name, so let's see if it's a globbed field name.
                // Globbing only works with field names of the recommended form (roughly like java identifiers)
                field = sp.getGlobbedId(null);
                ch = sp.ch();
                if (field != null && (Character.isWhitespace(ch) || ch == ',' || ch == 0)) {
                    // "*" looks and acts like a glob, but we give it special treatment
                    if ("*".equals(field)) {
                        _wantsAllFields = true;
                    } else {
                        globs.add(field);
                    }
                    continue;
                }
                // an invalid glob
                sp.pos = start;
            }
            String funcStr = sp.val.substring(start);
            if (funcStr.startsWith("[")) {
                ModifiableSolrParams augmenterParams = new ModifiableSolrParams();
                int end = QueryParsing.parseLocalParams(funcStr, 0, augmenterParams, req.getParams(), "[", ']');
                sp.pos += end;
                // [foo] is short for [type=foo] in localParams syntax
                String augmenterName = augmenterParams.get("type");
                augmenterParams.remove("type");
                String disp = key;
                if (disp == null) {
                    disp = '[' + augmenterName + ']';
                }
                TransformerFactory factory = req.getCore().getTransformerFactory(augmenterName);
                if (factory != null) {
                    DocTransformer t = factory.create(disp, augmenterParams, req);
                    if (t != null) {
                        if (!_wantsAllFields) {
                            String[] extra = t.getExtraRequestFields();
                            if (extra != null) {
                                for (String f : extra) {
                                    // also request this field from IndexSearcher
                                    fields.add(f);
                                }
                            }
                        }
                        augmenters.addTransformer(t);
                    }
                } else {
                //throw new SolrException(ErrorCode.BAD_REQUEST, "Unknown DocTransformer: "+augmenterName);
                }
                addField(field, disp, augmenters, true);
                continue;
            }
            // let's try it as a function instead
            QParser parser = QParser.getParser(funcStr, FunctionQParserPlugin.NAME, req);
            Query q = null;
            ValueSource vs = null;
            try {
                if (parser instanceof FunctionQParser) {
                    FunctionQParser fparser = (FunctionQParser) parser;
                    fparser.setParseMultipleSources(false);
                    fparser.setParseToEnd(false);
                    q = fparser.getQuery();
                    if (fparser.localParams != null) {
                        if (fparser.valFollowedParams) {
                            // need to find the end of the function query via the string parser
                            int leftOver = fparser.sp.end - fparser.sp.pos;
                            // reset our parser to the same amount of leftover
                            sp.pos = sp.end - leftOver;
                        } else {
                            // the value was via the "v" param in localParams, so we need to find
                            // the end of the local params themselves to pick up where we left off
                            sp.pos = start + fparser.localParamsEnd;
                        }
                    } else {
                        // need to find the end of the function query via the string parser
                        int leftOver = fparser.sp.end - fparser.sp.pos;
                        // reset our parser to the same amount of leftover
                        sp.pos = sp.end - leftOver;
                    }
                } else {
                    // A QParser that's not for function queries.
                    // It must have been specified via local params.
                    q = parser.getQuery();
                    assert parser.getLocalParams() != null;
                    sp.pos = start + parser.localParamsEnd;
                }
                funcStr = sp.val.substring(start, sp.pos);
                if (q instanceof FunctionQuery) {
                    vs = ((FunctionQuery) q).getValueSource();
                } else {
                    vs = new QueryValueSource(q, 0.0f);
                }
                if (key == null) {
                    SolrParams localParams = parser.getLocalParams();
                    if (localParams != null) {
                        key = localParams.get("key");
                    }
                }
                if (key == null) {
                    key = funcStr;
                }
                addField(funcStr, key, augmenters, true);
                augmenters.addTransformer(new ValueSourceAugmenter(key, parser, vs));
            } catch (SyntaxError e) {
                // try again, simple rules for a field name with no whitespace
                sp.pos = start;
                field = sp.getSimpleString();
                if (req.getSchema().getFieldOrNull(field) != null) {
                    // OK, it was an oddly named field
                    addField(field, key, augmenters, false);
                    if (key != null) {
                        rename.add(field, key);
                    }
                } else {
                    throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "Error parsing fieldname: " + e.getMessage(), e);
                }
            }
        // end try as function
        }
    // end for(;;)
    } catch (SyntaxError e) {
        throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "Error parsing fieldname", e);
    }
}
Also used : TransformerFactory(org.apache.solr.response.transform.TransformerFactory) Query(org.apache.lucene.search.Query) FunctionQuery(org.apache.lucene.queries.function.FunctionQuery) DocTransformer(org.apache.solr.response.transform.DocTransformer) ModifiableSolrParams(org.apache.solr.common.params.ModifiableSolrParams) FunctionQuery(org.apache.lucene.queries.function.FunctionQuery) ValueSource(org.apache.lucene.queries.function.ValueSource) QueryValueSource(org.apache.lucene.queries.function.valuesource.QueryValueSource) ModifiableSolrParams(org.apache.solr.common.params.ModifiableSolrParams) SolrParams(org.apache.solr.common.params.SolrParams) QueryValueSource(org.apache.lucene.queries.function.valuesource.QueryValueSource) ValueSourceAugmenter(org.apache.solr.response.transform.ValueSourceAugmenter) SolrException(org.apache.solr.common.SolrException)

Example 19 with FunctionQuery

use of org.apache.lucene.queries.function.FunctionQuery in project lucene-solr by apache.

the class TestCustomScoreExplanations method testOneTerm.

public void testOneTerm() throws Exception {
    Query q = new TermQuery(new Term(FIELD, "w1"));
    CustomScoreQuery csq = new CustomScoreQuery(q, new FunctionQuery(new ConstValueSource(5)));
    qtest(csq, new int[] { 0, 1, 2, 3 });
}
Also used : TermQuery(org.apache.lucene.search.TermQuery) FunctionQuery(org.apache.lucene.queries.function.FunctionQuery) Query(org.apache.lucene.search.Query) FunctionQuery(org.apache.lucene.queries.function.FunctionQuery) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) TermQuery(org.apache.lucene.search.TermQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) BoostQuery(org.apache.lucene.search.BoostQuery) Term(org.apache.lucene.index.Term) ConstValueSource(org.apache.lucene.queries.function.valuesource.ConstValueSource)

Example 20 with FunctionQuery

use of org.apache.lucene.queries.function.FunctionQuery in project lucene-solr by apache.

the class TestCustomScoreExplanations method testTopLevelBoost.

public void testTopLevelBoost() throws Exception {
    Query q = new TermQuery(new Term(FIELD, "w1"));
    CustomScoreQuery csq = new CustomScoreQuery(q, new FunctionQuery(new ConstValueSource(5)));
    BooleanQuery.Builder bqB = new BooleanQuery.Builder();
    bqB.add(new MatchAllDocsQuery(), BooleanClause.Occur.MUST);
    bqB.add(csq, BooleanClause.Occur.MUST);
    BooleanQuery bq = bqB.build();
    qtest(new BoostQuery(bq, 6), new int[] { 0, 1, 2, 3 });
}
Also used : TermQuery(org.apache.lucene.search.TermQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) FunctionQuery(org.apache.lucene.queries.function.FunctionQuery) Query(org.apache.lucene.search.Query) FunctionQuery(org.apache.lucene.queries.function.FunctionQuery) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) TermQuery(org.apache.lucene.search.TermQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) BoostQuery(org.apache.lucene.search.BoostQuery) Term(org.apache.lucene.index.Term) ConstValueSource(org.apache.lucene.queries.function.valuesource.ConstValueSource) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) BoostQuery(org.apache.lucene.search.BoostQuery)

Aggregations

FunctionQuery (org.apache.lucene.queries.function.FunctionQuery)20 Query (org.apache.lucene.search.Query)17 ValueSource (org.apache.lucene.queries.function.ValueSource)11 BooleanQuery (org.apache.lucene.search.BooleanQuery)10 BoostQuery (org.apache.lucene.search.BoostQuery)8 TermQuery (org.apache.lucene.search.TermQuery)7 QueryValueSource (org.apache.lucene.queries.function.valuesource.QueryValueSource)6 MatchAllDocsQuery (org.apache.lucene.search.MatchAllDocsQuery)6 Term (org.apache.lucene.index.Term)4 ConstValueSource (org.apache.lucene.queries.function.valuesource.ConstValueSource)4 IndexSearcher (org.apache.lucene.search.IndexSearcher)4 IndexReader (org.apache.lucene.index.IndexReader)3 ScoreDoc (org.apache.lucene.search.ScoreDoc)3 TopDocs (org.apache.lucene.search.TopDocs)3 ArrayList (java.util.ArrayList)2 BoostedQuery (org.apache.lucene.queries.function.BoostedQuery)2 DisjunctionMaxQuery (org.apache.lucene.search.DisjunctionMaxQuery)2 SolrException (org.apache.solr.common.SolrException)2 SolrParams (org.apache.solr.common.params.SolrParams)2 SchemaField (org.apache.solr.schema.SchemaField)2