解决此问题的另一种方法是使用嵌套文档:
首先设置映射以创建
vocab一个嵌套文档,这意味着每个
wtag/
rscore文档将在内部作为单独的文档建立索引:
curl -XPUT "http://localhost:9200/myindex/" -d'{ "settings": {"number_of_shards": 1}, "mappings": { "mytype": { "properties": { "vocab": { "type": "nested", "fields": { "wtag": { "type": "string" }, "rscore": { "type": "float" } } } } } }}'然后索引您的文档:
curl -XPUT "http://localhost:9200/myindex/mytype/1" -d'{ "vocab": [ { "wtag": "James Bond", "rscore": 2.14 }, { "wtag": "world", "rscore": 0.86 }, { "wtag": "somemore", "rscore": 3.15 } ]}'curl -XPUT "http://localhost:9200/myindex/mytype/2" -d'{ "vocab": [ { "wtag": "hiii", "rscore": 1.34 }, { "wtag": "world", "rscore": 0.94 }, { "wtag": "somemore", "rscore": 3.23 } ]}'并运行
nested查询以匹配所有嵌套文档,并
rscore为每个与之匹配的嵌套文档求和:
curl -XGET "http://localhost:9200/myindex/mytype/_search" -d'{ "query": { "nested": { "path": "vocab", "score_mode": "sum", "query": { "function_score": { "query": { "match": { "vocab.wtag": "james bond world" } }, "script_score": { "script": "doc["rscore"].value" } } } } }}'


