【问题标题】:Mongoosastic, how can i make an Geo Distance QueryMongoosastic,我如何进行地理距离查询
【发布时间】:2016-07-29 11:28:23
【问题描述】:

对于 mongoose,我的架构看起来像这样:

var articleSchema = new Schema({
    Name: {type: String, es_indexed: true},        
    geo_with_lat_lon: {
        geo_point: {
            type: String,
            es_type: 'geo_point',
            es_lat_lon: true
        },
        lat: {type: Number},
        lon: {type: Number}
    },
    ...
});

我添加了一个新文档:

var artikel = new global.DBModel.article({
      Name:"Test",
      geo_with_lat_lon:{lon:-70,lat:40}
});
artikel.save(...);

现在我想按距离进行过滤。我的查询如下所示:

global.DBModel.article.search({
        "bool": {
            "must": {
                "match_all": {}
            },
            "filter" :{
                "geo_distance": {
                    "distance": "200km",
                    "geo_with_lat_lon": {
                        "lat": 40,
                        "lon": -70
                    }
                }
            }
        }
    }, {...}

但我总是得到错误

{
    "error": {
      "root_cause": [
        {
          "type": "query_parsing_exception",
          "reason": "failed to find geo_point field [geo_with_lat_lon]",
          "index": "articles",
          "line": 1,
          "col": 127
        }
      ],
      "type": "search_phase_execution_exception",
      "reason": "all shards failed",
      "phase": "query",
      "grouped": true,
      "failed_shards": [
        {
          "shard": 0,
          "index": "articles",
          "node": "YdQHw7nSRc-T0LwItupmmw",
          "reason": {
            "type": "query_parsing_exception",
            "reason": "failed to find geo_point field [geo_with_lat_lon]",
            "index": "articles",
            "line": 1,
            "col": 127
          }
        }
      ]
    },
    "status": 400   }

我的问题是:如何按距离正确过滤?

【问题讨论】:

  • 问题不在于您的过滤器,而在于您的映射。你能检查一下你的elasticsearch数据库中'geo_with_lat_lon'的类型是什么
  • 您可以关注此SO thread 以获得答案

标签: elasticsearch mongoose mongoosastic


【解决方案1】:

谢谢Paradise228,这是映射。

这项工作:

...geo_with_lat_lon: {
        geo_point: {
            es_indexed: true,
            type: String,
            es_type: 'geo_point',
            es_lat_lon: true
        },
        lat: {type: Number},
        lon: {type: Number}
    },...

使用此映射:

global.DBModel.store.createMapping(function (err, mapping) {
    if (err) {
        console.log('error creating mapping (you can safely ignore this)');
        console.log(err);
    } else {
        console.log('mapping created!');
        console.log(mapping);
    }
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-04
    • 1970-01-01
    • 2019-05-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多