简体   繁体   English

ElasticSearch 上的嵌套查询

[英]Nested query on ElasticSearch

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.我需要通过'commodityTags'查询索引,所以给定一个字符串,它应该返回该字符串包含在commodityTags数组中的所有文档。

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.但没有运气,总是返回 0 次点击。 How can I do this kind of nested queries on ES?如何在 ES 上进行这种嵌套查询?

Nested query can be created like below Query可以像下面这样创建嵌套查询

"query": {
    "nested": {
      "path": "categorisedTags",
      "query": {
        "bool": {
         "must": [
           {
             "term": {
               "categorisedTags.commodityTags": {
                 "value": "oil"
               }
             }
           }
         ]
        }
      },
      "inner_hits": {}
    }
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM