es显式Mapping和常用参数

2020-04-14  本文已影响0人  7赢月

介绍

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

显示Mapping

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

PUT index
{
"mappings":{}
}

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,控制对某些字段的索引


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值是可以被搜索的


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字段的


#数组类型
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

上一篇 下一篇

猜你喜欢

热点阅读