Search in sources :

Example 11 with ValueSource

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

the class GeoDistValueSourceParser method makeMV.

/** make a MultiValueSource from two non MultiValueSources */
private VectorValueSource makeMV(List<ValueSource> sources, List<ValueSource> orig) throws SyntaxError {
    ValueSource vs1 = sources.get(0);
    ValueSource vs2 = sources.get(1);
    if (vs1 instanceof MultiValueSource || vs2 instanceof MultiValueSource) {
        throw new SyntaxError("geodist - invalid parameters:" + orig);
    }
    return new VectorValueSource(sources);
}
Also used : SyntaxError(org.apache.solr.search.SyntaxError) VectorValueSource(org.apache.lucene.queries.function.valuesource.VectorValueSource) ValueSource(org.apache.lucene.queries.function.ValueSource) DoubleConstValueSource(org.apache.lucene.queries.function.valuesource.DoubleConstValueSource) MultiValueSource(org.apache.lucene.queries.function.valuesource.MultiValueSource) VectorValueSource(org.apache.lucene.queries.function.valuesource.VectorValueSource) MultiValueSource(org.apache.lucene.queries.function.valuesource.MultiValueSource)

Example 12 with ValueSource

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

the class DirectUpdateHandler2 method getQuery.

private Query getQuery(DeleteUpdateCommand cmd) {
    Query q;
    try {
        // move this higher in the stack?
        QParser parser = QParser.getParser(cmd.getQuery(), cmd.req);
        q = parser.getQuery();
        q = QueryUtils.makeQueryable(q);
        // Make sure not to delete newer versions
        if (ulog != null && cmd.getVersion() != 0 && cmd.getVersion() != -Long.MAX_VALUE) {
            BooleanQuery.Builder bq = new BooleanQuery.Builder();
            bq.add(q, Occur.MUST);
            SchemaField sf = ulog.getVersionInfo().getVersionField();
            ValueSource vs = sf.getType().getValueSource(sf, null);
            ValueSourceRangeFilter filt = new ValueSourceRangeFilter(vs, Long.toString(Math.abs(cmd.getVersion())), null, true, true);
            FunctionRangeQuery range = new FunctionRangeQuery(filt);
            // formulated in the "MUST_NOT" sense so we can delete docs w/o a version (some tests depend on this...)
            bq.add(range, Occur.MUST_NOT);
            q = bq.build();
        }
        return q;
    } catch (SyntaxError e) {
        throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, e);
    }
}
Also used : SchemaField(org.apache.solr.schema.SchemaField) FunctionRangeQuery(org.apache.solr.search.FunctionRangeQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) Query(org.apache.lucene.search.Query) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) FunctionRangeQuery(org.apache.solr.search.FunctionRangeQuery) TermQuery(org.apache.lucene.search.TermQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) ValueSourceRangeFilter(org.apache.solr.search.function.ValueSourceRangeFilter) SyntaxError(org.apache.solr.search.SyntaxError) ValueSource(org.apache.lucene.queries.function.ValueSource) QParser(org.apache.solr.search.QParser) SolrException(org.apache.solr.common.SolrException)

Example 13 with ValueSource

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

the class SpatialExample method search.

