1

我发现了这个很棒的插件,用于在 elasticsearch 中创建基于 geohash 的构面。它似乎在 _head pluing 中工作得很好。我只是不太确定如何从 JavaClient 代码运行它。

我编写了代码来运行匹配查询,但不确定如何向其中添加 geohash 过滤器。

{
  "query": {
    "match_all": {}
  },
  "facets": {
    "places": {
      "geohash": {
        "field": "location",
        "factor": 0.9
      }
    }
  }
}

我认为以下内容会有所帮助,但无济于事。它抛出

Exception in thread "main" org.elasticsearch.action.search.SearchPhaseExecutionException: Failed to execute phase [query], all shards failed; shardFailures {[IokTP1RuQ520T86dxU345w][easythahr][1]: SearchParseException[[easythahr][1]: from[0],size[100]: Parse Failure [Failed to parse source [{"from":0,"size":100,"facets_binary":"InBsYWNlcyJ7ImZhY2V0cyI6eyJnZW9IYXNoIjp7ImZpZWxkIjoibG9jYXRpb24iLCJmYWN0b3IiOjAuOX19fQ=="}]]]; nested: SearchParseException[[easythahr][1]: from[0],size[100]: Parse Failure [No facet type found for [facets]]]; }{[IokTP1RuQ520T86dxU345w][easythahr][2]: SearchParseException[[easythahr][2]: from[0],size[100]: Parse Failure [Failed to parse source [{"from":0,"size":100,"facets_binary":"InBsYWNlcyJ7ImZhY2V0cyI6eyJnZW9IYXNoIjp7ImZpZWxkIjoibG9jYXRpb24iLCJmYWN0b3IiOjAuOX19fQ=="}]]]; nested: SearchParseException[[easythahr][2]: from[0],size[100]: Parse Failure [No facet type found for [facets]]]; }{[IokTP1RuQ520T86dxU345w][easythahr][3]: SearchParseException[[easythahr][3]: from[0],size[100]: Parse Failure [Failed to parse source [{"from":0,"size":100,"facets_binary":"InBsYWNlcyJ7ImZhY2V0cyI6eyJnZW9IYXNoIjp7ImZpZWxkIjoibG9jYXRpb24iLCJmYWN0b3IiOjAuOX19fQ=="}]]]; nested: SearchParseException[[easythahr][3]: from[0],size[100]: Parse Failure [No facet type found for [facets]]]; }{[IokTP1RuQ520T86dxU345w][easythahr][4]: SearchParseException[[easythahr][4]: from[0],size[100]: Parse Failure [Failed to parse source [{"from":0,"size":100,"facets_binary":"InBsYWNlcyJ7ImZhY2V0cyI6eyJnZW9IYXNoIjp7ImZpZWxkIjoibG9jYXRpb24iLCJmYWN0b3IiOjAuOX19fQ=="}]]]; nested: SearchParseException[[easythahr][4]: from[0],size[100]: Parse Failure [No facet type found for [facets]]]; }{[IokTP1RuQ520T86dxU345w][easythahr][0]: SearchParseException[[easythahr][0]: from[0],size[100]: Parse Failure [Failed to parse source [{"from":0,"size":100,"facets_binary":"InBsYWNlcyJ7ImZhY2V0cyI6eyJnZW9IYXNoIjp7ImZpZWxkIjoibG9jYXRpb24iLCJmYWN0b3IiOjAuOX19fQ=="}]]]; nested: SearchParseException[[easythahr][0]: from[0],size[100]: Parse Failure [No facet type found for [facets]]]; }

XContentBuilder b = jsonBuilder().startObject("places")
                .startObject("geoHash")
                .field("field", "location")
                .field("factor",0.9)
                .endObject()
                .endObject();



        SearchResponse response = client.prepareSearch("easythahr")
                .setTypes("com.easytha.Student")
                .setQuery(matchQuery)
                .setFacets(b)
                .setFrom(0)
                .setSize(100)
                .execute()
                .actionGet();
4

1 回答 1

0

我正在使用您提供的链接中也提到的这个插件。这是一种构建其方面的方法,并且效果很好。

XContentBuilder b = null;
try {
    b = jsonBuilder().startObject().
                    startObject("places")
                        .startObject("geo_cluster")
                            .field("field", Field.geoLocation)
                            .field("factor",0.9)
                        .endObject()
                    .endObject()
                .endObject();
} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

InternalGeoClusterFacet.registerStreams();


SearchRequestBuilder srb = client.prepareSearch("indexname")
        .setTypes("indextype")
        .setQuery(QueryBuilders.matchAllQuery())
        .setFacets(b)
        .setFrom(0)
        .setSize(100);

SearchResponse response = srb.execute().actionGet();

第二种方式: 将jar文件添加到buildpath之后,您可以使用以下。

b = new GeoClusterFacetBuilder(facetName, facetField, facetFactor);
于 2014-01-24T09:26:29.733 回答