@Override public List<Property> findPropertiesByLocation(final GeoPoint geoPoint, Double distance, final SortOrder sortOrder) { GeoDistanceFilterBuilder filter = FilterBuilders.geoDistanceFilter("location").point(geoPoint.getLat(), geoPoint.getLon()) .distance(distance, DistanceUnit.KILOMETERS); SearchQuery searchQuery = new NativeSearchQueryBuilder() .withFilter(filter) .withSort( SortBuilders.geoDistanceSort("location").point(geoPoint.getLat(), geoPoint.getLon()) .order(sortOrder == null ? SortOrder.ASC : sortOrder)).build(); searchQuery.addIndices("searchahouse"); searchQuery.addTypes("property"); List<Property> properties = this.elasticsearchOperations.queryForList(searchQuery, Property.class); return properties; }
@Test public void searchWithGeoFilter() { running(esFakeApplication(), new Runnable() { @Override public void run() { GeoPoint location = new GeoPoint(30.6943566,-88.0430541); Index1Type1 index1Type1 = new Index1Type1("1", "name1", "category", createDate(), location); index1Type1.index(); location = new GeoPoint(12.6943566,-10.0430541); Index1Type1 index1Type2 = new Index1Type1("2", "name1", "category", createDate(), location); index1Type2.index(); // refresh the index so the documents appear in the search results IndexService.refresh(); assertThat(Index1Type1.find.byId("1").name).isEqualTo("name1"); assertThat(Index1Type1.find.byId("2").name).isEqualTo("name1"); IndexQuery<Index1Type1> query = Index1Type1.find.query(); query.setBuilder(QueryBuilders.queryString("name1")); GeoDistanceFilterBuilder filter = FilterBuilders.geoDistanceFilter("type1.location") .point(30, -88) .distance(100, DistanceUnit.KILOMETERS); F.Promise<IndexResults<Index1Type1>> indexResultsPromise = Index1Type1.find.searchAsync(query, filter); IndexResults<Index1Type1> index1Type1IndexResults = indexResultsPromise.get(2L, TimeUnit.SECONDS); assertThat(index1Type1IndexResults.totalCount).isEqualTo(1); } }); }
public ElasticSearchPlaceQuery geoDistanceSearch(double lon, double lat, int distance) { queryBuilder = QueryBuilders.matchAllQuery(); GeoDistanceFilterBuilder filterBuilder = FilterBuilders.geoDistanceFilter("prefLocation.coordinates"); filterBuilder.distance(Integer.toString(distance) + "km"); filterBuilder.point(lat, lon); requestBuilder.setPostFilter(filterBuilder); return this; }
@Test public void testAddPercolator() throws Exception { // Prepare: Setup Thrift Client and add some docs populateWithTestDocs(); final FilterBuilder geoDistanceFilter = new GeoDistanceFilterBuilder(TEST_TYPE + ".location").distance(10, DistanceUnit.KILOMETERS) .lat(whiteHouse.getLocation().getLat()).lon(whiteHouse.getLocation().getLon()); final QueryBuilder filteredQuery = new FilteredQueryBuilder(QueryBuilders.matchAllQuery(), geoDistanceFilter); final PercolateQuery percolator = new PercolateQuery(); percolator.setVisibility(new Visibility().setFormalVisibility("U")); percolator.setId(UUID.randomUUID().toString()); final String queryDoc = jsonBuilder().startObject().field("query", filteredQuery).endObject().string(); percolator.setQueryDocument(queryDoc); // Action: Create and execute an OR match query on title final Document doc = new Document( ElasticUtils.PERCOLATOR_TYPE, new Visibility().setFormalVisibility("U"), percolator.getQueryDocument()); final IndexResponse percolateResponse = client.put(Collections.singletonList(doc)).get(0); assertTrue(percolateResponse.isSuccess()); // Assert: There should be 2 matching documents oneaaNavyYardDoc.setPercolate(new PercolateRequest()); oneaaColumbiaDoc.setPercolate(new PercolateRequest()); whiteHouseDoc.setPercolate(new PercolateRequest()); washingtonMonumentDoc.setPercolate(new PercolateRequest()); final List<PercolateQuery> matches = client.percolate( Arrays.asList(oneaaNavyYardDoc, oneaaColumbiaDoc, whiteHouseDoc, washingtonMonumentDoc)); assertEquals(3, matches.size()); }
@Test public void testPercolator() throws Exception { // Prepare: Setup Thrift Client and add some docs client = pool.getClient(SERVICE_NAME, EzElastic.Client.class); client.setTypeMapping(TEST_TYPE, getMappingForTest(), fakeSecurity); client.bulkPut( Arrays.asList(washingtonMonumentDoc, whiteHouseDoc, oneaaColumbiaDoc, oneaaNavyYardDoc), fakeSecurity); final FilterBuilder geoDistanceFilter = new GeoDistanceFilterBuilder(TEST_TYPE + ".location").distance(10, DistanceUnit.KILOMETERS) .lat(whiteHouse.getLocation().getLat()).lon(whiteHouse.getLocation().getLon()); final QueryBuilder filteredQuery = new FilteredQueryBuilder(QueryBuilders.matchAllQuery(), geoDistanceFilter); final PercolateQuery percolator = new PercolateQuery(); percolator.setVisibility(new Visibility().setFormalVisibility("U")); percolator.setId(UUID.randomUUID().toString()); final String queryDoc = jsonBuilder().startObject().field("query", filteredQuery).endObject().string(); percolator.setQueryDocument(queryDoc); // Action: Create and execute an OR match query on title final IndexResponse percolateResponse = client.addPercolateQuery(percolator, fakeSecurity); assertTrue(percolateResponse.isSuccess()); // Assert: There should be 3 matching documents oneaaNavyYardDoc.setPercolate(new PercolateRequest()); oneaaColumbiaDoc.setPercolate(new PercolateRequest()); whiteHouseDoc.setPercolate(new PercolateRequest()); washingtonMonumentDoc.setPercolate(new PercolateRequest()); final List<PercolateQuery> matches = client.percolate( Arrays.asList( oneaaNavyYardDoc, oneaaColumbiaDoc, whiteHouseDoc, washingtonMonumentDoc), fakeSecurity); assertEquals(3, matches.size()); pool.returnToPool(client); }
@Override public FilterBuilder convert(Filter filter) { String field = filter.getString("field"); String distance = filter.getString("distance"); String optimizeBbox = filter.getString("optimizeBbox"); Geometry geometry = filter.get(Geometry.class, "geometry"); Coordinate coord = geometry.getCentroid().getCoordinate(); GeoDistanceFilterBuilder result = FilterBuilders.geoDistanceFilter(field) .lon(coord.x).lat(coord.y) .distance(distance); if (optimizeBbox != null) { result.optimizeBbox("indexed"); } return result; }