1

How to use elasticsearch terms query in Java in order to achieve the following:

curl -XGET localhost:9200/tweets/_search -d '{
  "query" : {
    "terms" : {
      "user" : {
        "index" : "users",
        "type" : "user",
        "id" : "2",
        "path" : "followers"
      }
    }
  }
}'
1
  • 1
    You might want to add the java tag if you are interested in java answers. Also, please provide a bit more details about what you are trying to do and in which way it doesn't work as you expected. Commented Jul 27, 2016 at 5:07

2 Answers 2

3

You can do it like this in ES 2.3:

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

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

In ES 5, you'll be able to do it like this:

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

client.prepareSearch("tweets")
    .setQuery(terms)
    .execute().actionGet();
Sign up to request clarification or add additional context in comments.

5 Comments

Thank you very much.I am using Elasticsearch 2.3.3, there are 5 parameters: TermsLookup termsLookup = new TermsLookup(index, type, id, routing, path, queryParseContext),what path and queryParseContext should be in this case?
I tried,it does not work. Api changes :elastic.co/guide/en/elasticsearch/reference/master/…
I've modified my answer, please check it again.
can you help me solve this problem?stackoverflow.com/questions/38948461/… @Val
0

I found the answer after a hard way:

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

2 Comments

How is this different from my answer?
No worries, glad you figured it out!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.