1

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

timothy L
  • 65
  • 1
  • 6

2 Answers2

1

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"
        ]
    ]
]
Tanu
  • 1,503
  • 12
  • 21
  • Thanks but it give only one document. I looking for one document of each chantierId : one for 60 and 33 for this example. In production it will be 1,3,5,24,35,34 – timothy L May 25 '16 at 18:21
0

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

timothy L
  • 65
  • 1
  • 6