Marek Spányik
Marek Spányik

Reputation: 31

ElasticSearch Search for Field Values does not return

Lets say I have document in the Elastic Search which does containt field "provider" in the _source.

I have tryied many queries but none of them seem to return the document with searched value.

Doc:

"_source" : {
      "jobs" : [ ],
      "provider" : {
        "id" : "1",
        "name" : "Coursera"
      },
      "sckLevels" : [ ],
      "scks" : [ ],
      "trArea" : [ ],
      "trElems" : [ ],
      "training" : {
        "description" : "Cyber sec desc",
        "id" : "0",
        "img" : "img link",
        "link" : "https://google.com",
        "name" : "Cyber sec",
        "trainingProvID" : "1"
      }

And my code for the query is:

SearchRequest searchRequest = new SearchRequest(index);
    SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
    sourceBuilder.query(QueryBuilders.termQuery("provider", "Coursera"));
    searchRequest.source(sourceBuilder);
this.multiRequest.add(searchRequest);

My response is blank.

Thank you.

Upvotes: 0

Views: 513

Answers (2)

Marek Spányik
Marek Spányik

Reputation: 31

thank you, nesting was the problem. I thought that it will do the search on the object.

Came across good thread.

Solved it like this:

SearchRequest searchRequest = new SearchRequest(index);
    SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
    sourceBuilder.query(QueryBuilders.matchQuery("provider.name", "Coursera"));
    searchRequest.source(sourceBuilder);

    this.multiRequest.add(searchRequest);

Upvotes: 1

Amit
Amit

Reputation: 32386

There is definitely few issues with your Elasticsearch query

  1. Seems provider field is of object or nested type, while in your query you are just mentioning Coursera but it should be matched against the name subfield of provider field and based on object or nested data type, you need to modify your query.

  2. You are using the term query which is not analyzed and used for keyword ie extact match while if your name field is defined as text it would be lowercased at index time and Coursera with captial C won't match, you need to use the match query on text fields.

Upvotes: 2

Related Questions