<i id='4PFfH'><tr id='4PFfH'><dt id='4PFfH'><q id='4PFfH'><span id='4PFfH'><b id='4PFfH'><form id='4PFfH'><ins id='4PFfH'></ins><ul id='4PFfH'></ul><sub id='4PFfH'></sub></form><legend id='4PFfH'></legend><bdo id='4PFfH'><pre id='4PFfH'><center id='4PFfH'></center></pre></bdo></b><th id='4PFfH'></th></span></q></dt></tr></i><div id='4PFfH'><tfoot id='4PFfH'></tfoot><dl id='4PFfH'><fieldset id='4PFfH'></fieldset></dl></div>
    <tfoot id='4PFfH'></tfoot>
  • <small id='4PFfH'></small><noframes id='4PFfH'>

      1. <legend id='4PFfH'><style id='4PFfH'><dir id='4PFfH'><q id='4PFfH'></q></dir></style></legend>
        • <bdo id='4PFfH'></bdo><ul id='4PFfH'></ul>

        ElasticSearch 仅返回具有不同值的文档

        时间:2023-09-28

            <bdo id='klnsw'></bdo><ul id='klnsw'></ul>
              <tbody id='klnsw'></tbody>
          • <small id='klnsw'></small><noframes id='klnsw'>

            <legend id='klnsw'><style id='klnsw'><dir id='klnsw'><q id='klnsw'></q></dir></style></legend>
            • <i id='klnsw'><tr id='klnsw'><dt id='klnsw'><q id='klnsw'><span id='klnsw'><b id='klnsw'><form id='klnsw'><ins id='klnsw'></ins><ul id='klnsw'></ul><sub id='klnsw'></sub></form><legend id='klnsw'></legend><bdo id='klnsw'><pre id='klnsw'><center id='klnsw'></center></pre></bdo></b><th id='klnsw'></th></span></q></dt></tr></i><div id='klnsw'><tfoot id='klnsw'></tfoot><dl id='klnsw'><fieldset id='klnsw'></fieldset></dl></div>

                <tfoot id='klnsw'></tfoot>
                1. 本文介绍了ElasticSearch 仅返回具有不同值的文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  假设我有这个给定的数据

                  Let's say I have this given data

                  {
                              "name" : "ABC",
                              "favorite_cars" : [ "ferrari","toyota" ]
                            }, {
                              "name" : "ABC",
                              "favorite_cars" : [ "ferrari","toyota" ]
                            }, {
                              "name" : "GEORGE",
                              "favorite_cars" : [ "honda","Hyundae" ]
                            }
                  

                  每当我在搜索最喜欢的汽车是丰田的人时查询此数据时,它都会返回此数据

                  Whenever I query this data when searching for people who's favorite car is toyota, it returns this data

                  {
                  
                              "name" : "ABC",
                              "favorite_cars" : [ "ferrari","toyota" ]
                            }, {
                              "name" : "ABC",
                              "favorite_cars" : [ "ferrari","toyota" ]
                            }
                  

                  结果是两条名为 ABC 的记录.如何仅选择不同的文档?我想得到的结果只有这个

                  the result is Two records of with a name of ABC. How do I select distinct documents only? The result I want to get is only this

                  {
                                  "name" : "ABC",
                                  "favorite_cars" : [ "ferrari","toyota" ]
                                }
                  

                  这是我的查询

                  {
                      "fuzzy_like_this_field" : {
                          "favorite_cars" : {
                              "like_text" : "toyota",
                              "max_query_terms" : 12
                          }
                      }
                  }
                  

                  我正在使用 ElasticSearch 1.0.0.使用 java api 客户端

                  I am using ElasticSearch 1.0.0. with the java api client

                  推荐答案

                  您可以使用 聚合.使用 术语聚合结果将按一个字段分组,例如name,还提供了该字段每个值的出现次数,并将按此计数对结果进行排序(降序).

                  You can eliminate duplicates using aggregations. With term aggregation the results will be grouped by one field, e.g. name, also providing a count of the ocurrences of each value of the field, and will sort the results by this count (descending).

                  {
                    "query": {
                      "fuzzy_like_this_field": {
                        "favorite_cars": {
                          "like_text": "toyota",
                          "max_query_terms": 12
                        }
                      }
                    },
                    "aggs": {
                      "grouped_by_name": {
                        "terms": {
                          "field": "name",
                          "size": 0
                        }
                      }
                    }
                  }
                  

                  除了 hits 之外,结果还将包含 buckets,其中 key 中的唯一值和 中的计数>doc_count:

                  In addition to the hits, the result will also contain the buckets with the unique values in key and with the count in doc_count:

                  {
                    "took" : 4,
                    "timed_out" : false,
                    "_shards" : {
                      "total" : 5,
                      "successful" : 5,
                      "failed" : 0
                    },
                    "hits" : {
                      "total" : 2,
                      "max_score" : 0.19178301,
                      "hits" : [ {
                        "_index" : "pru",
                        "_type" : "pru",
                        "_id" : "vGkoVV5cR8SN3lvbWzLaFQ",
                        "_score" : 0.19178301,
                        "_source":{"name":"ABC","favorite_cars":["ferrari","toyota"]}
                      }, {
                        "_index" : "pru",
                        "_type" : "pru",
                        "_id" : "IdEbAcI6TM6oCVxCI_3fug",
                        "_score" : 0.19178301,
                        "_source":{"name":"ABC","favorite_cars":["ferrari","toyota"]}
                      } ]
                    },
                    "aggregations" : {
                      "grouped_by_name" : {
                        "buckets" : [ {
                          "key" : "abc",
                          "doc_count" : 2
                        } ]
                      }
                    }
                  }
                  

                  请注意,由于重复消除和结果排序,使用聚合的成本会很高.

                  Note that using aggregations will be costly because of duplicate elimination and result sorting.

                  这篇关于ElasticSearch 仅返回具有不同值的文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:在 MySQL 上使用 NoSQL 数据库 下一篇:寻找一个轻量级的 java 兼容的内存键值存储

                  相关文章

                  最新文章

                    <bdo id='dGKmJ'></bdo><ul id='dGKmJ'></ul>

                    <i id='dGKmJ'><tr id='dGKmJ'><dt id='dGKmJ'><q id='dGKmJ'><span id='dGKmJ'><b id='dGKmJ'><form id='dGKmJ'><ins id='dGKmJ'></ins><ul id='dGKmJ'></ul><sub id='dGKmJ'></sub></form><legend id='dGKmJ'></legend><bdo id='dGKmJ'><pre id='dGKmJ'><center id='dGKmJ'></center></pre></bdo></b><th id='dGKmJ'></th></span></q></dt></tr></i><div id='dGKmJ'><tfoot id='dGKmJ'></tfoot><dl id='dGKmJ'><fieldset id='dGKmJ'></fieldset></dl></div>

                  1. <legend id='dGKmJ'><style id='dGKmJ'><dir id='dGKmJ'><q id='dGKmJ'></q></dir></style></legend>
                    1. <small id='dGKmJ'></small><noframes id='dGKmJ'>

                      <tfoot id='dGKmJ'></tfoot>