This can be done by specifying tags as subdocuments , and then using nested in conjunction with a custom score query. In the example below, the terms “query” detect matching tags, a custom rating query uses the “wight” field from the “tags” of documents as points, and a sub-query uses the sum of these ratings as the final rating for the top-level document.
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
source share