Search in sources :

Example 21 with ValueSource

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

the class AbstractSpatialFieldType method getQueryFromSpatialArgs.

protected Query getQueryFromSpatialArgs(QParser parser, SchemaField field, SpatialArgs spatialArgs) {
    T strategy = getStrategy(field.getName());
    SolrParams localParams = parser.getLocalParams();
    //See SOLR-2883 needScore
    String scoreParam = (localParams == null ? null : localParams.get(SCORE_PARAM));
    //We get the valueSource for the score then the filter and combine them.
    ValueSource valueSource = getValueSourceFromSpatialArgs(parser, field, spatialArgs, scoreParam, strategy);
    if (valueSource == null) {
        //assumed constant scoring
        return strategy.makeQuery(spatialArgs);
    }
    FunctionQuery functionQuery = new FunctionQuery(valueSource);
    if (localParams != null && !localParams.getBool(FILTER_PARAM, true))
        return functionQuery;
    Query filterQuery = strategy.makeQuery(spatialArgs);
    return new BooleanQuery.Builder().add(functionQuery, //matches everything and provides score
    Occur.MUST).add(filterQuery, //filters (score isn't used)
    Occur.FILTER).build();
}
Also used : 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) BooleanQuery(org.apache.lucene.search.BooleanQuery) ValueSource(org.apache.lucene.queries.function.ValueSource) SolrParams(org.apache.solr.common.params.SolrParams)

Example 22 with ValueSource

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

the class TestOrdValues method doTestExactScore.

// Test that queries based on reverse/ordFieldScore returns docs with expected score.
private void doTestExactScore(String field, boolean inOrder) throws Exception {
    IndexReader r = DirectoryReader.open(dir);
    IndexSearcher s = newSearcher(r);
    ValueSource vs;
    if (inOrder) {
        vs = new OrdFieldSource(field);
    } else {
        vs = new ReverseOrdFieldSource(field);
    }
    Query q = new FunctionQuery(vs);
    TopDocs td = s.search(q, 1000);
    assertEquals("All docs should be matched!", N_DOCS, td.totalHits);
    ScoreDoc[] sd = td.scoreDocs;
    for (int i = 0; i < sd.length; i++) {
        float score = sd[i].score;
        String id = s.getIndexReader().document(sd[i].doc).get(ID_FIELD);
        log("-------- " + i + ". Explain doc " + id);
        log(s.explain(q, sd[i].doc));
        float expectedScore = N_DOCS - i - 1;
        assertEquals("score of result " + i + " should be " + expectedScore + " != " + score, expectedScore, score, TEST_SCORE_TOLERANCE_DELTA);
        String expectedId = inOrder ? // in-order ==> larger  values first
        id2String(N_DOCS - i) : // reverse  ==> smaller values first
        id2String(i + 1);
        assertTrue("id of result " + i + " should be " + expectedId + " != " + score, expectedId.equals(id));
    }
    r.close();
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) Query(org.apache.lucene.search.Query) FunctionQuery(org.apache.lucene.queries.function.FunctionQuery) ScoreDoc(org.apache.lucene.search.ScoreDoc) TopDocs(org.apache.lucene.search.TopDocs) FunctionQuery(org.apache.lucene.queries.function.FunctionQuery) ValueSource(org.apache.lucene.queries.function.ValueSource) IndexReader(org.apache.lucene.index.IndexReader)

Example 23 with ValueSource

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

the class PolyFieldTest method testPointFieldType.

@Test
public void testPointFieldType() throws Exception {
    SolrCore core = h.getCore();
    IndexSchema schema = core.getLatestSchema();
    SchemaField home = schema.getField("home");
    assertNotNull(home);
    assertTrue("home is not a poly field", home.isPolyField());
    FieldType tmp = home.getType();
    assertTrue(tmp instanceof PointType);
    PointType pt = (PointType) tmp;
    assertEquals(pt.getDimension(), 2);
    double[] xy = new double[] { 35.0, -79.34 };
    String point = xy[0] + "," + xy[1];
    List<IndexableField> fields = home.createFields(point);
    assertNotNull(pt.getSubType());
    //If DV=false, we expect one field per dimension plus a stored field
    int expectdNumFields = 3;
    if (pt.subField(home, 0, schema).hasDocValues()) {
        // If docValues=true, then we expect two more fields
        expectdNumFields += 2;
    }
    assertEquals("Unexpected fields created: " + Arrays.toString(fields.toArray()), expectdNumFields, fields.size());
    //first two/four fields contain the values, last one is just stored and contains the original
    for (int i = 0; i < expectdNumFields; i++) {
        boolean hasValue = fields.get(i).binaryValue() != null || fields.get(i).stringValue() != null || fields.get(i).numericValue() != null;
        assertTrue("Doesn't have a value: " + fields.get(i), hasValue);
    }
    /*assertTrue("first field " + fields[0].tokenStreamValue() +  " is not 35.0", pt.getSubType().toExternal(fields[0]).equals(String.valueOf(xy[0])));
    assertTrue("second field is not -79.34", pt.getSubType().toExternal(fields[1]).equals(String.valueOf(xy[1])));
    assertTrue("third field is not '35.0,-79.34'", pt.getSubType().toExternal(fields[2]).equals(point));*/
    home = schema.getField("home_ns");
    assertNotNull(home);
    fields = home.createFields(point);
    //one less field than with "home", since we aren't storing
    assertEquals(expectdNumFields - 1, fields.size(), 2);
    home = schema.getField("home_ns");
    assertNotNull(home);
    try {
        fields = home.createFields("35.0,foo");
        assertTrue(false);
    } catch (Exception e) {
    //
    }
    SchemaField s1 = schema.getField("test_p");
    SchemaField s2 = schema.getField("test_p");
    ValueSource v1 = s1.getType().getValueSource(s1, null);
    ValueSource v2 = s2.getType().getValueSource(s2, null);
    assertEquals(v1, v2);
    assertEquals(v1.hashCode(), v2.hashCode());
}
Also used : SolrCore(org.apache.solr.core.SolrCore) SolrException(org.apache.solr.common.SolrException) IndexableField(org.apache.lucene.index.IndexableField) ValueSource(org.apache.lucene.queries.function.ValueSource) Test(org.junit.Test)

