List of usage examples for org.apache.lucene.spatial.prefix PrefixTreeStrategy makeQuery
public abstract Query makeQuery(SpatialArgs args);
From source file:org.elasticsearch.index.query.GeoShapeQueryBuilder.java
License:Apache License
@Override protected Query doToQuery(QueryShardContext context) { if (shape == null) { throw new UnsupportedOperationException("query must be rewritten first"); }/*www .java 2s . c o m*/ final ShapeBuilder shapeToQuery = shape; final MappedFieldType fieldType = context.fieldMapper(fieldName); if (fieldType == null) { if (ignoreUnmapped) { return new MatchNoDocsQuery(); } else { throw new QueryShardException(context, "failed to find geo_shape field [" + fieldName + "]"); } } // TODO: This isn't the nicest way to check this if (!(fieldType instanceof GeoShapeFieldMapper.GeoShapeFieldType)) { throw new QueryShardException(context, "Field [" + fieldName + "] is not a geo_shape"); } final GeoShapeFieldMapper.GeoShapeFieldType shapeFieldType = (GeoShapeFieldMapper.GeoShapeFieldType) fieldType; PrefixTreeStrategy strategy = shapeFieldType.defaultStrategy(); if (this.strategy != null) { strategy = shapeFieldType.resolveStrategy(this.strategy); } Query query; if (strategy instanceof RecursivePrefixTreeStrategy && relation == ShapeRelation.DISJOINT) { // this strategy doesn't support disjoint anymore: but it did // before, including creating lucene fieldcache (!) // in this case, execute disjoint as exists && !intersects BooleanQuery.Builder bool = new BooleanQuery.Builder(); Query exists = ExistsQueryBuilder.newFilter(context, fieldName); Query intersects = strategy.makeQuery(getArgs(shapeToQuery, ShapeRelation.INTERSECTS)); bool.add(exists, BooleanClause.Occur.MUST); bool.add(intersects, BooleanClause.Occur.MUST_NOT); query = new ConstantScoreQuery(bool.build()); } else { query = new ConstantScoreQuery(strategy.makeQuery(getArgs(shapeToQuery, relation))); } return query; }
From source file:org.elasticsearch.index.query.GeoShapeQueryParser.java
License:Apache License
@Override public Query parse(QueryParseContext parseContext) throws IOException, QueryParsingException { XContentParser parser = parseContext.parser(); String fieldName = null;/*w w w . j av a2 s . c o m*/ ShapeRelation shapeRelation = ShapeRelation.INTERSECTS; String strategyName = null; ShapeBuilder shape = null; String id = null; String type = null; String index = DEFAULTS.INDEX_NAME; String shapePath = DEFAULTS.SHAPE_FIELD_NAME; XContentParser.Token token; String currentFieldName = null; float boost = 1f; String queryName = null; while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) { if (token == XContentParser.Token.FIELD_NAME) { currentFieldName = parser.currentName(); } else if (token == XContentParser.Token.START_OBJECT) { fieldName = currentFieldName; while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) { if (token == XContentParser.Token.FIELD_NAME) { currentFieldName = parser.currentName(); token = parser.nextToken(); if ("shape".equals(currentFieldName)) { shape = ShapeBuilder.parse(parser); } else if ("strategy".equals(currentFieldName)) { strategyName = parser.text(); } else if ("relation".equals(currentFieldName)) { shapeRelation = ShapeRelation.getRelationByName(parser.text()); if (shapeRelation == null) { throw new QueryParsingException(parseContext.index(), "Unknown shape operation [" + parser.text() + " ]"); } } else if ("indexed_shape".equals(currentFieldName) || "indexedShape".equals(currentFieldName)) { while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) { if (token == XContentParser.Token.FIELD_NAME) { currentFieldName = parser.currentName(); } else if (token.isValue()) { if ("id".equals(currentFieldName)) { id = parser.text(); } else if ("type".equals(currentFieldName)) { type = parser.text(); } else if ("index".equals(currentFieldName)) { index = parser.text(); } else if ("path".equals(currentFieldName)) { shapePath = parser.text(); } } } if (id == null) { throw new QueryParsingException(parseContext.index(), "ID for indexed shape not provided"); } else if (type == null) { throw new QueryParsingException(parseContext.index(), "Type for indexed shape not provided"); } shape = fetchService.fetch(id, type, index, shapePath); } else { throw new QueryParsingException(parseContext.index(), "[geo_shape] query does not support [" + currentFieldName + "]"); } } } } else if (token.isValue()) { if ("boost".equals(currentFieldName)) { boost = parser.floatValue(); } else if ("_name".equals(currentFieldName)) { queryName = parser.text(); } else { throw new QueryParsingException(parseContext.index(), "[geo_shape] query does not support [" + currentFieldName + "]"); } } } if (shape == null) { throw new QueryParsingException(parseContext.index(), "No Shape defined"); } else if (shapeRelation == null) { throw new QueryParsingException(parseContext.index(), "No Shape Relation defined"); } MapperService.SmartNameFieldMappers smartNameFieldMappers = parseContext.smartFieldMappers(fieldName); if (smartNameFieldMappers == null || !smartNameFieldMappers.hasMapper()) { throw new QueryParsingException(parseContext.index(), "Failed to find geo_shape field [" + fieldName + "]"); } FieldMapper fieldMapper = smartNameFieldMappers.mapper(); // TODO: This isn't the nicest way to check this if (!(fieldMapper instanceof GeoShapeFieldMapper)) { throw new QueryParsingException(parseContext.index(), "Field [" + fieldName + "] is not a geo_shape"); } GeoShapeFieldMapper shapeFieldMapper = (GeoShapeFieldMapper) fieldMapper; PrefixTreeStrategy strategy = shapeFieldMapper.defaultStrategy(); if (strategyName != null) { strategy = shapeFieldMapper.resolveStrategy(strategyName); } Query query = strategy.makeQuery(getArgs(shape, shapeRelation)); query.setBoost(boost); if (queryName != null) { parseContext.addNamedQuery(queryName, query); } return query; }
From source file:org.esa.beam.occci.LuceneQueryIndexMain.java
License:Open Source License
public static void main(String[] args) throws Exception { if (args.length != 3) { printUsage();//w ww .j ava 2s .co m } File indexfile = new File(args[0]); File insituCSVtFile = new File(args[1]); if (!insituCSVtFile.exists()) { System.err.printf("insituList file '%s' does not exits%n", args[2]); printUsage(); } int hours = 0; try { hours = Integer.parseInt(args[2]); } catch (NumberFormatException e) { e.printStackTrace(); System.err.printf("cannot parse hours '%s' %n", args[3]); printUsage(); } long maxTimeDifference = HOURS_IN_MILLIS * hours; final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS", ENGLISH); dateFormat.setCalendar(GregorianCalendar.getInstance(UTC, Locale.ENGLISH)); List<SimpleRecord> insituRecords = ProductDBCheckerMain.readInsituRecords(insituCSVtFile); System.out.println("num insituRecords = " + insituRecords.size()); Directory indexDirectory = FSDirectory.open(indexfile.toPath()); IndexReader indexReader = DirectoryReader.open(indexDirectory); int numProductsInIndex = indexReader.getDocCount("name"); System.out.println("numProductsInIndex = " + numProductsInIndex); IndexSearcher indexSearcher = new IndexSearcher(indexReader); DateRangePrefixTree dateRangePrefixTree = DateRangePrefixTree.INSTANCE; PrefixTreeStrategy strategy = new NumberRangePrefixTreeStrategy(dateRangePrefixTree, "productDateRange"); SpatialOperation operation = SpatialOperation.Intersects; int hits = 0; long t1 = System.currentTimeMillis(); Set<Integer> matches = new HashSet<>(); Calendar calendar = dateRangePrefixTree.newCal(); for (SimpleRecord insituRecord : insituRecords) { final long referenceTime = insituRecord.getTime(); final long windowStartTime = referenceTime - maxTimeDifference; final long windowEndTime = referenceTime + maxTimeDifference; calendar.setTimeInMillis(windowStartTime); NumberRangePrefixTree.UnitNRShape leftShape = dateRangePrefixTree.toShape(calendar); calendar.setTimeInMillis(windowEndTime); NumberRangePrefixTree.UnitNRShape rightShape = dateRangePrefixTree.toShape(calendar); NumberRangePrefixTree.NRShape nrShape = dateRangePrefixTree.toRangeShape(leftShape, rightShape); SpatialArgs sargs = new SpatialArgs(operation, nrShape); Query query = strategy.makeQuery(sargs); TopDocs topDocs = indexSearcher.search(query, 1000); ScoreDoc[] scoreDocs = topDocs.scoreDocs; for (ScoreDoc scoreDoc : scoreDocs) { matches.add(scoreDoc.doc); } // Document doc = indexSearcher.doc(docID); // String productName = doc.get("name"); // matches.add(productName); // } // System.out.println("topDocs.totalHits = " + topDocs.totalHits); // hits += topDocs.totalHits; } long t2 = System.currentTimeMillis(); System.out.println("delta time test insitu = " + ((t2 - t1) / 1000f)); System.out.println("hits = " + hits); System.out.println("matches = " + matches.size()); }