簡體   English   中英

Elasticsearch 地理查詢與聚合

[英]Elasticsearch geo query with aggregation

我有一個包含用戶位置的 elasticsearch 索引。 我需要使用 geohash 網格對地理邊界框執行聚合查詢,對於文檔計數小於某個值的存儲桶,我需要返回所有文檔。

我怎樣才能做到這一點?

由於您沒有提供有關您創建的索引和用戶位置的任何相關信息。

我正在考慮以下數據:

索引定義

{
  "mappings": {
    "properties": {
      "location": {
        "type": "geo_point"
      }
    }
  }
}   

索引樣本文件

POST _bulk

{"index":{"_id":1}}
{"location":"52.37408,4.912350","name":"The golden dragon"}
{"index":{"_id":2}}
{"location":"52.369219,4.901618","name":"Burger King"}
{"index":{"_id":3}}
{"location":"52.371667,4.914722","name":"Wendys"}
{"index":{"_id":4}}
{"location":"51.222900,4.405200","name":"Taco Bell"}
{"index":{"_id":5}}
{"location":"48.861111,2.336389","name":"McDonalds"}
{"index":{"_id":6}}
{"location":"48.860000,2.327000","name":"KFC"}

根據你的問題:

當請求詳細的桶時,應應用geo_bounding_box之類的過濾器來縮小主題區域

想了解更多,可以參考這個官方的 ES doc

  • 現在,為了通過聚合過濾基於doc_count的數據,我們可以使用bucket_selector管道聚合。

文檔

管道聚合處理從其他聚合而不是文檔集產生的輸出,將信息添加到 output 樹。

因此,計算 doc_count 所需完成的工作量將是相同的。

詢問

    {
  "aggs": {
    "location": {
      "filter": {
        "geo_bounding_box": {
          "location": {
            "top_left": {
              "lat": 52.5225,
              "lon": 4.5552
            },
            "bottom_right": {
              "lat": 52.2291,
              "lon": 5.2322
            }
          }
        }
      },
      "aggs": {
        "around_amsterdam": {
          "geohash_grid": {
            "field": "location",
            "precision": 8
          },
          "aggs": {
            "the_filter": {
              "bucket_selector": {
                "buckets_path": {
                  "the_doc_count": "_count"
                },
                "script": "params.the_doc_count < 2"
              }
            }
          }
        }
      }
    }
  }
}

搜索結果

"hits": {
    "total": {
      "value": 6,
      "relation": "eq"
    },
    "max_score": 1.0,
    "hits": [
      {
        "_index": "restaurant",
        "_type": "_doc",
        "_id": "1",
        "_score": 1.0,
        "_source": {
          "location": "52.37408,4.912350",
          "name": "The golden dragon"
        }
      },
      {
        "_index": "restaurant",
        "_type": "_doc",
        "_id": "2",
        "_score": 1.0,
        "_source": {
          "location": "52.369219,4.901618",
          "name": "Burger King"
        }
      },
      {
        "_index": "restaurant",
        "_type": "_doc",
        "_id": "3",
        "_score": 1.0,
        "_source": {
          "location": "52.371667,4.914722",
          "name": "Wendys"
        }
      },
      {
        "_index": "restaurant",
        "_type": "_doc",
        "_id": "4",
        "_score": 1.0,
        "_source": {
          "location": "51.222900,4.405200",
          "name": "Taco Bell"
        }
      },
      {
        "_index": "restaurant",
        "_type": "_doc",
        "_id": "5",
        "_score": 1.0,
        "_source": {
          "location": "48.861111,2.336389",
          "name": "McDonalds"
        }
      },
      {
        "_index": "restaurant",
        "_type": "_doc",
        "_id": "6",
        "_score": 1.0,
        "_source": {
          "location": "48.860000,2.327000",
          "name": "KFC"
        }
      }
    ]
  },
  "aggregations": {
    "location": {
      "doc_count": 3,
      "around_amsterdam": {
        "buckets": [
          {
            "key": "u173zy3j",
            "doc_count": 1
          },
          {
            "key": "u173zvfz",
            "doc_count": 1
          },
          {
            "key": "u173zt90",
            "doc_count": 1
          }
        ]
      }
    }
  }
}

它將根據"params.the_doc_count < 2"過濾掉所有計數小於2的文檔

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM