Elasticsearch 常用参数说明
2020-09-07 本文已影响0人
觉释
copy_to
作用是将该字段的值复制到目标字段,实现类似_all的作用。不会出现在_source中,只能用来搜索。
PUT my_index4
{
"mappings": {
"properties": {
"first_name": {
"type": "text"
, "copy_to": "full_name"
},
"last_name": {
"type": "text"
, "copy_to": "full_name"
},
"full_name" : {
"type": "text"
}
}
}
}
可以看到这个index中,full_name的内容就是从 first_name 和 last_name 中复制过来的。
然后创建一个新的文档,文档只需要写first_name 和 last_name即可:
PUT my_index4/_doc/1
{
"first_name": "john",
"last_name": "smith"
}
查询:查询包含关键字john smith的文档,必须同时包含两个关键字才返回。
GET my_index4/_search
{
"query": {
"match": {
"full_name": {
"query": "john smith",
"operator": "and"
}
}
}
}
index参数
index参数作用是控制当前字段是否被索引,默认为true,false表示不记录,即不可被搜索。
PUT my_index5
{
"mappings": {
"properties": {
"cookie": {
"type": "text",
"index": false
},
"content": {
"type": "text",
"index": true
}
}
}
}
这个index有两个字段,其中cookie设定为不可被搜索
写入文档:
PUT my_index/_doc/1
{
"cookie": "name=mike",
"content": "hello world"
}
尝试分别查询一下两个字段,看看区别:
GET my_index5/_search
{
"query": {
"match": {
"cookie": "mike"
}
}
}
GET my_index5/_search
{
"query": {
"match": {
"content": "hello"
}
}
当在es中存储了一些不想要被检索的字段如身份证、手机等,这是对于这些字段就可以使用index设置为false,这样有一定的安全性还可以节省空间
index_options参数
**index_options的作用是用于控制倒排索引记录的内容,有如下四种配置:
- docs:只记录doc id
- freqs:记录doc id 和term frequencies
- positions:记录doc id、 term frequencies和term position
- offsets:记录doc id、 term frequencies、term position、character offsets
text类型的默认配置为positions,其他默认为docs。记录的内容越多,占据的空间越大。
null_value参数
这个参数的作用是当字段遇到null值的时候的处理策略,默认为null,即空值,此时es会忽略该值。可以通过这个参数设置某个字段的默认值。