2017-02-10 150 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