private void search() throws Exception {
    IndexReader indexReader = DirectoryReader.open(directory);
    IndexSearcher indexSearcher = new IndexSearcher(indexReader);
    Sort idSort = new Sort(new SortField("id", SortField.Type.INT));
    //--Filter by circle (<= distance from a point)
    {
        //Search with circle
        //note: SpatialArgs can be parsed from a string
        SpatialArgs args = new SpatialArgs(SpatialOperation.Intersects, ctx.makeCircle(-80.0, 33.0, DistanceUtils.dist2Degrees(200, DistanceUtils.EARTH_MEAN_RADIUS_KM)));
        Query query = strategy.makeQuery(args);
        TopDocs docs = indexSearcher.search(query, 10, idSort);
        assertDocMatchedIds(indexSearcher, docs, 2);
        //Now, lets get the distance for the 1st doc via computing from stored point value:
        // (this computation is usually not redundant)
        Document doc1 = indexSearcher.doc(docs.scoreDocs[0].doc);
        String doc1Str = doc1.getField(strategy.getFieldName()).stringValue();
        //assume doc1Str is "x y" as written in newSampleDocument()
        int spaceIdx = doc1Str.indexOf(' ');
        double x = Double.parseDouble(doc1Str.substring(0, spaceIdx));
        double y = Double.parseDouble(doc1Str.substring(spaceIdx + 1));
        double doc1DistDEG = ctx.calcDistance(args.getShape().getCenter(), x, y);
        assertEquals(121.6d, DistanceUtils.degrees2Dist(doc1DistDEG, DistanceUtils.EARTH_MEAN_RADIUS_KM), 0.1);
        //or more simply:
        assertEquals(121.6d, doc1DistDEG * DistanceUtils.DEG_TO_KM, 0.1);
    }
    //--Match all, order by distance ascending
    {
        Point pt = ctx.makePoint(60, -50);
        //the distance (in km)
        ValueSource valueSource = strategy.makeDistanceValueSource(pt, DistanceUtils.DEG_TO_KM);
        //false=asc dist
        Sort distSort = new Sort(valueSource.getSortField(false)).rewrite(indexSearcher);
        TopDocs docs = indexSearcher.search(new MatchAllDocsQuery(), 10, distSort);
        assertDocMatchedIds(indexSearcher, docs, 4, 20, 2);
    //To get the distance, we could compute from stored values like earlier.
    // However in this example we sorted on it, and the distance will get
    // computed redundantly.  If the distance is only needed for the top-X
    // search results then that's not a big deal. Alternatively, try wrapping
    // the ValueSource with CachingDoubleValueSource then retrieve the value
    // from the ValueSource now. See LUCENE-4541 for an example.
    }
    //demo arg parsing
    {
        SpatialArgs args = new SpatialArgs(SpatialOperation.Intersects, ctx.makeCircle(-80.0, 33.0, 1));
        SpatialArgs args2 = new SpatialArgsParser().parse("Intersects(BUFFER(POINT(-80 33),1))", ctx);
        assertEquals(args.toString(), args2.toString());
    }
    indexReader.close();
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) SpatialArgs(org.apache.lucene.spatial.query.SpatialArgs) Query(org.apache.lucene.search.Query) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) SpatialArgsParser(org.apache.lucene.spatial.query.SpatialArgsParser) SortField(org.apache.lucene.search.SortField) Point(org.locationtech.spatial4j.shape.Point) Document(org.apache.lucene.document.Document) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) TopDocs(org.apache.lucene.search.TopDocs) ValueSource(org.apache.lucene.queries.function.ValueSource) IndexReader(org.apache.lucene.index.IndexReader) Sort(org.apache.lucene.search.Sort)

Example 14 with ValueSource

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

the class GeoFieldUpdater method create.