Example 24 with ValueSource

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

the class VersionInfo method getMaxVersionFromIndex.

/**
   * Returns the highest version from the index, or 0L if no versions can be found in the index.
   */
public Long getMaxVersionFromIndex(IndexSearcher searcher) throws IOException {
    String versionFieldName = versionField.getName();
    log.debug("Refreshing highest value of {} for {} version buckets from index", versionFieldName, buckets.length);
    long maxVersionInIndex = 0L;
    // if indexed, then we have terms to get the max from
    if (versionField.indexed()) {
        LeafReader leafReader = SlowCompositeReaderWrapper.wrap(searcher.getIndexReader());
        Terms versionTerms = leafReader.terms(versionFieldName);
        Long max = (versionTerms != null) ? LegacyNumericUtils.getMaxLong(versionTerms) : null;
        if (max != null) {
            maxVersionInIndex = max.longValue();
            log.debug("Found MAX value {} from Terms for {} in index", maxVersionInIndex, versionFieldName);
        } else {
            log.debug("No terms found for {}, cannot seed version bucket highest value from index", versionFieldName);
        }
    } else {
        ValueSource vs = versionField.getType().getValueSource(versionField, null);
        Map funcContext = ValueSource.newContext(searcher);
        vs.createWeight(funcContext, searcher);
        // TODO: multi-thread this
        for (LeafReaderContext ctx : searcher.getTopReaderContext().leaves()) {
            int maxDoc = ctx.reader().maxDoc();
            FunctionValues fv = vs.getValues(funcContext, ctx);
            for (int doc = 0; doc < maxDoc; doc++) {
                long v = fv.longVal(doc);
                maxVersionInIndex = Math.max(v, maxVersionInIndex);
            }
        }
    }
    return maxVersionInIndex;
}
Also used : LeafReader(org.apache.lucene.index.LeafReader) ValueSource(org.apache.lucene.queries.function.ValueSource) Terms(org.apache.lucene.index.Terms) LeafReaderContext(org.apache.lucene.index.LeafReaderContext) FunctionValues(org.apache.lucene.queries.function.FunctionValues) Map(java.util.Map)

Example 25 with ValueSource

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

the class VersionInfo method getVersionFromIndex.

/**
   * Returns the latest version from the index, searched by the given id (bytes) as seen from the realtime searcher.
   * Returns null if no document can be found in the index for the given id.
   */
public Long getVersionFromIndex(BytesRef idBytes) {
    // TODO: we could cache much of this and invalidate during a commit.
    // TODO: most DocValues classes are threadsafe - expose which.
    RefCounted<SolrIndexSearcher> newestSearcher = ulog.uhandler.core.getRealtimeSearcher();
    try {
        SolrIndexSearcher searcher = newestSearcher.get();
        long lookup = searcher.lookupId(idBytes);
        // this means the doc doesn't exist in the index yet
        if (lookup < 0)
            return null;
        ValueSource vs = versionField.getType().getValueSource(versionField, null);
        Map context = ValueSource.newContext(searcher);
        vs.createWeight(context, searcher);
        FunctionValues fv = vs.getValues(context, searcher.getTopReaderContext().leaves().get((int) (lookup >> 32)));
        long ver = fv.longVal((int) lookup);
        return ver;
    } catch (IOException e) {
        throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, "Error reading version from index", e);
    } finally {
        if (newestSearcher != null) {
            newestSearcher.decref();
        }
    }
}
Also used : ValueSource(org.apache.lucene.queries.function.ValueSource) FunctionValues(org.apache.lucene.queries.function.FunctionValues) SolrIndexSearcher(org.apache.solr.search.SolrIndexSearcher) IOException(java.io.IOException) Map(java.util.Map) SolrException(org.apache.solr.common.SolrException)

Aggregations

ValueSource (org.apache.lucene.queries.function.ValueSource)54 Query (org.apache.lucene.search.Query)13 FunctionQuery (org.apache.lucene.queries.function.FunctionQuery)12 SolrException (org.apache.solr.common.SolrException)11 SchemaField (org.apache.solr.schema.SchemaField)11 FunctionValues (org.apache.lucene.queries.function.FunctionValues)10 ArrayList (java.util.ArrayList)8 QueryValueSource (org.apache.lucene.queries.function.valuesource.QueryValueSource)7 FieldType (org.apache.solr.schema.FieldType)6 BooleanQuery (org.apache.lucene.search.BooleanQuery)5 AggValueSource (org.apache.solr.search.facet.AggValueSource)5 IOException (java.io.IOException)4 Map (java.util.Map)4 DoubleConstValueSource (org.apache.lucene.queries.function.valuesource.DoubleConstValueSource)4 VectorValueSource (org.apache.lucene.queries.function.valuesource.VectorValueSource)4 MatchAllDocsQuery (org.apache.lucene.search.MatchAllDocsQuery)4 IndexReader (org.apache.lucene.index.IndexReader)3 LeafReaderContext (org.apache.lucene.index.LeafReaderContext)3 BoostedQuery (org.apache.lucene.queries.function.BoostedQuery)3 TermGroupSelector (org.apache.lucene.search.grouping.TermGroupSelector)3