1

I have a indexed field e.g. called "text" now i want to replace the relevance score by using the "function_score" query.

Here is my query:

{
   "index":"search-read",
   "type":"all",
   "body":{
      "from":0,
      "size":30,
      "explain":true,
      "query":{
         "function_score":{
            "query":{
               "query_string":{
                  "query":"assetType:ARTICLE AND miete"
               }
            },
            "script_score":{
               "script":"doc['text'].value.length()"
            },
            "boost_mode":"replace"
         }
      }
   }
}

The problem i do not understand is that

doc['text'].value.length()

do not retrieve me the length of a string, but why? A string with 2455 chars is stored in this field but i get 14.

This

doc['text'].value

should return me the field 'text' as java string object and than length() the length. Some ideas why my config is wrong or a other way to achive this? My goal is to lower the relevance score the shorter the text is; i know that this is the opposite how elasticsearch handles this.

Gizzmo
  • 691
  • 8
  • 21
  • I think i got the answer: this is not possible like i thought. From the maual of elasticsearch: The script_score function allows you to wrap another query and customize the scoring of it optionally with a computation derived from other NUMERIC field values in the doc using a script expression. – Gizzmo Aug 26 '14 at 09:02
  • 1
    it's not possible to call methods on value. what you are accessing are document fields that are limited to whatever ES provides to you. you can see what's available here: http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/modules-scripting.html you could always write a hack, however. when indexing your text value, add another property (e.g. text_length) and populate it based on your text input. it is a hack, but it will help you accomplish your goal as long as you are ok with the tradeoff – coffeeaddict Aug 27 '14 at 00:47
  • Hi, the solution you propose is the one i already implemented now:-) But its the right answer; not possible but viaable with a workarround, so if you post an answer i will mark it as right one! – Gizzmo Aug 28 '14 at 08:44

1 Answers1

1

it's not possible to call methods on value. what you are accessing are document fields that are limited to whatever ES provides to you. you can see what's available here:

http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/modules-scripting.html

you could always write a hack, however. when indexing your text value, add another property (e.g. text_length) and populate it based on your text input. it is a hack, but it will help you accomplish your goal as long as you are ok with the tradeoff

coffeeaddict
  • 858
  • 5
  • 3