2014-02-26 42 views
6

我是Elastic Search的新手。我已經收錄電影藝術家(演員和導演)的ElasticSearch和一個簡單的文本搜索工作正常,例如,如果我搜索「史蒂芬」語法如下彈性搜索中的function_score的語法

{"query": 
    {"query_string": 
     {"query":"steven"} 
    } 
} 

...我得到下面的結果,這是罰款:

1. Steven Conrad  - Popularity (from document) = 487 - elasticsearch _score = 3,2589545 
2. Steven Knight  - Popularity (from document) = 487 - elasticsearch _score = 3,076738 
3. Steven Waddington - Popularity (from document) = 431 - elasticsearch _score = 2,4931839 
4. Steven E. De Souza - Popularity (from document) = 534 - elasticsearch _score = 2,4613905 
5. Steven R. Monroe - Popularity (from document) = 293 - elasticsearch _score = 2,4613905 
6. Steven Mackintosh - Popularity (from document) = 363 - elasticsearch _score = 2,2812681 
7. Steven Wright  - Popularity (from document) = 356 - elasticsearch _score = 2,2812681 
8. Steven Soderbergh - Popularity (from document) = 5947 - elasticsearch _score = 2,270944 
9. Steven Seagal  - Popularity (from document) = 1388 - elasticsearch _score = 2,270944 
10. Steven Bauer  - Popularity (from document) = 714 - elasticsearch _score = 2,270944 

然而,正如你可以在上面看到,我有一個普及數字字段我的文檔中,並且,對於「史蒂芬」進行搜索時,我想最當紅藝人(史蒂芬·索德伯格,史蒂文西格爾......)先來。

理想情況下,我想對結果進行排序由以上popularity * _score

我敢肯定,我必須使用彈性搜索的function_score功能,但我不能找出確切的語法。

我試着做我的「改良」的語法如下

{ 
    "query": { 
    "custom_score": { 
     "query": { 
     "query_string": { 
      "query": "steven" 
     } 
     }, 
     "script": "_score * doc['popularity']" 
    } 
    } 
} 

搜索,但我得到一個異常(摘自錯誤信息如下:)

org.elasticsearch.search.query.QueryPhaseExecutionException: [my_index][4]: query[filtered(function score (_all:steven,function=script[_score * doc['popularity']], params [null]))->cache(_type:artist)],from[0],size[10]: Query Failed [Failed to execute main query] 
    // .... 
Caused by: java.lang.RuntimeException: uncomparable values <<1.9709579>> and <<[email protected]>> 
    // ... 
    ... 9 more 
Caused by: java.lang.ClassCastException: org.elasticsearch.index.fielddata.ScriptDocValues$Longs cannot be cast to java.lang.Float 
    at java.lang.Float.compareTo(Float.java:33) 
    at org.elasticsearch.common.mvel2.math.MathProcessor.doOperationNonNumeric(MathProcessor.java:266) 

在我的印象我使用的語法不正確

什麼應該是正確的語法?還是有什麼我失蹤?感謝很多提前

編輯 我的表映射定義如下:

"mappings" : { 
    "artist" : { 
    "_all" : { 
     "auto_boost" : true 
    }, 
    "properties" : { 
     "first_name" : { 
     "type" : "string", 
     "index" : "not_analyzed", 
     "analyzer" : "standard" 
     }, 
     "last_name" : { 
     "type" : "string", 
     "boost" : 2.0, 
     "index" : "not_analyzed", 
     "norms" : { 
      "enabled" : true 
     }, 
     "analyzer" : "standard" 
     }, 
     "popularity" : { 
     "type" : "integer" 
     } 
    } 
    } 
} 
+0

可以請你告訴我你是怎麼讓腳本我的意思是你寫什麼你YML文件或任何內部的請告訴我,我在尋找這爲so長? –

回答

6

你錯過了.value附近doc['...']

這對我的作品(不映射我存儲的整數):

$ curl -XPUT localhost:9200/test/test/a -d '{"name":"steven", "popularity": 666}' 
{"_index":"test","_type":"test","_id":"a","_version":1,"created":true} 

$ curl -XPUT localhost:9200/test/test/b -d '{"name":"steven", "popularity": 42}' 
{"_index":"test","_type":"test","_id":"b","_version":1,"created":true} 

$ curl -XPOST localhost:9200/test/test/_search\?pretty -d '{ "query": { "custom_score": { "query": { "match_all": {}}, "script": "_score * doc[\"popularity\"].value" } } }'                                     
{ 
    "took" : 83, 
    "timed_out" : false, 
    "_shards" : { 
    "total" : 1, 
    "successful" : 1, 
    "failed" : 0 
    }, 
    "hits" : { 
    "total" : 2, 
    "max_score" : 666.0, 
    "hits" : [ { 
     "_index" : "test", 
     "_type" : "test", 
     "_id" : "a", 
     "_score" : 666.0, "_source" : {"name":"steven", "popularity": 666} 
    }, { 
     "_index" : "test", 
     "_type" : "test", 
     "_id" : "b", 
     "_score" : 42.0, "_source" : {"name":"steven", "popularity": 42} 
    } ] 
    } 
} 
+2

doh!當然 !它現在適用於「.value」。非常感謝 – benoit

+0

你能告訴我你是如何啓用腳本的嗎?我的意思是你在你的yml文件或任何東西里面寫了什麼? –