我们使用弹性搜索(ES)上的两种类型的文档:项目和插槽,其中项目是插槽文档的父项。 我们使用以下命令定义索引:
curl -XPOST 'localhost:9200/items' -d @itemsdef.json
其中itemsdef.json
具有以下定义
{
"mappings" : {
"item" : {
"properties" : {
"id" : {"type" : "long" },
"name" : {
"type" : "string",
"_analyzer" : "textIndexAnalyzer"
},
"location" : {"type" : "geo_point" },
}
}
},
"settings" : {
"analysis" : {
"analyzer" : {
"activityIndexAnalyzer" : {
"alias" : ["activityQueryAnalyzer"],
"type" : "custom",
"tokenizer" : "whitespace",
"filter" : ["trim", "lowercase", "asciifolding", "spanish_stop", "spanish_synonym"]
},
"textIndexAnalyzer" : {
"type" : "custom",
"tokenizer" : "whitespace",
"filter" : ["word_delimiter_impl", "trim", "lowercase", "asciifolding", "spanish_stop", "spanish_synonym"]
},
"textQueryAnalyzer" : {
"type" : "custom",
"tokenizer" : "whitespace",
"filter" : ["trim", "lowercase", "asciifolding", "spanish_stop"]
}
},
"filter" : {
"spanish_stop" : {
"type" : "stop",
"ignore_case" : true,
"enable_position_increments" : true,
"stopwords_path" : "analysis/spanish-stopwords.txt"
},
"spanish_synonym" : {
"type" : "synonym",
"synonyms_path" : "analysis/spanish-synonyms.txt"
},
"word_delimiter_impl" : {
"type" : "word_delimiter",
"generate_word_parts" : true,
"generate_number_parts" : true,
"catenate_words" : true,
"catenate_numbers" : true,
"split_on_case_change" : false
}
}
}
}
}
然后我们使用以下命令添加子文档定义:
curl -XPOST 'localhost:9200/items/slot/_mapping' -d @slotsdef.json
slotsdef.json
具有以下定义:
{
"slot" : {
"_parent" : {"type" : "item"},
"_routing" : {
"required" : true,
"path" : "parent_id"
},
"properties": {
"id" : { "type" : "long" },
"parent_id" : { "type" : "long" },
"activity" : {
"type" : "string",
"_analyzer" : "activityIndexAnalyzer"
},
"day" : { "type" : "integer" },
"start" : { "type" : "integer" },
"end" : { "type" : "integer" }
}
}
}
最后,我们使用以下命令执行批量索引:
curl -XPOST 'localhost:9200/items/_bulk' --data-binary @testbulk.json
testbulk.json保存以下数据:
{"index":{"_type": "item", "_id":35}}
{"location":[40.4,-3.6],"id":35,"name":"A Name"}
{"index":{"_type":"slot","_id":126,"_parent":35}}
{"id":126,"start":1330,"day":1,"end":1730,"activity":"An Activity","parent_id":35}
我们通过ES Head插件看到定义似乎没问题。我们测试分析仪以检查它们是否已加载并且它们有效。这两个文档都显示在ES Head浏览器视图中。但是如果我们尝试使用API检索子项,ES会回复它不存在:
$ curl -XGET 'localhost:9200/items/slot/126'
{"_index":"items","_type":"slot","_id":"126","exists":false}
当我们导入50个文档时,可以通过API检索所有父文档,但只有一些子元素请求得到成功响应。
我的猜测是,它可能与如何通过分片和路由存储文档有关...我当然不清楚它是如何工作的。
有关如何检索单个子文档的任何线索? ES Head显示它们已存储但HTTP GET到localhost:9200 / items / slot / XXX随机响应“exists”:false。
答案 0 :(得分:10)
子文档使用父ID进行路由。因此,为了检索子文档,您需要在查询的路由参数中指定父ID:
curl "localhost:9200/items/slot/126?routing=35"
如果父ID不可用,则必须搜索子文档:
curl "localhost:9200/items/slot/_search?q=id:126"
或切换到具有单个分片的索引。