如何在java中构建elasticsearch术语查询?

时间:2016-07-27 05:04:20

标签: java elasticsearch

如何在Java中使用elasticsearch terms查询以实现以下目的:

curl -XGET localhost:9200/tweets/_search -d '{
  "query" : {
    "terms" : {
      "user" : {
        "index" : "users",
        "type" : "user",
        "id" : "2",
        "path" : "followers"
      }
    }
  }
}'

2 个答案:

答案 0 :(得分:3)

你可以在ES 2.3中这样做:

TermsLookupQueryBuilder terms = QueryBuilders.termsLookupQuery("user")
    .lookupIndex("users")
    .lookupType("user")
    .lookupId("2")
    .lookupPath("followers");

client.prepareSearch("tweets")
    .setQuery(terms)
    .execute().actionGet();

在ES 5中,您可以这样做:

TermsLookup termsLookup = new TermsLookup("users", "user", "2", "followers");
TermsQueryBuilder terms = QueryBuilders.termsLookupQuery("user", termsLookup);

client.prepareSearch("tweets")
    .setQuery(terms)
    .execute().actionGet();

答案 1 :(得分:0)

我经过艰难的努力找到了答案:

TermsLookupQueryBuilder tlqb = QueryBuilders
             .termsLookupQuery("user")
             .lookupIndex("users")
             .lookupType("user")
             .lookupId("2")
             .lookupPath("followers");