简体   繁体   English

elasticsearch只返回一个ID字段的文档

[英]elasticsearch return only one document of id field

I have this data returned with my actual query. 我的实际查询返回了这些数据。

  {
        "id": 1,
        "chantierId": 60,
        "location": {
          "lat": 49.508804203333,
          "lon": 2.4385195366667
        }
  },
  {
        "id": 2,
        "chantierId": 60,
        "location": {
          "lat": 49.508780168333,
          "lon": 2.43844484
        }
  },
  {
        "id": 3,
        "chantierId": 33,
        "location": {
          "lat": 49.50875823,
          "lon": 2.4383772216667
        }
  }

This my Elasticsearch query which search the point with geo_point. 这是我的Elasticsearch查询,其中使用geo_point搜索该点。 :

[
    "query" => [
        "filtered" => [
            "query" => [
                "match_all" => []
            ],
            "filter" => [
                "geo_distance" => [
                    "distance" => "100m",
                    "location" => ['lat' => 49.508804203333, 'lon => 2.4385195366667]
                ]
            ]
        ]
    ],
    "sort" => [
        "_geo_distance" => [
            "location" => ['lat' => 49.508804203333, 'lon => 2.4385195366667],
            "order" => "asc"
        ]
    ]
]

How can I to have only one documents of chantierId for 33, 60 and the must nearest of my location. 我如何只拥有一份chantierId文档,分别是chantierId和必须与我所在位置最近的文档。

Thanks 谢谢

You can add size parameter before query as the number of documents you want to recieve. 您可以在query前添加size参数作为要接收的文档数。 The modified query will be: 修改后的查询将是:

[   "size" => 1,
    "query" => [
        "filtered" => [
            "query" => [
                "match_all" => []
            ],
            "filter" => [
                "geo_distance" => [
                    "distance" => "100m",
                    "location" => ['lat' => 49.508804203333, 'lon => 2.4385195366667]
                ]
            ]
        ]
    ],
    "sort" => [
        "_geo_distance" => [
            "location" => ['lat' => 49.508804203333, 'lon => 2.4385195366667],
            "order" => "asc"
        ]
    ]
]

I Resolved my problem with this answer of stackoverflow question : Remove duplicate documents from a search in Elasticsearch 我用以下stackoverflow问题的答案解决了我的问题: 从Elasticsearch的搜索中删除重复的文档

So : 因此:

         [
            "query" => [
                "filtered" => [
                    "query" => [
                        "match_all" => []
                    ],
                    "filter" => [
                        "geo_distance" => [
                            "distance" => "100m",
                            "location" => $location
                        ]
                    ]
                ]
            ],
            "sort" => [
                "_geo_distance" => [
                    "location" => $location,
                    "order" => "asc"
                ]
            ],
            "aggs" => [
                "Geoloc" => [
                    "terms" => [
                        "field" => "chantierId"
                    ],
                    "aggs" => [
                        "Geoloc_docs" => [
                            "top_hits" => [
                                "size" => 1
                            ]
                        ]
                    ]
                ]
            ]
        ]);

Thanks to @Tanu who tried to help me 感谢@Tanu试图帮助我

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

相关问题 Laravel 5-从关系中仅返回一个字段 - Laravel 5 - return only one field from relationship Codeigniter return()仅返回db中的一个字段 - Codeigniter return() returns only one field in db 如果其中一个记录与另一个字段的给定ID相匹配,则返回所有非给定ID的记录 - Return all records of non-given id if just one of those records matches the given id of another field 仅返回查询中的字段 - Return only field in query 如果结果不止一个,则对db中一个字段的值进行排名,并仅返回最高/最低排名值 - If more than one result, rank values from one field in db and return only the highest/lowest ranked value 如何使用 PHP 更新/替换 ElasticSearch 文档中的字段? - How to update/replace a field in an ElasticSearch document using PHP? doctrine2 queryBuilder必须只返回与数组值(ids)匹配的结果:0 / Null和/或One和/或许多id必须返回一个结果 - doctrine2 queryBuilder must return only result matching with array values (ids): 0/Null and/or One and/or Many id(s) have to return One result 从elasticsearch返回的查询结果中渲染PDF或其他文档 - Rendering PDF or other other document from the query result return by elasticsearch Laravel-hasManyThrough仅返回_id - Laravel - hasManyThrough return only _id 使用LEFT OUTER JOIN返回一个匹配的行和其他仅匹配的ID - Using LEFT OUTER JOIN return one matched row and additional only matching id's
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM