List of usage examples for org.apache.lucene.queries.function ValueSource getSortField
public SortField getSortField(boolean reverse)
From source file:com.orientechnologies.lucene.manager.OLuceneSpatialIndexManager.java
License:Apache License
public Object searchIntersect(OCompositeKey key, double distance, OCommandContext context) throws IOException { double lat = ((Double) OType.convert(((OCompositeKey) key).getKeys().get(0), Double.class)).doubleValue(); double lng = ((Double) OType.convert(((OCompositeKey) key).getKeys().get(1), Double.class)).doubleValue(); SpatialOperation operation = SpatialOperation.Intersects; Point p = ctx.makePoint(lng, lat); SpatialArgs args = new SpatialArgs(operation, ctx.makeCircle(lng, lat, DistanceUtils.dist2Degrees(distance, DistanceUtils.EARTH_MEAN_RADIUS_KM))); Filter filter = strategy.makeFilter(args); IndexSearcher searcher = getSearcher(); ValueSource valueSource = strategy.makeDistanceValueSource(p); Sort distSort = new Sort(valueSource.getSortField(false)).rewrite(searcher); return new LuceneResultSet(this, new SpatialQueryContext(context, searcher, new MatchAllDocsQuery(), filter, distSort) .setSpatialArgs(args)); }
From source file:com.orientechnologies.spatial.engine.OLuceneLegacySpatialIndexEngine.java
License:Apache License
public Object searchIntersect(OCompositeKey key, double distance, OCommandContext context) throws IOException { double lat = ((Double) OType.convert(((OCompositeKey) key).getKeys().get(0), Double.class)).doubleValue(); double lng = ((Double) OType.convert(((OCompositeKey) key).getKeys().get(1), Double.class)).doubleValue(); SpatialOperation operation = SpatialOperation.Intersects; Point p = ctx.makePoint(lng, lat); SpatialArgs args = new SpatialArgs(operation, ctx.makeCircle(lng, lat, DistanceUtils.dist2Degrees(distance, DistanceUtils.EARTH_MEAN_RADIUS_KM))); Filter filter = strategy.makeFilter(args); IndexSearcher searcher = searcher(); ValueSource valueSource = strategy.makeDistanceValueSource(p); Sort distSort = new Sort(valueSource.getSortField(false)).rewrite(searcher); return new LuceneResultSet(this, new SpatialQueryContext(context, searcher, new MatchAllDocsQuery(), filter, distSort) .setSpatialArgs(args)); }
From source file:com.orientechnologies.spatial.strategy.SpatialQueryBuilderNear.java
License:Apache License
@Override public SpatialQueryContext build(Map<String, Object> query) throws Exception { Shape shape = parseShape(query); double distance = 0; Number n = (Number) query.get(MAX_DISTANCE); if (n != null) { distance = n.doubleValue();/*from w ww . j a va2 s . c o m*/ } Point p = (Point) shape; SpatialArgs args = new SpatialArgs(SpatialOperation.Intersects, factory.context().makeCircle(p.getX(), p.getY(), DistanceUtils.dist2Degrees(distance, DistanceUtils.EARTH_MEAN_RADIUS_KM))); Filter filter = manager.strategy().makeFilter(args); ValueSource valueSource = manager.strategy().makeDistanceValueSource(p); IndexSearcher searcher = manager.searcher(); Sort distSort = new Sort(valueSource.getSortField(false)).rewrite(searcher); return new SpatialQueryContext(null, searcher, new MatchAllDocsQuery(), filter, distSort) .setSpatialArgs(args); }
From source file:com.stratio.cassandra.lucene.search.sort.GeoDistanceSortField.java
License:Apache License
/** {@inheritDoc} */ @Override//from w ww .java 2 s .c o m public org.apache.lucene.search.SortField sortField(Schema schema) { final Mapper mapper = schema.getMapper(this.mapper); if (mapper == null) { throw new IndexException("No mapper found for sortFields mapper '%s'", this.mapper); } else if (!mapper.sorted) { throw new IndexException("Mapper '%s' is not sorted", mapper.field); } else if (!(mapper instanceof GeoPointMapper)) { throw new IndexException("Only Geo Point Mapper is allowed but Mapper '%s' is not", mapper.field); } GeoPointMapper geoPointMapper = (GeoPointMapper) mapper; SpatialStrategy strategy = geoPointMapper.getDistanceStrategy(); Point pt = GeoPointMapper.SPATIAL_CONTEXT.makePoint(longitude, latitude); // The distance (in km) ValueSource valueSource = strategy.makeDistanceValueSource(pt, DistanceUtils.DEG_TO_KM); return valueSource.getSortField(this.reverse); }
From source file:com.xiaomi.linden.core.search.query.sort.SortConstructor.java
License:Apache License
public static Sort constructSort(LindenSearchRequest request, IndexSearcher indexSearcher, LindenConfig config) throws IOException { if (!request.isSetSort()) return null; LindenSort lindenSort = request.getSort(); SortField[] sortFields = new SortField[lindenSort.getFieldsSize()]; for (int i = 0; i < lindenSort.getFieldsSize(); ++i) { LindenSortField field = lindenSort.getFields().get(i); SortField.Type type = SortField.Type.STRING; boolean isReverse = field.isReverse(); switch (field.getType()) { case STRING: type = SortField.Type.STRING; break; case DOUBLE: type = SortField.Type.DOUBLE; break; case FLOAT: type = SortField.Type.FLOAT; break; case INTEGER: type = SortField.Type.INT; break; case LONG: type = SortField.Type.LONG; break; case SCORE: type = SortField.Type.SCORE; isReverse = !isReverse;/*from w w w . j a va2 s . c om*/ break; case DISTANCE: if (request.isSetSpatialParam()) { Point point = SpatialContext.GEO.makePoint( request.getSpatialParam().getCoordinate().getLongitude(), request.getSpatialParam().getCoordinate().getLatitude()); ValueSource valueSource = config.getSpatialStrategy().makeDistanceValueSource(point, DistanceUtils.DEG_TO_KM); sortFields[i] = valueSource.getSortField(false).rewrite(indexSearcher); } continue; } sortFields[i] = new SortField(field.getName(), type, isReverse); } return new Sort(sortFields); }
From source file:org.elasticsearch.script.expression.ExpressionScriptEngine.java
License:Apache License
private SearchScript.LeafFactory newSearchScript(Expression expr, SearchLookup lookup, @Nullable Map<String, Object> vars) { MapperService mapper = lookup.doc().mapperService(); // NOTE: if we need to do anything complicated with bindings in the future, we can just extend Bindings, // instead of complicating SimpleBindings (which should stay simple) SimpleBindings bindings = new SimpleBindings(); ReplaceableConstDoubleValueSource specialValue = null; boolean needsScores = false; for (String variable : expr.variables) { try {/* w w w. j ava 2 s. c o m*/ if (variable.equals("_score")) { bindings.add(new SortField("_score", SortField.Type.SCORE)); needsScores = true; } else if (variable.equals("_value")) { specialValue = new ReplaceableConstDoubleValueSource(); bindings.add("_value", specialValue); // noop: _value is special for aggregations, and is handled in ExpressionScriptBindings // TODO: if some uses it in a scoring expression, they will get a nasty failure when evaluating...need a // way to know this is for aggregations and so _value is ok to have... } else if (vars != null && vars.containsKey(variable)) { // TODO: document and/or error if vars contains _score? // NOTE: by checking for the variable in vars first, it allows masking document fields with a global constant, // but if we were to reverse it, we could provide a way to supply dynamic defaults for documents missing the field? Object value = vars.get(variable); if (value instanceof Number) { bindings.add(variable, new DoubleConstValueSource(((Number) value).doubleValue()).asDoubleValuesSource()); } else { throw new ParseException("Parameter [" + variable + "] must be a numeric type", 0); } } else { String fieldname = null; String methodname = null; String variablename = "value"; // .value is the default for doc['field'], its optional. boolean dateAccessor = false; // true if the variable is of type doc['field'].date.xxx VariableContext[] parts = VariableContext.parse(variable); if (parts[0].text.equals("doc") == false) { throw new ParseException("Unknown variable [" + parts[0].text + "]", 0); } if (parts.length < 2 || parts[1].type != VariableContext.Type.STR_INDEX) { throw new ParseException( "Variable 'doc' must be used with a specific field like: doc['myfield']", 3); } else { fieldname = parts[1].text; } if (parts.length == 3) { if (parts[2].type == VariableContext.Type.METHOD) { methodname = parts[2].text; } else if (parts[2].type == VariableContext.Type.MEMBER) { variablename = parts[2].text; } else { throw new IllegalArgumentException( "Only member variables or member methods may be accessed on a field when not accessing the field directly"); } } if (parts.length > 3) { // access to the .date "object" within the field if (parts.length == 4 && ("date".equals(parts[2].text) || "getDate".equals(parts[2].text))) { if (parts[3].type == VariableContext.Type.METHOD) { methodname = parts[3].text; dateAccessor = true; } else if (parts[3].type == VariableContext.Type.MEMBER) { variablename = parts[3].text; dateAccessor = true; } } if (!dateAccessor) { throw new IllegalArgumentException("Variable [" + variable + "] does not follow an allowed format of either doc['field'] or doc['field'].method()"); } } MappedFieldType fieldType = mapper.fullName(fieldname); if (fieldType == null) { throw new ParseException("Field [" + fieldname + "] does not exist in mappings", 5); } IndexFieldData<?> fieldData = lookup.doc().getForField(fieldType); // delegate valuesource creation based on field's type // there are three types of "fields" to expressions, and each one has a different "api" of variables and methods. final ValueSource valueSource; if (fieldType instanceof GeoPointFieldType) { // geo if (methodname == null) { valueSource = GeoField.getVariable(fieldData, fieldname, variablename); } else { valueSource = GeoField.getMethod(fieldData, fieldname, methodname); } } else if (fieldType instanceof DateFieldMapper.DateFieldType) { if (dateAccessor) { // date object if (methodname == null) { valueSource = DateObject.getVariable(fieldData, fieldname, variablename); } else { valueSource = DateObject.getMethod(fieldData, fieldname, methodname); } } else { // date field itself if (methodname == null) { valueSource = DateField.getVariable(fieldData, fieldname, variablename); } else { valueSource = DateField.getMethod(fieldData, fieldname, methodname); } } } else if (fieldData instanceof IndexNumericFieldData) { // number if (methodname == null) { valueSource = NumericField.getVariable(fieldData, fieldname, variablename); } else { valueSource = NumericField.getMethod(fieldData, fieldname, methodname); } } else { throw new ParseException("Field [" + fieldname + "] must be numeric, date, or geopoint", 5); } needsScores |= valueSource.getSortField(false).needsScores(); bindings.add(variable, valueSource.asDoubleValuesSource()); } } catch (Exception e) { // we defer "binding" of variables until here: give context for that variable throw convertToScriptException("link error", expr.sourceText, variable, e); } } return new ExpressionSearchScript(expr, bindings, specialValue, needsScores); }