Function request with field field_value_factor by non (yet) existing field

I have been dealing with this problem for a long time and can not figure it out.

Take the following case:

I have 2 employees in my company who have their own blog page:

POST blog/page/1 { "author": "Byron", "author-title": "Junior Software Developer", "content" : "My amazing bio" } 

and

 POST blog/page/2 { "author": "Jason", "author-title": "Senior Software Developer", "content" : "My amazing bio is better" } 

After they created their blog posts, we would like to track the โ€œviewsโ€ of their blogs and increase search results based on their โ€œviewsโ€.

This can be done using the function evaluation query:

 GET blog/_search { "query": { "function_score": { "query": { "match": { "author-title": "developer" } }, "functions": [ { "filter": { "range": { "views": { "from": 1 } } }, "field_value_factor": { "field": "views" } } ] } } } 

I use a range filter to make sure that field_value_factor does not affect the rating when the number of views is 0 (the rating will also be 0).

Now, when I try to run this query, I will get the following exception:

 nested: ElasticsearchException[Unable to find a field mapper for field [views]]; }] 

This makes sense because the field does not exist anywhere in the index. If I added views = 0 by the time index, I would not have the above problem, since this field is known in the index. But in my use case, I cannot add this either by time index or by matching.

Based on the ability to use a range filter in a function evaluation request, I thought I could use

Still gives:

 nested: ElasticsearchException[Unable to find a field mapper for field [views]]; }] 

Where would I expect Elasticsearch to apply a filter first before parsing field_value_factor.

Any thoughts on how to fix this problem without using map files or fixes during indexing or scripts?

+6
source share
1 answer

The error you see occurs during the parsing of the request, i.e. nothing has been done yet. At that time, FieldValueFactorFunctionParser creates the filter_value_factor function, which will be executed later, but notices that the views field exists in the display type.

Please note that the filter has not yet been executed, like the filter_value_factor function, it is analyzed only by FunctionScoreQueryParser .

I am wondering why you cannot just add a field to your type of mapping, it's as simple as running this

 curl -XPUT 'http://localhost:9200/blog/_mapping/page' -d '{ "page" : { "properties" : { "views" : {"type" : "integer"} } } }' 

If this is REALLY not an option, another possibility would be to use script_score instead, for example:

 { "query": { "function_score": { "query": { "match": { "author-title": "developer" } }, "functions": [ { "filter": { "range": { "views": { "from": 1 } } }, "script_score": { "script": "_score * doc.views.value" } } ] } } } 
+3
source

Source: https://habr.com/ru/post/979053/


All Articles