elasticsearch 如何在elasticsearch_源中存储数据,但不对其进行索引?
我只搜索了几个字段,但我希望能够将整个文档存储在ES中,以避免额外的DB(MySQL)查询 我尝试将elasticsearch 如何在elasticsearch_源中存储数据,但不对其进行索引?,elasticsearch,mapping,elasticsearch,Mapping,我只搜索了几个字段,但我希望能够将整个文档存储在ES中,以避免额外的DB(MySQL)查询 我尝试将index:no,store:no添加到映射中的整个对象/属性,但我仍然不确定字段是否正在被索引,并增加了不必要的开销 假设我有书,每本都有作者。我只想按书名搜索,但我希望能够检索整个文档 这样可以吗: mappings: properties: title: type: string index: analyzed author:
index:no
,store:no
添加到映射中的整个对象/属性,但我仍然不确定字段是否正在被索引,并增加了不必要的开销
假设我有书,每本都有作者。我只想按书名搜索,但我希望能够检索整个文档
这样可以吗:
mappings:
properties:
title:
type: string
index: analyzed
author:
type: object
index: no
store: no
properties:
first_name:
type: string
last_name:
type: string
或者我应该:
mappings:
properties:
title:
type: string
index: analyzed
author:
type: object
properties:
first_name:
index: no
store: no
type: string
last_name:
index: no
store: no
type: string
或者我完全错了?
那么不应该索引的
嵌套的属性呢?默认情况下,文档的\u源
将被存储,而不考虑您选择索引的字段。\u源文件
用于返回搜索结果中的文档,而索引的字段用于搜索
不能在对象上设置index:no
以阻止对象中的所有字段被索引,但可以使用path\u match
属性将index:no
设置应用于对象中的每个字段。下面是一个简单的例子
使用映射创建索引,其中包括作者
对象和嵌套的类别
对象的动态模板:
POST /shop
{
"mappings": {
"book": {
"dynamic_templates": [
{
"author_object_template": {
"path_match": "author.*",
"mapping": {
"index": "no"
}
}
},
{
"categories_object_template": {
"path_match": "categories.*",
"mapping": {
"index": "no"
}
}
}
],
"properties": {
"categories": {
"type": "nested"
}
}
}
}
}
为文档编制索引:
POST /shop/book/1
{
"title": "book one",
"author": {
"first_name": "jon",
"last_name": "doe"
},
"categories": [
{
"cat_id": 1,
"cat_name": "category one"
},
{
"cat_id": 2,
"cat_name": "category two"
}
]
}
如果您使用搜索词book
在title
字段中搜索,则会返回文档。如果在author.first\u name
或author.last\u name
上搜索,则不会有匹配项,因为此字段未编制索引:
POST /shop/book/_search
{
"query": {
"match": {
"author.first_name": "jon"
}
}
}
对于类别字段上的嵌套查询也是如此:
POST /shop/book/_search
{
"query": {
"nested": {
"path": "categories",
"query": {
"match": {
"categories.cat_name": "category"
}
}
}
}
}
您还可以使用该工具期望Lucene索引,并查看哪些字段已被索引 “索引”:“否”
是否意味着“存储”:“否”
?我读过store
的意思是在lucene中存储原始属性的\u源
,但我不确定它与索引的关系。为了确保-我不必为非索引字段提供映射?如果我放置了一个属性为X且为int的文档,然后放置了一个属性相同但为string的文档,ES不会抛出错误?否,索引的设置不会决定存储的设置。store的默认值是no,这在您的用例中很好,因为启用了_源。如果禁用_源字段并选择要存储的字段,则仅当存在匹配项时,才会在搜索结果中返回存储的字段。您必须为非索引字段提供映射,以便告诉Elasticsearch不要为其编制索引,否则Elasticsearch将使用默认分析器(标准分析器)为字段编制索引。但是,在上述示例中,动态模板用于非索引字段的映射。如果没有字段的映射,则如果更改属性的类型,将不会返回错误。