搜索排序分页的数据inside _source在一个单一的文档中通过它的ElasticSearch查询

djmepvbi  于 2022-11-02  发布在  ElasticSearch
关注(0)|答案(1)|浏览(121)

在ElasticSearch索引中,我们有一个多文档,但其中一个文档与其他文档不同。它包含其他文档所没有的供应商产品Map,因此它是索引中的同类文档之一。如下所示:

{
      "_index": "portal_support_20200911",
      "_type": "_doc",
      "_id": "techno_products",
      "_version": 20220829,
      "_seq_no": 39,
      "_primary_term": 1,
      "found": true,
      "_source": {
        "doc_name": "techno_products",
        "updated_on": "20220829",
        "products": [
          {
            "vendor": "Apple",
            "product": "Iphone"
          },
          {
            "vendor": "Samsung",
            "product": "Galaxy Z"
          },
          {
            "vendor": "Volkswagen",
            "product": "Passat"
          },
          {
            "vendor": "Volkswagen",
            "product": "Tiguan"
          }
        ]
      }
    }

它在“products”数组中有数千个供应商产品Map。我们的要求是编写一个ElasticSearch查询来获取此文档的数据,但我们还希望通过ES查询来搜索、排序和分页此“products”数组的元素。这意味着我们希望通过ES查询来搜索、排序和分页此ES文档的源。
例如,按“vendor”或“product”对“products”数组进行降序排序。由于该数组有数千个条目,因此我们希望分页并一次获得100个元素,然后在下一页中获得下一个100个元素,以此类推。我们还希望提供搜索选项,如vendor=Volkswagen,因此它只在ElasticSearch查询的输出中提供匹配的元素。
我是ES的新手,但根据我的知识,我们可以在ES中按字段搜索、排序和分页文档。
但是我们是否也可以在ElasticSearch中搜索、排序和分页一个文档的数据inside _source?我如何使用ElasticSearch查询来实现这一点?
请帮我这个谢谢。

wfsdck30

wfsdck301#

我想你是在找inner hits吧?
如果有嵌套查询,可以添加inner_hits作为选项:

{
  nested: {
    path: "products",
    query: {
      term: {
        "field": "term"
      }
    },
    inner_hits: {
      size: 10000,
      sort: [
        {
          "products.product": "desc",
        },
      ]
    }
  }
}

如果您从未使用嵌套查询来检索文档,我认为您可以添加嵌套查询,并在嵌套查询中使用match_all: {}

{
  nested: {
    path: "products",
    query: {
      match_all: {}
    },
    inner_hits: {
      size: 10000,
      sort: [
        {
          "products.product": "desc",
        },
      ]
    }
  }
}

您可以在inner_hits中使用from来翻阅结果。
我希望这能帮上忙。

相关问题