java客户端中的elasticsearch geohash facet

时间:2014-01-23 13:07:49

标签: java elasticsearch elasticsearch-plugin

我发现this amazing plugin用于在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();

1 个答案:

答案 0 :(得分:0)

我正在使用您提供的链接中提到的this plugin。这是一种构建其方面的方法,它完美地运作。

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);