Note that there are some explanatory texts on larger screens.

plurals
  1. POElasticsearch: search/order by overall tag weight
    primarykey
    data
    text
    <p>I have to solve a problem that exeeds my very basic knowhow of elasticsearch.</p> <p>I have a set of objects - each one has a set of tags. Like:</p> <pre><code>obj_1 = ["a", "b", "c"] obj_2 = ["a", "b"] obj_3 = ["c", "b"] </code></pre> <p>I want to search the objects using <strong>weighted tags</strong>. For example:</p> <pre><code>search_tags = {'a': 1.0, 'c': 1.5} </code></pre> <p>I want the search tags to be an OR query. That is - I don't want to exclude documents that don't have all of the queried tags. But I want them to be ordered by the one that has the most weight (sort of: each matched tag multiplied by its weight).</p> <p>Using the example above the order of the ducuments returned would be: </p> <ul> <li>obj_1 (score: 1.0+1.5)</li> <li>obj_3 (score: 1.5) </li> <li>obj_2 (score: 1.0)</li> </ul> <p>What would be the best approach to this regarding the document's structure and the correct way to query ES? </p> <p>There is a similar question here: <a href="https://stackoverflow.com/questions/13095866/elastic-search-tagging-strength-nested-child-document-boosting">Elastic search - tagging strength (nested/child document boosting)</a> only that I do not want to specify the weight when indexing - I want it done when searching.</p> <p>My current setup is as follows.</p> <p>The objects:</p> <pre><code>[ "title":"1", "tags" : ["a", "b", "c"], "title":"2", "tags" : ["a", "b"], "title":"3", "tags" : ["c", "b"], "title":"4", "tags" : ["b"] ] </code></pre> <p>And my query:</p> <pre><code>{ "query": { "custom_filters_score": { "query": { "terms": { "tags": ["a", "c"], "minimum_match": 1 } }, "filters": [ {"filter":{"term":{"tags":"a"}}, "boost":1.0}, {"filter":{"term":{"tags":"c"}}, "boost":1.5} ], "score_mode": "total" } } } </code></pre> <p>The problem is that it only returns object 1 and 3. It should match object 2 (has tag "a") as well, or am I doing something wrong?</p> <p><strong>UPDATE AS SUGGESTED</strong></p> <p>Ok. Changed boost to script to calculate the minimum. Removed minimum match. My request:</p> <pre><code>{ "query": { "custom_filters_score": { "query": { "terms": { "tags": ["a", "c"] } }, "filters": [ {"filter":{"term":{"tags":"a"}}, "script":"1.0"}, {"filter":{"term":{"tags":"c"}}, "script":"1.5"} ], "score_mode": "total" } } } </code></pre> <p>Response:</p> <pre><code>{ "_shards": { "failed": 0, "successful": 5, "total": 5 }, "hits": { "hits": [ { "_id": "3", "_index": "test", "_score": 0.23837921, "_source": { "tags": [ "c", "b" ], "title": "3" }, "_type": "bit" }, { "_id": "1", "_index": "test", "_score": 0.042195037, "_source": { "tags": [ "a", "b", "c" ], "title": "1" }, "_type": "bit" } ], "max_score": 0.23837921, "total": 2 }, "timed_out": false, "took": 3 } </code></pre> <p>Still getting wrong order and one result missing. obj_1 should be before obj_3 (because it has both tags) and obj_2 is still missing completely. How can this be?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload