简体   繁体   中英

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. :

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

Thanks

You can add size parameter before query as the number of documents you want to recieve. 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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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