0

I have an elastic search index which is storing documents in the following way:

    { 

  categorisedTags: 
   { urlTags: { L: [] },
     commodityTags: { L: [Array] },
     tags: { L: [] } },
  newOptions: [],
  created_at: 'Mon, 07 Oct 2019 12:55:34 GMT',
  name: 'Template ',
  }

I need to query the index by 'commodityTags', so given a string, it should return all documents where the string is included in the commodityTags array.

I have tried with:

service.queryTags = async (index, values) => {
    const { hits } = await esClient.search({
      index,
      type: '_doc',
      body: {
        query: {
          term: {
            'categorisedTags.commodityTags': 'oil'
          }
        },
      },
    });
    return hits.hits.map(({ _source }) => _source);
  };

But no luck, always returns 0 hits. How can I do this kind of nested queries on ES ?

1 Answer 1

1

Nested query can be created like below Query

"query": {
    "nested": {
      "path": "categorisedTags",
      "query": {
        "bool": {
         "must": [
           {
             "term": {
               "categorisedTags.commodityTags": {
                 "value": "oil"
               }
             }
           }
         ]
        }
      },
      "inner_hits": {}
    }
  }
Sign up to request clarification or add additional context in comments.

Comments

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.