如何在elasticsearch中对地理坐标应用滤镜?

时间:2016-04-27 07:07:21

标签: node.js elasticsearch mongoosastic

我正在使用带有mongoosastic npm模块的elasticsearch。我正在尝试对具有以下模型结构的地理坐标应用过滤器

geoLocation: {
    type: {
      type: String,
      default: 'Point'
    },
    coordinates: [Number] //orders should be lat,lng
  }

映射如下

{
      "events": {
        "settings": {
          "analysis": {
            "filter": {
              "edgeNGram_filter": {
                "type": "edgeNGram",
                "min_gram": 1,
                "max_gram": 50,
                "side": "front"
              }
            },
            "analyzer": {
              "edge_nGram_analyzer": {
                "type": "custom",
                "tokenizer": "edge_ngram_tokenizer",
                "filter": [
                  "lowercase",
                  "asciifolding",
                  "edgeNGram_filter"
                ]
              },
              "whitespace_analyzer": {
                "type": "custom",
                "tokenizer": "whitespace",
                "filter": [
                  "lowercase",
                  "asciifolding"
                ]
              }
            },
            "tokenizer": {
              "edge_ngram_tokenizer": {
                "type": "edgeNGram",
                "min_gram": "1",
                "max_gram": "50",
                "token_chars": [
                  "letter",
                  "digit"
                ]
              }
            }
          }
        },
        "mappings": {
          "event": {
            "_all": {
              "index_analyzer": "nGram_analyzer",
              "search_analyzer": "whitespace_analyzer"
            },
            "properties": {
              "title": {
                "type": "string",
                "index": "not_analyzed"
              },
              "geoLocation": {
                "index": "not_analyzed",
                "type": "geo_point"
              }
            }
          }
        }
      }
    }

查询

{
        "query": {
          "multi_match": {
            "query": "the",
            "fields": ["title", ]
          }
        },
        "filter" : {
            "geo_distance" : {
                "distance" : "200km",
                "geoLocation.coordinates" : {
                    "lat" : 19.007452,
                    "lon" : 72.831556
                }
            }
        }
      }

我无法使用以下模型结构在地理坐标上创建索引,我不明白是否无法使用上述模型结构索引地理坐标,因为在我的情况下坐标的顺序为lat,long和I已找到某个地方,elasticsearch期望坐标顺序为lat,lat。

错误

  

错误:SearchPhaseExecutionException [无法执行阶段[查询],   所有分片都失败了; shardFailures {[CDHdgtJnTbeu8tl2mDfllg] [events] [0]:   SearchParseException [[events] [0]:from [-1],size [-1]:Parse Failure   [无法解析来源

curl -XGET localhost:9200 / events

{
  "events": {
    "aliases": {},
    "mappings": {
      "1": {
        "properties": {
          "location": {
            "type": "double"
          },
          "text": {
            "type": "string"
          }
        }
      },
      "event": {
        "properties": {
          "city": {
            "type": "string"
          },
          "endTime": {
            "type": "date",
            "format": "dateOptionalTime"
          },
          "geo_with_lat_lon": {
            "type": "geo_point",
            "lat_lon": true
          },
          "isActive": {
            "type": "boolean"
          },
          "isRecommended": {
            "type": "boolean"
          },
          "location": {
            "type": "string"
          },
          "title": {
            "type": "string"
          }
        }
      }
    },
    "settings": {
      "index": {
        "creation_date": "1461675012489",
        "uuid": "FT-xVUdPQtyuKFm4J4Rd7g",
        "number_of_replicas": "1",
        "number_of_shards": "5",
        "events": {
          "mappings": {
            "event": {
              "_all": {
                "enabled": "false",
                "search_analyzer": "whitespace_analyzer",
                "index_analyzer": "nGram_analyzer"
              },
              "properties": {
                "geoLocation": {
                  "coordinates": {
                    "type": "geo_shape",
                    "index": "not_analyzed"
                  }
                },
                "location": {
                  "type": "string",
                  "index": "not_analyzed"
                },
                "title": {
                  "type": "string",
                  "index": "not_analyzed"
                },
                "geo_with_lat_lon": {
                  "type": "geo_point",
                  "lat_lon": "true",
                  "index": "not_analyzed"
                }
              }
            }
          },
          "settings": {
            "analysis": {
              "analyzer": {
                "edge_nGram_analyzer": {
                  "type": "custom",
                  "filter": [
                    "lowercase",
                    "asciifolding",
                    "edgeNGram_filter"
                  ],
                  "tokenizer": "edge_ngram_tokenizer"
                },
                "whitespace_analyzer": {
                  "type": "custom",
                  "filter": [
                    "lowercase",
                    "asciifolding"
                  ],
                  "tokenizer": "whitespace"
                }
              },
              "filter": {
                "edgeNGram_filter": {
                  "max_gram": "50",
                  "type": "edgeNGram",
                  "min_gram": "1",
                  "side": "front"
                }
              },
              "tokenizer": {
                "edge_ngram_tokenizer": {
                  "max_gram": "50",
                  "type": "edgeNGram",
                  "min_gram": "1",
                  "token_chars": [
                    "letter",
                    "digit"
                  ]
                }
              }
            }
          }
        },
        "version": {
          "created": "1070099"
        }
      }
    },
    "warmers": {}
  }
}

1 个答案:

答案 0 :(得分:2)

我为我的问题找到了解决方案

<强>映射

PUT / geo_test

{
   "mappings": {
      "type_test": {
         "properties": {
            "name": {
               "type": "string"
            },
            "geoLocation": {
               "type": "nested",
               "properties": {
                  "coordinates": {
                     "type": "geo_point",
                     "lat_lon": true
                  }
               }
            }
         }
      }
   }
}

查询

POST / geo_test / type_test / _search

{
   "query": {
      "filtered": {
         "filter": {
            "nested": {
               "path": "geoLocation",
               "query": {
                  "filtered": {
                     "filter": {
                        "geo_distance": {
                           "distance": 5,
                           "distance_unit": "km",
                           "geoLocation.coordinates": {
                              "lat": 41.12,
                              "lon": -71.34
                           }
                        }
                     }
                  }
               }
            }
         }
      }
   }
}