Elasticsearch: Field level custom scores in text searches -
i started exploring elasticsearch. need find approach specifying custom scores @ field level. example:
i have collection named blog documents have following format:
{ "_id": "1736hst26672829", "name": "learning regular expressions basics", "author": "john lee", "summery": "here summery.", "body": "content of blog." }
if search text 'xyz' in collection result should reflect following score criteria
- match in field 'name' has priority 1.
- match in author field has 2nd priority.
- match in summery has 3rd priority.
- match in body has least priority.
i need top 10 results on basis of above criteria.
:
scoring in elasticsearch extremely customizable, following applies query time based custom scoring. there various other scoring options, index, in mapping (and applied every query), on filters or facets, using boosts or custom scoring.
while custom score query powerful solution, here docs various custom scoring methods read on.
the following simplest methods apply custom scoring in query time, although suggest read on custom score query.
"query": { "filtered": { "query": "bool": { "must": [ {"multi_match": { "fields": [ "name^4", "author^3", "summery^2", "body^1" ], "query": "xyz", "operator": "and", "type": "cross_fields", "analyzer": "standard" }} ] } } } }
for people search answer wish use nest, bellow same query using nest. use ^ character boost specific fields or use onfieldswithboost give fields custom scoring, , query sorted score.
var query = "xyz"; //add field names string in lower camelcase es default. list<string> searchin = new list<string(new string[] {"_id","name","author","summery","body"}); .type("blogtype") .sortdescending("_score") .query( q => q.multimatch( t => t.onfields( searchin .select(qs => qs == "name" ? "name^4" : qs) .select(qs => qs == "author" ? "author^3" : qs) .select(qs => qs == "summery" ? "summery^2" : qs) .select(qs => qs == "body" ? "body" : qs) ) .query(query) ) )
if have correct (default) mapping in es (c# object es indexed json object), can use following within onfields:
t => t.onfieldswithboost(qs => qs.add(entry => entry.name, 4.0) .add(entry => entry.author, 3.0) .add(entry => entry.summary, 2.0) .add(entry => entry.body, 1.0))
Comments
Post a Comment