@Override
public DocTransformer create(String display, SolrParams params, SolrQueryRequest req) {
    String fname = params.get("f", display);
    if (fname.startsWith("[") && fname.endsWith("]")) {
        fname = display.substring(1, display.length() - 1);
    }
    SchemaField sf = req.getSchema().getFieldOrNull(fname);
    if (sf == null) {
        throw new SolrException(ErrorCode.BAD_REQUEST, this.getClass().getSimpleName() + " using unknown field: " + fname);
    }
    if (!(sf.getType() instanceof AbstractSpatialFieldType)) {
        throw new SolrException(ErrorCode.BAD_REQUEST, "GeoTransformer requested non-spatial field: " + fname + " (" + sf.getType().getClass().getSimpleName() + ")");
    }
    final GeoFieldUpdater updater = new GeoFieldUpdater();
    updater.field = fname;
    updater.display = display;
    updater.display_error = display + "_error";
    ValueSource shapes = null;
    AbstractSpatialFieldType<?> sdv = (AbstractSpatialFieldType<?>) sf.getType();
    SpatialStrategy strategy = sdv.getStrategy(fname);
    if (strategy instanceof CompositeSpatialStrategy) {
        shapes = ((CompositeSpatialStrategy) strategy).getGeometryStrategy().makeShapeValueSource();
    } else if (strategy instanceof SerializedDVStrategy) {
        shapes = ((SerializedDVStrategy) strategy).makeShapeValueSource();
    }
    String writerName = params.get("w", "GeoJSON");
    updater.formats = strategy.getSpatialContext().getFormats();
    updater.writer = updater.formats.getWriter(writerName);
    if (updater.writer == null) {
        StringBuilder str = new StringBuilder();
        str.append("Unknown Spatial Writer: ").append(writerName);
        str.append(" [");
        for (ShapeWriter w : updater.formats.getWriters()) {
            str.append(w.getFormatName()).append(' ');
        }
        str.append("]");
        throw new SolrException(ErrorCode.BAD_REQUEST, str.toString());
    }
    QueryResponseWriter qw = req.getCore().getQueryResponseWriter(req);
    updater.isJSON = (qw.getClass() == JSONResponseWriter.class) && (updater.writer instanceof GeoJSONWriter);
    // Using ValueSource
    if (shapes != null) {
        // we don't really need the qparser... just so we can reuse valueSource
        QParser parser = new QParser(null, null, params, req) {

            @Override
            public Query parse() throws SyntaxError {
                return new MatchAllDocsQuery();
            }
        };
        return new ValueSourceAugmenter(display, parser, shapes) {

            @Override
            protected void setValue(SolrDocument doc, Object val) {
                updater.setValue(doc, val);
            }
        };
    }
    // Using the raw stored values
    return new DocTransformer() {

        @Override
        public void transform(SolrDocument doc, int docid, float score) throws IOException {
            Object val = doc.remove(updater.field);
            if (val != null) {
                updater.setValue(doc, val);
            }
        }

        @Override
        public String getName() {
            return updater.display;
        }

        @Override
        public String[] getExtraRequestFields() {
            return new String[] { updater.field };
        }
    };
}
Also used : CompositeSpatialStrategy(org.apache.lucene.spatial.composite.CompositeSpatialStrategy) JSONResponseWriter(org.apache.solr.response.JSONResponseWriter) ShapeWriter(org.locationtech.spatial4j.io.ShapeWriter) AbstractSpatialFieldType(org.apache.solr.schema.AbstractSpatialFieldType) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) CompositeSpatialStrategy(org.apache.lucene.spatial.composite.CompositeSpatialStrategy) SpatialStrategy(org.apache.lucene.spatial.SpatialStrategy) SerializedDVStrategy(org.apache.lucene.spatial.serialized.SerializedDVStrategy) SchemaField(org.apache.solr.schema.SchemaField) SolrDocument(org.apache.solr.common.SolrDocument) ValueSource(org.apache.lucene.queries.function.ValueSource) QParser(org.apache.solr.search.QParser) QueryResponseWriter(org.apache.solr.response.QueryResponseWriter) GeoJSONWriter(org.locationtech.spatial4j.io.GeoJSONWriter) SolrException(org.apache.solr.common.SolrException)

Example 15 with ValueSource

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

the class Grouping method addFunctionCommand.

public void addFunctionCommand(String groupByStr, SolrQueryRequest request) throws SyntaxError {
    QParser parser = QParser.getParser(groupByStr, "func", request);
    Query q = parser.getQuery();
    final Grouping.Command gc;
    if (q instanceof FunctionQuery) {
        ValueSource valueSource = ((FunctionQuery) q).getValueSource();
        if (valueSource instanceof StrFieldSource) {
            String field = ((StrFieldSource) valueSource).getField();
            CommandField commandField = new CommandField();
            commandField.groupBy = field;
            gc = commandField;
        } else {
            CommandFunc commandFunc = new CommandFunc();
            commandFunc.groupBy = valueSource;
            gc = commandFunc;
        }
    } else {
        CommandFunc commandFunc = new CommandFunc();
        commandFunc.groupBy = new QueryValueSource(q, 0.0f);
        gc = commandFunc;
    }
    gc.withinGroupSort = withinGroupSort;
    gc.key = groupByStr;
    gc.numGroups = limitDefault;
    gc.docsPerGroup = docsPerGroupDefault;
    gc.groupOffset = groupOffsetDefault;
    gc.offset = cmd.getOffset();
    gc.groupSort = groupSort;
    gc.format = defaultFormat;
    gc.totalCount = defaultTotalCount;
    if (main) {
        gc.main = true;
        gc.format = Grouping.Format.simple;
    }
    if (gc.format == Grouping.Format.simple) {
        // doesn't make sense
        gc.groupOffset = 0;
    }
    commands.add(gc);
}
Also used : 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) QueryValueSource(org.apache.lucene.queries.function.valuesource.QueryValueSource) ValueSource(org.apache.lucene.queries.function.ValueSource) StrFieldSource(org.apache.solr.schema.StrFieldSource) QueryValueSource(org.apache.lucene.queries.function.valuesource.QueryValueSource)

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