2017-02-10 148 views
0
ElasticSearch version is 2.3.4 

如何查询geo_distance_query和script_query组合?ElasticSearch脚本查询和地理距离查询

我有一个索引,它有很多的商店信息,有位置和距离的字段。 给定一个geo_point,该索引根据基于geo_distance_query的查询顶部这一点来存储所有的距离,并且计算出的距离应该小于当前存储在距离字段中的值。

我觉得script_query和geo_distance_query的组合,不知道该怎么实现。

试试下面的代码:

query: { 
       bool: { 
        must: [ 
         { 
          script: { 
           script: { 
            inline: "doc['location'].arcDistance(" + _.lat + "," + _.lon + ") < doc['distance'].value" 
            , lang: "painless" 
           } 
          } 
         } 
        ] 
       } 
      } 

结果Elasticsearch错误:

[illegal_argument_exception] script_lang not supported [painless] 

是否有人遇到并解决了这个问题,用什么方法来实现查询?

变化码:

{ 
bool: { 
    must: [ 
     { 
      script: { 
       script: { 
        inline: "doc['location'].arcDistance(" + _.lat + "," + _.lon + ") < doc['distance'].value" 
        , lang: "groovy" 
       } 
      } 
     } 
    ] 
} 

}

也错误:

message: '[script_exception] failed to run inline script [doc[\'location\'].arcDistance(31.89484,120.287825) < doc[\'distance\'].value] using lang [groovy]', 

细节图像:

code result

{ 
"error": { 
    "root_cause": [{ 
     "type": "script_exception", 
     "reason": "failed to run inline script [doc[\'location\'].arcDistance(31.89484,120.287825) < 3000] using lang [groovy]" 
    }], 
    "type": "search_phase_execution_exception", 
    "reason": "all shards failed", 
    "phase": "query_fetch", 
    "grouped": true, 
    "failed_shards": [{ 
     "shard": 0, 
     "index": "business_stores_v1", 
     "node": "Z_65eOYXT6u8aDf7mp2ZRg", 
     "reason": { 
      "type": "script_exception", 
      "reason": "failed to run inline script [doc[\'location\'].arcDistance(31.89484,120.287825) < 3000] using lang [groovy]", 
      "caused_by": {"type": "null_pointer_exception", "reason": null} 
     } 
    }] 
}, "status": 500 

}

+0

的'painless'脚本语言已经在ES 5中引入了。这个错误意味着你正在运行ES 2.x,并且正在阅读ES 5的文档。如果你正在运行ES 2.x,那么使用'groovy'而不是'无痛' – Val

+0

@Val已经更新。也错误。 我不认为他支持这种查询与动态比较的方式进行比较。 – sky91

+0

你现在得到什么错误?肯定是一个不同的。 '_.lat'和'_.lon'从哪里来? – Val

回答

0

你得到的错误,即null_pointer_exception是因为在你business_stores_v1索引的文件之一有一个空location场,因此式失败

doc['location'].arcDistance(...) 
      ^
       | 
    null_pointer_exception here 
+0

对不起,很久没写java了,有没有直觉意识到这个错误null_pointer_exception ... 再次感谢 – sky91

+0

已使用doc ['location']。为空字段值检查... – sky91