如何在Elasticsearch中的geo_polygon查询中传递多边形点?

时间:2016-06-29 09:23:08

标签: java arrays elasticsearch elasticsearch-2.0

多边形积分

13.049901579550138,77.60384321849386
13.050271501523547,77.6000084075878
13.042883570605907,77.60009104570553
13.042505842873242,77.60865383263827
13.039972708604349,77.6169725155289
13.04006230763758,77.62306611131774
13.036301472612582,77.62675523932252
13.027836648748504,77.63111238425978
13.026664014408041,77.63260862851725
13.02476188706518,77.63997438672054
13.01580550477802,77.66057241738156
12.994606646225883,77.66614853601016
12.99922317402202,77.67626586171559
12.999248673711989,77.6806137299943
12.989982441225264,77.68721899668857
12.967414209806904,77.700928481046
12.965565353801034,77.70119657236678
12.956563533462424,77.70009555907806
12.948645461168596,77.69833987361152
12.943956894161838,77.69679408243776
12.939778114785996,77.69436134214627
12.931071740988296,77.68511223031385
12.921149580164533,77.66436305248996
12.917680671328007,77.66871057636764
12.920113294517337,77.66953194946153
12.920753105482662,77.66763705053846
12.921023128671994,77.66772822363235
12.920722219835467,77.66810534751005
12.929401859011703,77.68625616968615
12.938520885214002,77.69594365785375
12.94313170583816,77.69862791756223
12.948114538831405,77.7002707263885
12.956224866537577,77.70206904092193
12.965587846198966,77.70321422763321
12.968104390193098,77.702849318954
12.991083558774735,77.68889020331143
13.00125472628801,77.6816398700057
13.001220625977979,77.67582553828441
12.997411953774119,77.66747866398984
13.017252695221979,77.66225978261843
13.026658912934819,77.64062721327947
13.02849598559196,77.63351337148275
13.029140951251495,77.63269041574023
13.037485927387419,77.62839496067748
13.04207469236242,77.62389368868227
13.041977091395651,77.6172558844711
13.04449275712676,77.60899456736173
13.044798229394091,77.60206975429448
13.048066898476453,77.60203319241221
13.04791082044986,77.60365118150614 

我将这个多边形作为数组传递给Elasticsearch。

在ES中,我们有geo_polygon查询。下面我写了一个部分方法。

部分查询

public VendorSearchResult searchVendors(String query, Geopoint[] polygon, CostType... costtypes) {

        try {
            query = URLDecoder.decode(query, "UTF-8").replace(",", " ");
        } catch (Exception e1) {
            query = "";
        }

        BoolQueryBuilder bqBuilder = new BoolQueryBuilder();
        bqBuilder.must(new TypeQueryBuilder(VENDOR_INDEXTYPE));     
        if (!StringUtils.isEmpty(query)){
            /*
             * add query string to BoolQuery
             */
            bqBuilder.must(QueryBuilders.multiMatchQuery(query, VENDOR_SEARCHED_FIELDS).type(Type.PHRASE_PREFIX));
        }
        bqBuilder.must(QueryBuilders.geoPolygonQuery("address.geopoint")
         .addpoint().....); /*how do I pass all those points like addpoint(,); ?*/




        return null;
    }

有人可以指导我如何使用某个循环/任何其他

添加多个点

更新

public VendorSearchResult searchVendors(String query, List<GeoPoint> polygon, CostType... costtypes) {

        try {
            query = URLDecoder.decode(query, "UTF-8").replace(",", " ");
        } catch (Exception e1) {
            query = "";
        }

        BoolQueryBuilder bqBuilder = new BoolQueryBuilder();
        bqBuilder.must(new TypeQueryBuilder(VENDOR_INDEXTYPE));     
        if (!StringUtils.isEmpty(query)){
            /*
            * add query string to BoolQuery
            */
            bqBuilder.must(QueryBuilders.multiMatchQuery(query, VENDOR_SEARCHED_FIELDS).type(Type.PHRASE_PREFIX));
        }
        /*
        * iterate over polygon to add each point to the query
        */
        GeoPolygonQueryBuilder gpqb = QueryBuilders.geoPolygonQuery("address.geopoint");
        for (GeoPoint gp : polygon) {
            if(String.valueOf(gp.getLat()).equals("NaN")){

            }else{
                gpqb.addPoint(gp.getLat(), gp.getLon());
            }
        }
        bqBuilder.must(gpqb);
        /*
        * add vendortype to query, if provided
        */
        if ( costtypes != null && costtypes.length > 0 ) {
            for (CostType costType : costtypes) {
                bqBuilder.should(QueryBuilders.termQuery("vendortype", costType.name().toLowerCase()));
            }
            bqBuilder.minimumNumberShouldMatch(1);
        }

        VendorSearchResult sr = elasticSearch(bqBuilder, SORTFIELD, SortOrder.ASC);
        return sr;
    }

1 个答案:

答案 0 :(得分:0)

对于ES 2.x,有一个示例online at

GeoPolygonQueryBuilder gpqb = QueryBuilders.geoPolygonQuery("address.geopoint");       
// iterate over your polygon
for (Geopoint gp : polygon) {
    gpqb.addPoint(gp.lat(), gp.lon());
}

从ES 5开始,您将能够as shown here

import org.elasticsearch.index.search.geo.GeoPoint;

List<GeoPoint> points = new ArrayList<GeoPoint>();
// iterate over your polygon
for (Geopoint gp : polygon) {
    points.add(new GeoPoint(gp.lat(), gp.lon()));
}

QueryBuilder qb = QueryBuilders.geoPolygonQuery("address.geopoint", points);