美文网首页
es显式Mapping和常用参数

es显式Mapping和常用参数

作者: 7赢月 | 来源:发表于2020-04-14 20:34 被阅读0次

介绍

本章会简单介绍显示Mapping和一些常用参数的使用

显示Mapping

可以通过以下的语句使用显示Mapping

PUT index
{
"mappings":{}
}
  • 为了减少自定义Mapping的工作量,减少出错概率,可以使用如下步骤:
    1、创建一个临时的index,写入一些样本数据
    2、通过访问Mapping API获得该文件的动态Mapping定义
    3、修改,使用该配置创建自己的索引
    4、删除临时索引

  • 控制单前字段是否被索引
PUT users
{
    "mappings" : {
      "properties" : {
        "firstName" : {
          "type" : "text"
        },
        "lastName" : {
          "type" : "text"
        },
        "mobile" : {
          "type" : "text",
          "index": false
        }
      }
    }
}

如上添加了参数 设置了"index": false

PUT users/_doc/1
{
  "firstName":"Li",
  "lastName": "DP",
  "mobile": "12345678"
}

POST /users/_search
{
  "query": {
    "match": {
      "mobile":"12345678"
    }
  }
}

我们先插入后数据,然后使用设置字段进行查询时会出现错误


返回结果

上面也提示了无法进行搜索没有设置index的字段,所有通过以上,我们可以通过设置"index": false,控制对某些字段的索引


  • Index Option
    四种不同级别的Index Option的配置,可以控制倒排索引的内容
    1、docs:记录doc id
    2、freqs:记录doc id和term frequencies
    3、positions:记录doc id,term frequencies和term position
    4、offsets:记录doc id,term frequencies,term position和character offect

  • 实现对Null进行搜索时
    当我们出现对Null进行搜索的需求时,可以使用如下:

PUT users
{
    "mappings" : {
      "properties" : {
        "firstName" : {
          "type" : "text"
        },
        "lastName" : {
          "type" : "text"
        },
        "mobile" : {
          "type" : "keyword",
          "null_value": "NULL"
        }
      }
    }
}
PUT users/_doc/1
{
  "firstName":"Li",
  "lastName": "DP",
  "mobile": null
}


PUT users/_doc/2
{
  "firstName":"Zhang",
  "lastName": "DP"
}

我们先创建索引,通过设置 "null_value": "NULL"来实现NULL的搜索,然后插入两条数据,其中一条包含NULL值,进行搜索,看看结果:


返回结果

我们可以看到NULL值是可以被搜索的


  • copy_to 的使用
    copy_to实现对字段的拷贝到字段的功能,同时注意copy_to不会出现在_source中,看看下面的例子:
PUT users
{
  "mappings": {
    "properties": {
      "firstName":{
        "type": "text",
        "copy_to": "fullName"
      },
      "lastName":{
        "type": "text",
        "copy_to": "fullName"
      }
    }
  }
}
PUT users/_doc/1
{
  "firstName":"Zhang",
  "lastName": "DP"

}

上面进行了copy_to用法的设置和数据的插入,下面我们进行搜索看看

GET users/_search?q=fullName:(Zhang DP)

POST users/_search
{
  "query": {
    "match": {
       "fullName":{
        "query": "Zhang DP",
        "operator": "and"
      }
    }
  }
}
返回结果

我们可以看到返回结果中是没有fullName字段的


  • 数组类型
    es是不提供原生的数组类型的,但是在任何字段下,都可以包含多个值,看看下面的例子:
#数组类型
PUT users/_doc/1
{
  "name":"onebird",
  "interests":"reading"
}

PUT users/_doc/1
{
  "name":"twobirds",
  "interests":["reading","music"]
}

我们先插入两组数据,然后看看Mapping

GET users/_mapping
返回结果

结果显示该字段依然是text类型的

小结

本章介绍了显示Mapping的定义,以及index option,NULL值搜索,copy_to和数据类型的使用

引用

https://time.geekbang.org/course/detail/197-105685

相关文章

网友评论

      本文标题:es显式Mapping和常用参数

      本文链接:https://www.haomeiwen.com/subject/cnlxvhtx.html