Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>It can be done by indexing tags as <a href="http://www.elasticsearch.org/guide/reference/mapping/nested-type.html" rel="noreferrer">nested documents</a> and then using the <a href="http://www.elasticsearch.org/guide/reference/query-dsl/nested-query.html" rel="noreferrer">nested</a> query in combination with the <a href="http://www.elasticsearch.org/guide/reference/query-dsl/custom-score-query.html" rel="noreferrer">custom score</a> query. In the example below, the terms query finds matching tags, the custom score query uses values of the "wight" field of "tags" documents as scores and the nested query is using sum of these scores as the final score for the top level document.</p> <pre><code>curl -XDELETE 'http://localhost:9200/test-idx' echo curl -XPUT 'http://localhost:9200/test-idx' -d '{ "mappings": { "doc": { "properties": { "title": { "type": "string" }, "tags": { "type": "nested", "properties": { "tag": { "type": "string", "index": "not_analyzed" }, "weight": { "type": "float" } } } } } } }' echo curl -XPUT 'http://localhost:9200/test-idx/doc/1' -d '{ "title": "1", "tags": [{ "tag": "A", "weight": 1 }, { "tag": "B", "weight": 2 }, { "tag": "C", "weight": 4 }] } ' echo curl -XPUT 'http://localhost:9200/test-idx/doc/2' -d '{ "title": "2", "tags": [{ "tag": "B", "weight": 2 }, { "tag": "C", "weight": 3 }] } ' echo curl -XPUT 'http://localhost:9200/test-idx/doc/3' -d '{ "title": "3", "tags": [{ "tag": "B", "weight": 2 }, { "tag": "D", "weight": 4 }] } ' echo curl -XPOST 'http://localhost:9200/test-idx/_refresh' echo # Example with custom script (slower but more flexable) curl -XGET 'http://localhost:9200/test-idx/doc/_search?pretty=true' -d '{ "query" : { "nested": { "path": "tags", "score_mode": "total", "query": { "custom_score": { "query": { "terms": { "tag": ["A", "B", "D"], "minimum_match" : 1 } }, "script" : "doc['\''weight'\''].value" } } } }, "fields": [] }' echo </code></pre>
    singulars
    1. This table or related slice is empty.
    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.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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