我正在尝试确定在elasticsearch中索引文档的最佳方法。我有一个文档Doc,其中包含一些字段:
Doc created_at updated_at field_a field_b
但是Doc还将具有一些特定于单个用户的字段。例如,用户1的field_x的值为’A’,用户2的field_x的值为’B’。对于每个文档,用户数量非常有限(通常为2,最多10个用户)。当用户搜索field_x时,他们必须搜索属于他们的值。我一直在探索ES中的嵌套类型。
Doc created_at updated_at field_x: [{ user: 1 field_x: A },{ user: 2 field_x: B }]
当用户1在field_x上搜索值“ A”时,此文档应会命中。但是,当用户1按值“ B”搜索时,则不应这样做。
但是,根据文档:
为文档中多次出现的内部对象建立索引时的问题之一是,将发生“跨对象”搜索匹配
有没有一种方法可以避免嵌套类型的这种行为,还是应该探索其他类型?
有关此类查询性能的其他信息将非常有价值。仅阅读文档,它就表明嵌套查询在性能上与常规查询没有太大差异。如果有人有真正的经验,我很想听听。
嵌套类型是您要寻找的,而不必太担心性能。
在为文档建立索引之前,您需要为文档设置映射:
curl -XDELETE localhost:9200/index curl -XPUT localhost:9200/index curl -XPUT localhost:9200/index/type/_mapping -d '{ "type": { "properties": { "field_x": { "type": "nested", "include_in_parent": false, "include_in_root": false, "properties": { "user": { "type": "string" }, "field_x": { "type": "string", "index" : "not_analyzed" // NOTE* } } } } } }'
然后,索引您的文档:
curl -XPUT http://localhost:9200/index/type/1 -d ' { "field_a": "foo", "field_b": "bar", "field_x" : [{ "user" : "1", "field_x" : "A" }, { "user" : "2", "field_x" : "B" }] }'
并运行查询:
curl -XGET localhost:9200/index/type/_search -d '{ "query": { "nested" : { "path" : "field_x", "score_mode" : "avg", "query" : { "bool" : { "must" : [ { "term": { "field_x.user": "1" } }, { "term": { "field_x.field_x": "A" } } ] } } } } }';
这将导致
{"took":13,"timed_out":false,"_shards":{"total":5,"successful":5,"failed":0},"hits":{"total":1,"max_score":1.987628,"hits":[{"_index":"index","_type":"type","_id":"1","_score":1.987628, "_source" : { "field_a": "foo", "field_b": "bar", "field_x" : [{ "user" : "1", "field_x" : "A" }, { "user" : "2", "field_x" : "B" }] }}]}}
但是,查询
curl -XGET localhost:9200/index/type/_search -d '{ "query": { "nested" : { "path" : "field_x", "score_mode" : "avg", "query" : { "bool" : { "must" : [ { "term": { "field_x.user": "1" } }, { "term": { "field_x.field_x": "B" } } ] } } } } }';
不会返回任何结果
{"took":6,"timed_out":false,"_shards":{"total":5,"successful":5,"failed":0},"hits":{"total":0,"max_score":null,"hits":[]}}