全字段查询 - Elasticsearch 权威指南中文版

返回介绍

全字段查询

发布于 2019-07-04 字数 3089 浏览 833 评论 0

自定义_all字段

在元数据:_all字段中,我们解释了特殊的_all字段会将其它所有字段中的值作为一个大字符串进行索引。尽管将所有字段的值作为一个字段进行索引并不是非常灵活。如果有一个自定义的_all字段用来索引人名,另外一个自定义的_all字段用来索引地址就更好了。

ES通过字段映射中的copy_to参数向我们提供了这一功能:

PUT /my_index
{
    "mappings": {
        "person": {
            "properties": {
                "first_name": {
                    "type":     "string",
                    "copy_to":  "full_name" <1>
                },
                "last_name": {
                    "type":     "string",
                    "copy_to":  "full_name" <1>
                },
                "full_name": {
                    "type":     "string"
                }
            }
        }
    }
}

// SENSE: 110_Multi_Field_Search/45_Custom_all.json

first_name和last_name字段中的值会被拷贝到full_name字段中。

有了这个映射,我们可以通过first_name字段查询名字,last_name字段查询姓氏,或者full_name字段查询姓氏和名字。

提示:first_name和last_name字段的映射和full_name字段的索引方式的无关。full_name字段会从其它两个字段中拷贝字符串的值,然后仅根据full_name字段自身的映射进行索引。

<!–
[[custom-all]]
=== Custom _all Fields
In , we explained that the special `_all` field indexes the values
from all other fields as one big string.(((“_all field”, sortas=”all field”)))(((“multifield search”, “custom _all fields”))) Having all fields indexed into one
field is not terribly flexible, though. It would be nice to have one custom
`_all` field for the person’s name, and another custom `_all` field for the
address.
Elasticsearch provides us with this functionality via the `copy_to` parameter
in a field (((“copy_to parameter”)))(((“mapping (types)”, “copy_to parameter”)))mapping:
[source,js]
————————————————–
PUT /my_index
{
“mappings”: {
“person”: {
“properties”: {
“first_name”: {
“type”: “string”,
“copy_to”: “full_name”
},
“last_name”: {
“type”: “string”,
“copy_to”: “full_name”
},
“full_name”: {
“type”: “string”
}
}
}
}
}
————————————————–
// SENSE: 110_Multi_Field_Search/45_Custom_all.json
The values in the `first_name` and `last_name` fields
are also copied to the `full_name` field.
With this mapping in place, we can query the `first_name` field for first
names, the `last_name` field for last name, or the `full_name` field for first
and last names.
NOTE: Mappings of the `first_name` and `last_name` fields have no bearing
on how the `full_name` field is indexed. The `full_name` field copies the
string values from the other two fields, then indexes them according to the
mapping of the `full_name` field only.
–>

上一篇:以字段为中心查询

下一篇:跨字段查询

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

目前还没有任何评论,快来抢沙发吧!