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

时间:2016-05-25 10:29:45

标签: php elasticsearch

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

  {
        "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且必须离我所在地最近的文件。

由于

2 个答案:

答案 0 :(得分:0)

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

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

答案 1 :(得分:0)

我用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试图帮助我