Elasticsearch 地理空间搜索,索引设置问题

Elasticsearch geospatial search, problems with index setup

我正在尝试搜索以前添加到索引的文档,该索引已配置为允许地理空间查询(或者我认为如此)。
我的 elasticsearch 实例托管在 qbox.io.

这是我编写的用于从命令行创建索引的代码

curl -XPOST username:password@instance-id.qbox.io/events -d '{
    "settings" : {
        "number_of_shards" : 1
    },
    "mappings" : {
      "mygeopoints": {
        "properties": {
          "geopoint": {
            "type": "geo_point",
            "lat_lon" : true
          },
          "radius": {
            "type": "long"
          }
        }
      }
    }
  }'

据我所知,我应该在我的 events 索引和我想对其执行的搜索类型之间创建一个映射。

这是我编写的用于创建测试文档的代码:

var elasticsearch = require('elasticsearch');
var client = new elasticsearch.Client({
  host: 'username:password@instance-id.qbox.io'
});

client.create({
  index: 'events',
  type: 'geo_point',
  body: {
    location: {
      lat: 51.507351,
      lon: -0.127758
    }
  }
}, console.log);

这是我编写的用于搜索给定半径的文档的代码

var elasticsearch = require('elasticsearch');
var client = new elasticsearch.Client({
  host: 'username:password@instance-id.qbox.io'
});

client.search({
  filtered: {
    query: {
      match_all: {}
    },
    filter: {
      geo_distance: {
        distance: '1km',
        location: {
          lat: 48.507351,
          lon: -0.127758
        }
      }
    }
  }
}, console.log);

我的问题是 event 索引的所有文档总是显示,所以我没有成功地使用地理空间查询进行过滤;你有没有发现任何错误或者你有任何我可以遵循的指南来做到这一点?我搜索了一下,只找到了一些信息。

您的代码中存在几个问题:

问题 1:当您在第二个片段中创建文档时,您没有使用正确的映射类型,并且您的正文不包含声明的正确字段名称在您的映射中:

client.create({
  index: 'events',
  type: 'geo_point',     <-------- wrong type
  body: {
    location: {          <-------- wrong field name
      lat: 51.507351,
      lon: -0.127758
    }
  }
}, console.log);

由于在您的映射类型中,您声明的类型称为 mygeopoints,而 geo_point 字段称为 geopoint,您的 create 调用必须使用它们像这样:

client.create({
  index: 'events',
  type: 'mygeopoints',
  body: {
    geopoint: {
      lat: 51.507351,
      lon: -0.127758
    }
  }
}, console.log);

问题 2: 您的 search call 中的参数散列不正确,因为您的查询 DSL 需要分配给 body 参数(类似于您的 create 调用),最好添加一个 index 参数来集中搜索(见下文)

问题 3: 最后,在您的查询中,您没有在 geo_distance 过滤器中使用正确的字段,您使用 location 而不是 geopoint。您的查询应如下所示:

client.search({
  index: 'events',                <---- add index name
  body: {                         <---- add query in body parameter
    query:{
     filtered: {
       filter: {
         geo_distance: {
           distance: '1km',
           geopoint: {            <---- proper geo point field name
             lat: 48.507351,
             lon: -0.127758
           }
         }
       }
     }
    }
  }
}, console.log);