【问题标题】:elasticsearch return only one document of id fieldelasticsearch只返回一个id字段的文档
【发布时间】:2016-05-26 04:22:41
【问题描述】:

我的实际查询返回了这些数据。

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

这是我的 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"
        ]
    ]
]

我怎样才能只有一份chantierId 33、60 和必须离我最近的位置的文件。

谢谢

【问题讨论】:

    标签: php elasticsearch


    【解决方案1】:

    您可以在query 之前添加size 参数作为要接收的文档数。修改后的查询将是:

    [   "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"
            ]
        ]
    ]
    

    【讨论】:

    • 谢谢,但它只提供一份文件。我在寻找每个 chantierId 的一个文档:一个用于 60 和 33 这个示例。在生产中它将是 1,3,5,24,35,34
    【解决方案2】:

    我用这个 stackoverflow 问题的答案解决了我的问题:Remove duplicate documents from a search in Elasticsearch

    所以:

             [
                "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
                                ]
                            ]
                        ]
                    ]
                ]
            ]);
    

    感谢试图帮助我的@Tanu

    【讨论】:

      猜你喜欢
      • 2022-06-30
      • 2012-03-25
      • 1970-01-01
      • 2015-05-03
      • 2014-12-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多