我们在弹性搜索 (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 显示它们已被存储,但对 localhost:9200/items/slot/XXX 的 HTTP GET 随机响应“exists”:false。