Hiya
I am pretty new to Elastic Search. Trying it out and looks
awesome till now. I was trying to understand how to use custom_score
and from the documentation could not fathom how to use it.
"custom_score" : {
"query" : {
....
},
"script" : "score * doc['my_numeric_field'].value"
}
What is inside that "query" string ??? Can someone give me a basic
example using custom_score query dsl ?
"query" is whatever query you are using to search for documents, which
could be (eg):
matches all documents
"query": { "match_all": {} }
search all fields for the keywords "foo" and "bar"
"query": { "query_string": { "query": "foo bar" } }
By default, the results would be ordered by relevance/score (where all
documents in the match_all query have the same relevance)
So "custom_score" gives you a way of customising the relevance of each
document dynamically. For instance, you could make recent documents more
relevant than older documents. Or in your case, posts with more "likes"
would be more relevant than posts with fewer "likes".
Note, however, that this is a difficult calculation to get right. For
instance, what happens if you have one post which matches 1 keyword but
has 1000 likes, and another which matches 10 keywords, but has no likes?
Getting the right balance probably requires a lot of experimentation.
And should be revisited once you have more live data.
Also i would like to know how to solve this problem:
Say you have a forum with lots of threads and each post in a thread
has a bunch of thanks (similar to "like" functionality we see in
facebook). Now when i search on that forum index i want it results
posts. i want to order posts on number of likes it has.
The problem is - "How to index likes ?"
If i keep likes as a field in the post document then i will be
suffering consistency issues when 2 people simultaneously make thank a
post. if i put like as a seperate document then there wont be any
consistency issues but then how can i sort posts in the search results
according to number of thanks they receive ???
You can't "join" documents like you can in a relational database. So if
you want to use the number of likes to influence the score, then you
need to store this value in the post document.
So you need to solve the consistency issue, which obviously depends on
how your application is setup.
Are you storing this data in a database as well? If so, then you could
just do a COUNT of all the likes that each post has.
If you're not, well you could still store a "likes" document in ES, and
have it reference the ID of the post document, and do a count using ES.
hth
clint