我认为更好的方法是使用percolator mapping type。首先,创建一个索引来存储您的geo_shape 查询(在queries 类型中)。您还需要定义点的映射(在 points 类型中),以便 ES 知道它在查询什么:
PUT /my-index
{
"mappings": {
"points": {
"properties": {
"point": {
"type": "geo_shape"
}
}
},
"queries": {
"properties": {
"query": {
"type": "percolator"
}
}
}
}
}
然后,您可以为每个已索引的多边形索引一个 geo_shape 查询。在这里,我们为您已经存储在polygon-index 中的每个多边形定义了一个geo_shape 查询:
PUT /my-index/queries/query-id-for-polygon-id
{
"query" : {
"geo_shape": {
"location": {
"indexed_shape": {
"index": "polygon-index",
"type": "polygon-type",
"id": "polygon-id",
"path": "polygon-field"
}
}
}
}
}
最后,您可以针对要检查的每个点发出一个 msearch query,其中包含一个 percolate query。
单个点的渗透查询看起来像这样。这个查询基本上是说:“找到我所有包含给定点的多边形查询”。结果,您将获得一个命中列表,其中每个命中都是一个查询,并将包含匹配的多边形(查询)的 id。
POST /my-index/_search
{
"query" : {
"percolate" : {
"field" : "query",
"document_type" : "points",
"document" : {
"point" : {
"type" : "point",
"coordinates" : [-77.03653, 38.897676]
}
}
}
}
}
所以现在您需要为每个要检查的点创建一个,格式如下:
POST /my-index/_search
{}
{"query": {"percolate": {"field": "query", "document_type" : "points", "document" : {"point" : {"type" : "point","coordinates" : [-77.03653, 38.897676]}}}}}
{}
{"query": {"percolate": {"field": "query", "document_type" : "points", "document" : {"point" : {"type" : "point","coordinates" : [-77.03653, 38.897676]}}}}}
{}
{"query": {"percolate": {"field": "query", "document_type" : "points", "document" : {"point" : {"type" : "point","coordinates" : [-77.03653, 38.897676]}}}}}
{}
{"query": {"percolate": {"field": "query", "document_type" : "points", "document" : {"point" : {"type" : "point","coordinates" : [-77.03653, 38.897676]}}}}}
...
您将返回每个点,包含它的多边形。