Mapping的定义

可以对索引的数据字段设置类型, 还可以设置不变索引.也就是说不被搜索到.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
PUT users 
{
  "mappings": {
    "properties": {
      "firstName":{
        "type": "text"
      },
      "lastName":{
        "type": "text"
      },
      "mobile":{
        "type": "text",
        "index": false  # 设置不变索引的字段
      }
    }
  }
}

结果

1
2
3
4
5
{
  "acknowledged" : true,
  "shards_acknowledged" : true,
  "index" : "users"
}

添加真实数据.

1
2
3
4
5
6
PUT users/_doc/1
{
  "firstName":"sg",
  "lastName":"foot",
  "mobile":"123"
}

对已经禁止索引的mobile字段查询

1
2
3
4
5
6
7
8
POST /users/_search
{
  "query": {
    "match": {
      "mobile": "123"
    }
  }
}

结果显示报错是:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
{
  "error" : {
    "root_cause" : [
      {
        "type" : "query_shard_exception",
        "reason" : "failed to create query: Cannot search on field [mobile] since it is not indexed.",
        "index_uuid" : "YkoG3QrnS3qKMPJwgYuvug",
        "index" : "users"
      }
    ],
    "type" : "search_phase_execution_exception",
    "reason" : "all shards failed",
    "phase" : "query",
    "grouped" : true,
    "failed_shards" : [
      {
        "shard" : 0,
        "index" : "users",
        "node" : "bob_wQxnRHaCvOo8eG__fA",
        "reason" : {
          "type" : "query_shard_exception",
          "reason" : "failed to create query: Cannot search on field [mobile] since it is not indexed.",
          "index_uuid" : "YkoG3QrnS3qKMPJwgYuvug",
          "index" : "users",
          "caused_by" : {
            "type" : "illegal_argument_exception",
            "reason" : "Cannot search on field [mobile] since it is not indexed."
          }
        }
      }
    ]
  },
  "status" : 400
}

空字段设置

删除上面的数据.重新定义

1
DELETE users

重新定义

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
PUT users 
{
  "mappings": {
    "properties": {
      "firstName":{
        "type": "text"
      },
      "lastName":{
        "type": "text"
      },
      "mobile":{
        "type": "keyword",
        "null_value": "NULL"
      }
    }
  }
}

添加真实数据

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
PUT users/_doc/1
{
  "firstName":"sg",
  "lastName":"foot",
  "mobile":"123"
}

PUT users/_doc/2
{
  "firstName":"aaa",
  "lastName":"bbb",
  "mobile":""
}

PUT users/_doc/3
{
  "firstName":"qqq",
  "lastName":"www",
  "mobile":null
}

进行空搜索

1
2
3
4
5
6
7
8
POST /users/_search
{
  "query": {
    "match": {
      "mobile": "NULL"
    }
  }
}

查询结果

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
{
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 0.9808291,
    "hits" : [
      {
        "_index" : "users",
        "_type" : "_doc",
        "_id" : "3",
        "_score" : 0.9808291,
        "_source" : {
          "firstName" : "qqq",
          "lastName" : "www",
          "mobile" : null
        }
      }
    ]
  }
}