Is it possible to give fix score for specific field search in elasticsearch - elasticsearch

Is it possible to give fix score based on every field match. for e.g one field matched then increment score for document by 1 , second field matched then increment document score by 1, so if there are one fields matched the score would be 1 if two fields matched then score would be 2 and so on...

The best tool for modifying the _score value of a query is the function_score query.
Elasticsearch actually offers many methods for calculating the score for each match. You can use a custom_score query along with a script to access the value of a particular numeric field. Consider this statement:
"script" : "_score * doc['my_numeric_field'].value"
Here, we are giving weight to the value of my_numeric_field by multiplying it with the default _score. You could also use the custom_filters_score_query, in which you apply filters to restrict the result set and then use a script or a boost to assign a score to any documents that match the filter. Similarly, you could apply the custom_boost_factor to any query to multiply the default score of that query with a boost value. In the 0.19.0 release of Elasticsearch, there is a new query that combines all of these into the function_score query.

Related

How to search result set from a result set in elasticsearch

Im tring to understand the DSL query i needed if i want to make a search from a result set i got. means i have an initial term search, then i want make another query upon the previous result.
lets say i a have 10 documents with a sharing identifier between them, each document has a description field. i want to search first all the documents containing the value 'Hello' in the description and then take their id's, and search the document containing the value 'good by'.
thanks.
No need to execute two queries, you can use filter context that will filter out the results.filter parameter will filter out documents that do not match, and will also not affect the score for matching documents.
Filter context is in effect whenever a query clause is passed to a
filter parameter, such as the filter or must_not parameters in the
bool query, the filter parameter in the constant_score query, or the
filter aggregation.
Refer this to know more about Query and Filter contexts

Boosting score in Elasticsearch based on aggregation result

I need to boost the score of my documents based on a particular value. That value can be obtained from aggregation query.
Currently I am using 2 queries to do it, would like to achieve it in a single query.
1-So basically first query gets me the highest no of occurrence of a particular chapter based on a simple term/match query.
2- Next step is once I get the highest occurring chapter will fire another query which would basically have the same above query with another term query added with a boost factor of 10.
Any input with this regards is welcomed, if we can accomplish this in one query. Thanks in advance.
Ashit

Sort descending with timestamp in elasticsearch and then filter the sort with a particular value on top

I want to sort the data in elastic search using the timestamp.. And in that sorted results i want those matching a particular key on top and then the rest
for eg.. I have a data where there is a key timestamp and gender.
So i want to sort the data in the descending order of the timestamp and the result should contain the gender with male on top and then the rest of the genders.
I tried but can achieve any one at a time
P.S : My elasticsearch version is 1.5
You could use a constant score query to give all documents with the desired key the score "1", and all others the score "0".
Then you can sort by score & timestamp.
https://www.elastic.co/guide/en/elasticsearch/reference/1.5/query-dsl-constant-score-query.html
https://www.elastic.co/guide/en/elasticsearch/reference/1.5/query-dsl-function-score-query.html
You could also do the same with a script, but the function score query would be recommended.
https://www.elastic.co/guide/en/elasticsearch/reference/1.5/search-request-sort.html#_script_based_sorting

ElasticSearch Stats Aggregation custom field value for max/min

Is it possible to retrieve Stats Aggregation with custom field value of max/min document?
Eg. imagine example from ElasticSearch Stats Aggregation documentation is there possible to retrieve for example name of student with max/min value of grade? (we assume that beside grade there is student name in document).
Is it even possible? What if ElasticSearch gives us multiple documents with max/min value?
No - it's not possible to get a max value and the rest of the document in a single query.
You would need to do two queries - the first to get the max value, the second to return documents that have a matching value.

Using matching document original score in filter script for custom filters score query

I want to use "custom filters score" query and use filters to control the score of resulting documents.
I want a way to use the document's original score as computed by ElasticSearch, and then use that score to calculate the final score of the document, which matches the given filters.
Something like "_docScore * 50/100" as a script for a filter, where "_docScore" is the original score of a document that matches the filter.
How to achieve this in ElasticSearch?
Any help is greatly appreciated.
Regards & Thanks,
Aditya.
Documents in a filtered query would be unranked and have the same score.
http://www.elasticsearch.org/guide/reference/query-dsl/custom-score-query/
But you can use a custom score query together with a filtered query and use a script to calculate a score based on the document values. This was added in 0.90, I believe.

Resources