简体   繁体   English

搜索排序通过它查询在弹性搜索中对单个文档的_source内的数据进行分页

[英]Search sort paginate the data inside _source of a single document in elastic search through it query

In an Elastic Search index we have a multiple document but one document is different from others.在 Elastic Search 索引中,我们有多个文档,但一个文档与其他文档不同。 It contains vendor product mapping which no other document has, so it is one of its kind in the index.它包含其他文档没有的供应商产品映射,因此它是索引中的同类之一。 Like this:像这样:

    {
      "_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"
          }
        ]
      }
    }

It has thousands of vendor product mapping in "products" array.它在“产品”数组中有数千个供应商产品映射。 Our requirement is to write an Elastic Search query to get the data of this document but we also want to search, sort and paginate elements of this "products" array through ES query.我们的要求是编写一个 Elastic Search 查询来获取该文档的数据,但我们还想通过 ES 查询对这个“产品”数组的元素进行搜索、排序和分页。 Means we want to search, sort and paginate _source of this ES Document through ES query.表示我们要通过 ES 查询对这个 ES Document 的 _source 进行搜索、排序和分页。

For example sort the "products" array by "vendor" or by "product" in desc order.例如,按“供应商”或“产品”按 desc 顺序对“产品”数组进行排序。 Since this array has thousands of entries so we want to paginate and get 100 elements at a time then in next page next 100 and so on.由于这个数组有数千个条目,所以我们想要分页并一次获取 100 个元素,然后在下一页获取下一个 100 个元素,依此类推。 We also want to give search option like vendor=Volkswagen, so it gives only matching elements in output from Elastic Search query.我们还想提供像 vendor=Volkswagen 这样的搜索选项,因此它只提供来自 Elastic Search 查询的 output 中的匹配元素。

I am new to ES but as per my knowledge we can search, sort and paginate documents in ES by their fields.我是 ES 新手,但据我所知,我们可以在 ES 中按字段搜索、排序和分页文档。

But can we also search, sort and paginate data inside _source of a document in Elastic Search?但是我们也可以在 Elastic Search 的文档的 _source 中搜索、排序和分页数据吗? How can I achieve this using Elastic search query?如何使用弹性搜索查询来实现这一点?

Please help me in this thanks.请帮助我,谢谢。

I think you're looking for inner hits ?我想你正在寻找内心的打击

If you have a nested query, you can add inner_hits as option:如果您有嵌套查询,则可以添加inner_hits作为选项:

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

If you never use a nested query to retrieve your documents, I think you can add the nested query anyway and use match_all: {} inside the nested query:如果您从不使用嵌套查询来检索文档,我认为您无论如何都可以添加嵌套查询并在嵌套查询中使用match_all: {}

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

You can use from in inner_hits to page through the results.您可以使用inner_hits中的from对结果进行分页。

I hope this helps.我希望这有帮助。

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

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