弹性搜索中 doc.field_name 与 field_name 之间的区别?
Difference between doc.field_name vs field_name in Elastic Search?
我有这样的来源:
{
"doc" : {
"questions" : [
{
"question" : "Which gold customers in Texas had bank accounts with more than million last year ?",
"state" : "COMPLETED",
"tags" : [
"computer"
]
}
]
}
}
当我编写基于状态和标签的搜索查询时,查询如下所示,
{
"size": "2000",
"query": {
"bool": {
"must": [
{
"match": {
"doc.questions.state": "COMPLETED"
}
},
{
"match": {
"doc.questions.tags": "computer"
}
}
]
}
},
"from": "0",
"_source": {
"includes": [
"doc.questions.question",
"doc.questions.state",
"doc.questions.tags"
]
}
}
为什么要写 doc.questions.tag 而不是 questions.tags ?
如果我从任何键中删除 doc.,则没有匹配项
哪里不一样了
基本上,Elasticsearch 在 JSON 结构 中有两个 JSON 的变体,即 nested
or object
类型。
在您的情况下,它是 object
类型。您可以使用
验证 mapping 或 RDBMS 术语 index 的 schema
GET <your_index_name>/_mapping
为了根据内部 json 结构的值进行搜索,您需要 包含 整个路径字段 否则 elasticsearch 会将 questions
视为 doc
.
之外的字段
因此,如果您的 JSON 结构如下所示,则可以通过删除 doc
来实现。
{
"questions":[
{
"question":"Sample Question",
"state":"Completed",
"tags":[
"computer"
]
}
]
}
希望您现在明白为什么您不会很简单地得到任何结果,因为 questions
的 JSON 结构从 doc
开始。
我分享的链接可以帮助您澄清。希望对您有所帮助!
除了 Kamal 的评论之外,在您的文档中使用半保留关键字 doc
通常是一种不好的做法!
Doc
例如用作顶层 doc_values context within scripts,如下所示:
GET my_index/_search
{
"script_fields": {
"sales_price": {
"script": {
"lang": "expression",
"source": "doc['cost_price'] * markup", <---
"params": {
"markup": 0.2
}
}
}
}
}
这意味着它很容易与实际文档中的 doc 混淆。所以重命名为更具描述性......这里的一切都是 "doc" ;)
我有这样的来源:
{
"doc" : {
"questions" : [
{
"question" : "Which gold customers in Texas had bank accounts with more than million last year ?",
"state" : "COMPLETED",
"tags" : [
"computer"
]
}
]
}
}
当我编写基于状态和标签的搜索查询时,查询如下所示,
{
"size": "2000",
"query": {
"bool": {
"must": [
{
"match": {
"doc.questions.state": "COMPLETED"
}
},
{
"match": {
"doc.questions.tags": "computer"
}
}
]
}
},
"from": "0",
"_source": {
"includes": [
"doc.questions.question",
"doc.questions.state",
"doc.questions.tags"
]
}
}
为什么要写 doc.questions.tag 而不是 questions.tags ?
如果我从任何键中删除 doc.,则没有匹配项
哪里不一样了
基本上,Elasticsearch 在 JSON 结构 中有两个 JSON 的变体,即 nested
or object
类型。
在您的情况下,它是 object
类型。您可以使用
GET <your_index_name>/_mapping
为了根据内部 json 结构的值进行搜索,您需要 包含 整个路径字段 否则 elasticsearch 会将 questions
视为 doc
.
因此,如果您的 JSON 结构如下所示,则可以通过删除 doc
来实现。
{
"questions":[
{
"question":"Sample Question",
"state":"Completed",
"tags":[
"computer"
]
}
]
}
希望您现在明白为什么您不会很简单地得到任何结果,因为 questions
的 JSON 结构从 doc
开始。
我分享的链接可以帮助您澄清。希望对您有所帮助!
除了 Kamal 的评论之外,在您的文档中使用半保留关键字 doc
通常是一种不好的做法!
Doc
例如用作顶层 doc_values context within scripts,如下所示:
GET my_index/_search
{
"script_fields": {
"sales_price": {
"script": {
"lang": "expression",
"source": "doc['cost_price'] * markup", <---
"params": {
"markup": 0.2
}
}
}
}
}
这意味着它很容易与实际文档中的 doc 混淆。所以重命名为更具描述性......这里的一切都是 "doc" ;)