Search in sources :

Example 6 with QueryValueSource

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

the class TestValueSources method testQueryWrapedFuncWrapedQuery.

public void testQueryWrapedFuncWrapedQuery() throws Exception {
    ValueSource vs = new QueryValueSource(new FunctionQuery(new ConstValueSource(2f)), 0f);
    assertHits(new FunctionQuery(vs), new float[] { 2f, 2f });
    assertAllExist(vs);
}
Also used : SumTotalTermFreqValueSource(org.apache.lucene.queries.function.valuesource.SumTotalTermFreqValueSource) DoubleConstValueSource(org.apache.lucene.queries.function.valuesource.DoubleConstValueSource) ConstValueSource(org.apache.lucene.queries.function.valuesource.ConstValueSource) QueryValueSource(org.apache.lucene.queries.function.valuesource.QueryValueSource) DocFreqValueSource(org.apache.lucene.queries.function.valuesource.DocFreqValueSource) NormValueSource(org.apache.lucene.queries.function.valuesource.NormValueSource) NumDocsValueSource(org.apache.lucene.queries.function.valuesource.NumDocsValueSource) MaxDocValueSource(org.apache.lucene.queries.function.valuesource.MaxDocValueSource) JoinDocFreqValueSource(org.apache.lucene.queries.function.valuesource.JoinDocFreqValueSource) LiteralValueSource(org.apache.lucene.queries.function.valuesource.LiteralValueSource) TotalTermFreqValueSource(org.apache.lucene.queries.function.valuesource.TotalTermFreqValueSource) IDFValueSource(org.apache.lucene.queries.function.valuesource.IDFValueSource) TermFreqValueSource(org.apache.lucene.queries.function.valuesource.TermFreqValueSource) TFValueSource(org.apache.lucene.queries.function.valuesource.TFValueSource) DoubleConstValueSource(org.apache.lucene.queries.function.valuesource.DoubleConstValueSource) ConstValueSource(org.apache.lucene.queries.function.valuesource.ConstValueSource) QueryValueSource(org.apache.lucene.queries.function.valuesource.QueryValueSource)

Example 7 with QueryValueSource

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

the class StatsComponentTest method testStatsFieldWhitebox.

/**
   * Whitebox test of {@link StatsField} parsing to ensure expected equivilence 
   * operations hold up
   */
public void testStatsFieldWhitebox() throws Exception {
    StatsComponent component = new StatsComponent();
    List<SearchComponent> components = new ArrayList<>(1);
    components.add(component);
    SolrParams common = params("stats", "true", "q", "*:*", "nested", "foo_t:cow");
    // all of these should produce the same SchemaField based StatsField
    for (String param : new String[] { "foo_i", "{!func}field(\"foo_i\")", "{!lucene}_val_:\"field(foo_i)\"" }) {
        SolrQueryRequest req = req(common);
        try {
            ResponseBuilder rb = new ResponseBuilder(req, new SolrQueryResponse(), components);
            StatsField sf = new StatsField(rb, param);
            assertNull("value source of: " + param, sf.getValueSource());
            assertNotNull("schema field of: " + param, sf.getSchemaField());
            assertEquals("field name of: " + param, "foo_i", sf.getSchemaField().getName());
        } finally {
            req.close();
        }
    }
    // all of these should produce the same QueryValueSource based StatsField
    for (String param : new String[] { "{!lucene}foo_t:cow", "{!func}query($nested)", "{!field f=foo_t}cow" }) {
        SolrQueryRequest req = req(common);
        try {
            ResponseBuilder rb = new ResponseBuilder(req, new SolrQueryResponse(), components);
            StatsField sf = new StatsField(rb, param);
            assertNull("schema field of: " + param, sf.getSchemaField());
            assertNotNull("value source of: " + param, sf.getValueSource());
            assertTrue(sf.getValueSource().getClass() + " is vs type of: " + param, sf.getValueSource() instanceof QueryValueSource);
            QueryValueSource qvs = (QueryValueSource) sf.getValueSource();
            assertEquals("query of :" + param, new TermQuery(new Term("foo_t", "cow")), qvs.getQuery());
        } finally {
            req.close();
        }
    }
}
Also used : TermQuery(org.apache.lucene.search.TermQuery) SolrQueryResponse(org.apache.solr.response.SolrQueryResponse) ArrayList(java.util.ArrayList) Term(org.apache.lucene.index.Term) SolrQueryRequest(org.apache.solr.request.SolrQueryRequest) LocalSolrQueryRequest(org.apache.solr.request.LocalSolrQueryRequest) SolrParams(org.apache.solr.common.params.SolrParams) MapSolrParams(org.apache.solr.common.params.MapSolrParams) QueryValueSource(org.apache.lucene.queries.function.valuesource.QueryValueSource)

Example 8 with QueryValueSource

use of org.apache.lucene.queries.function.valuesource.QueryValueSource 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 9 with QueryValueSource

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

the class TestValueSources method testQuery.

