繁体   English   中英

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

[英]elasticsearch return only one document of id field

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

  {
        "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文档,分别是chantierId和必须与我所在位置最近的文档。

谢谢

您可以在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"
        ]
    ]
]

我用以下stackoverflow问题的答案解决了我的问题: 从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试图帮助我

暂无
暂无

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

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