【问题标题】:Elasticsearch geospatial search, problems with index setupElasticsearch 地理空间搜索,索引设置问题
【发布时间】:2015-06-15 12:46:29
【问题描述】:

我正在尝试搜索以前添加到索引中的文档,该索引已配置为允许地理空间查询(或者我认为是这样)。
我的 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 索引的所有文档总是显示出来,所以我没有成功地使用地理空间查询进行过滤;您是否发现任何错误,或者您有任何我可以遵循的指南来执行此操作?我搜索了一下,只找到了一些信息。

【问题讨论】:

    标签: elasticsearch geospatial


    【解决方案1】:

    您的代码中有几个问题:

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

    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);
    

    【讨论】:

    • 非常感谢您的回答,我一回到家就试试这个!
    • 我已经尝试过了,但出现以下错误:gist.github.com/lazywithclass/099ef5ec61f870a07e56 如果我从查询的body 中删除filtered 属性,虽然我得到了预期的结果,你能摆脱一些吗请点亮这个或指出我正确的方向?感谢您的帮助,非常感谢!
    • 哎呀,我的错,我复制/粘贴了您的查询并忘记将其包含在 query 中。更新了我上面的答案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-05-16
    • 1970-01-01
    • 1970-01-01
    • 2012-07-20
    • 2017-09-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多