public void testQuery() throws Exception {
    Similarity saved = searcher.getSimilarity(true);
    try {
        searcher.setSimilarity(new ClassicSimilarity());
        ValueSource vs = new QueryValueSource(new TermQuery(new Term("string", "bar")), 42F);
        assertHits(new FunctionQuery(vs), new float[] { 42F, 1.4054651F });
        // valuesource should exist only for things matching the term query
        // sanity check via quick & dirty wrapper arround tf
        ValueSource expected = new MultiFloatFunction(new ValueSource[] { new TFValueSource("bogus", "bogus", "string", new BytesRef("bar")) }) {

            @Override
            protected String name() {
                return "tf_based_exists";
            }

            @Override
            protected float func(int doc, FunctionValues[] valsArr) throws IOException {
                return valsArr[0].floatVal(doc);
            }

            @Override
            protected boolean exists(int doc, FunctionValues[] valsArr) throws IOException {
                // if tf > 0, then it should exist
                return 0 < func(doc, valsArr);
            }
        };
        assertExists(expected, vs);
        // Query matches all docs, func exists for all docs
        vs = new QueryValueSource(new TermQuery(new Term("text", "test")), 0F);
        assertAllExist(vs);
        // Query matches no docs, func exists for no docs
        vs = new QueryValueSource(new TermQuery(new Term("bogus", "does not exist")), 0F);
        assertNoneExist(vs);
    } finally {
        searcher.setSimilarity(saved);
    }
}
Also used : ClassicSimilarity(org.apache.lucene.search.similarities.ClassicSimilarity) TermQuery(org.apache.lucene.search.TermQuery) ClassicSimilarity(org.apache.lucene.search.similarities.ClassicSimilarity) Similarity(org.apache.lucene.search.similarities.Similarity) MultiFloatFunction(org.apache.lucene.queries.function.valuesource.MultiFloatFunction) SumTotalTermFreqValueSource(org.apache.lucene.queries.function.valuesource.SumTotalTermFreqValueSource) DoubleConstValueSource(org.apache.lucene.queries.function.valuesource.DoubleConstValueSource) ConstValueSource(org.apache.lucene.queries.function.valuesource.ConstValueSource) QueryValueSource(org.apache.lucene.queries.function.valuesource.QueryValueSource) DocFreqValueSource(org.apache.lucene.queries.function.valuesource.DocFreqValueSource) NormValueSource(org.apache.lucene.queries.function.valuesource.NormValueSource) NumDocsValueSource(org.apache.lucene.queries.function.valuesource.NumDocsValueSource) MaxDocValueSource(org.apache.lucene.queries.function.valuesource.MaxDocValueSource) JoinDocFreqValueSource(org.apache.lucene.queries.function.valuesource.JoinDocFreqValueSource) LiteralValueSource(org.apache.lucene.queries.function.valuesource.LiteralValueSource) TotalTermFreqValueSource(org.apache.lucene.queries.function.valuesource.TotalTermFreqValueSource) IDFValueSource(org.apache.lucene.queries.function.valuesource.IDFValueSource) TermFreqValueSource(org.apache.lucene.queries.function.valuesource.TermFreqValueSource) TFValueSource(org.apache.lucene.queries.function.valuesource.TFValueSource) TFValueSource(org.apache.lucene.queries.function.valuesource.TFValueSource) Term(org.apache.lucene.index.Term) QueryValueSource(org.apache.lucene.queries.function.valuesource.QueryValueSource) BytesRef(org.apache.lucene.util.BytesRef)

Aggregations

QueryValueSource (org.apache.lucene.queries.function.valuesource.QueryValueSource)9 FunctionQuery (org.apache.lucene.queries.function.FunctionQuery)6 ValueSource (org.apache.lucene.queries.function.ValueSource)5 Query (org.apache.lucene.search.Query)5 ArrayList (java.util.ArrayList)3 Term (org.apache.lucene.index.Term)2 BoostedQuery (org.apache.lucene.queries.function.BoostedQuery)2 ConstValueSource (org.apache.lucene.queries.function.valuesource.ConstValueSource)2 DocFreqValueSource (org.apache.lucene.queries.function.valuesource.DocFreqValueSource)2 DoubleConstValueSource (org.apache.lucene.queries.function.valuesource.DoubleConstValueSource)2 IDFValueSource (org.apache.lucene.queries.function.valuesource.IDFValueSource)2 JoinDocFreqValueSource (org.apache.lucene.queries.function.valuesource.JoinDocFreqValueSource)2 LiteralValueSource (org.apache.lucene.queries.function.valuesource.LiteralValueSource)2 MaxDocValueSource (org.apache.lucene.queries.function.valuesource.MaxDocValueSource)2 NormValueSource (org.apache.lucene.queries.function.valuesource.NormValueSource)2 NumDocsValueSource (org.apache.lucene.queries.function.valuesource.NumDocsValueSource)2 SumTotalTermFreqValueSource (org.apache.lucene.queries.function.valuesource.SumTotalTermFreqValueSource)2 TFValueSource (org.apache.lucene.queries.function.valuesource.TFValueSource)2 TermFreqValueSource (org.apache.lucene.queries.function.valuesource.TermFreqValueSource)2 TotalTermFreqValueSource (org.apache.lucene.queries.function.valuesource.TotalTermFreqValueSource)2