自定义数据类型-mapping

创建索引

curl -X PUT 192.168.1.27:9200/study-mappings

修改索引的数据类型

curl -X POST 192.168.1.27:9200/study-mappings/_mapping \
  -H "Content-Type: application/json" \
  -d '{
    "properties": {
        "name": {
            "type": "text",
            "index": true
        },
        "gender": {
            "type": "keyword",
            "index": true
        },
        "province": {
            "type": "keyword",
            "index": true
        },
        "city": {
            "type": "keyword",
            "index": false
        },
        "address": {
            "type": "keyword",
            "index": true
        },
        "email": {
            "type": "keyword",
            "index": true
        },
        "ip_addr": {
            "type": "ip"
        },
        "birthday": {
            "type": "date",
            "format": "yyyy-MM-dd"
        }
    }
}
'

查看索引的映射关系

curl -s 192.168.1.27:9200/study-mappings | jq

创建文档数据

curl -X POST 192.168.1.27:9200/_bulk \
  -H "Content-Type: application/json" \
  -d '{ "create": { "_index": "study-mappings"} }
{ "name": "Ann", "gender": "female", "province": "Sichuan", "city": "Chengduu", "address": "huayang 1hao", "email": "[email protected]", "ip_addr": "192.168.2.10", "birthday": "1992-11-01", "telephone": "13666666668" }
{ "create": { "_index": "study-mappings"} }
{ "name": "Anny", "gender": "female", "province": "Yunnan", "city": "KunMing", "address": "jianshelu 1hao", "email": "[email protected]", "ip_addr": "192.168.1.210", "birthday": "1982-10-01", "telephone": "13666666666" }
{ "create": { "_index": "study-mappings"} }
{ "name": "Bob", "gender": "male", "province": "Sichuan", "city": "Chengdu", "address": "jiannandadao 2hao", "email": "[email protected]", "ip_addr": "192.168.1.10", "birthday": "1992-10-09", "telephone": "13812345678" }
{ "create": { "_index": "study-mappings"} }
{ "name": "Cici", "gender": "female", "province": "Jiangxi", "city": "Nanchang", "address": "Kehualu 4hao", "email": "[email protected]", "ip_addr": "10.0.1.30", "birthday": "1991-09-01", "telephone": "13999999999" }
{ "create": { "_index": "study-mappings"} }
{ "name": "David", "gender": "male", "province": "Sichuan", "city": "Nanchong", "address": "qingyangqu 3hao", "email": "[email protected]", "ip_addr": "10.0.2.21", "birthday": "1985-11-11", "telephone": "15888888888", "hobby": ["抽烟", "喝酒", "烫头"] }
{ "create": { "_index": "study-mappings"} }
{ "name": "李涛", "gender": "male", "province": "Sichuan", "city": "Nanchong", "address": "qingyangqu 3hao", "email": "[email protected]", "ip_addr": "10.0.2.2", "birthday": "1985-11-11", "telephone": "15888888888", "hobby": ["抽烟", "喝酒", "烫头"] }
{ "create": { "_index": "study-mappings"} }
{ "name": "李四", "gender": "male", "province": "Sichuan", "city": "Nanchong", "address": "qingyangqu 3hao", "email": "[email protected]", "ip_addr": "10.0.2.3", "birthday": "1985-11-11", "telephone": "15888888888", "hobby": ["抽烟", "喝酒", "烫头"] }
'

查看文档数据

curl -s 192.168.1.27:9200/study-mappings/_doc/_search | jq

搜索文档数据-基于gender查询-匹配keyword类型

curl -s 192.168.1.27:9200/study-mappings/_doc/_search \
  -H "Content-Type: application/json" \
  -d '{
    "query": {
        "match": {
            "gender": "male"
        }
    }
}
' | jq

搜索文档数据-基于name查询-匹配text类型

curl -s 192.168.1.27:9200/study-mappings/_doc/_search \
  -H "Content-Type: application/json" \
  -d '{
    "query": {
        "match": {
            "name": "李"
        }
    }
}
' | jq

# 数据类型为text类型时单个中文字符会进行分词,单个英文字母不分词

搜索文档数据-基于ip_addr查询-匹配ip类型

curl -s 192.168.1.27:9200/study-mappings/_doc/_search \
  -H "Content-Type: application/json" \
  -d '{
    "query": {
        "match": {
            "ip_addr": "192.168.1.0/24"
        }
    }
}
' | jq

搜索文档数据-基于city查询-匹配keyword类型

curl -s 192.168.1.27:9200/study-mappings/_doc/_search \
  -H "Content-Type: application/json" \
  -d '{
    "query": {
        "match": {
            "city": "Chengdu"
        }
    }
}
' | jq

output:

{
  "error": {
    "root_cause": [
      {
        "type": "query_shard_exception",
        "reason": "failed to create query: Cannot search on field [city] since it is not indexed.",
        "index_uuid": "-Sab2eSJTJuOERTgxnvIwQ",
        "index": "study-mappings"
      }
    ],
    "type": "search_phase_execution_exception",
    "reason": "all shards failed",
    "phase": "query",
    "grouped": true,
    "failed_shards": [
      {
        "shard": 0,
        "index": "study-mappings",
        "node": "850f9hemSI65_nQXYB8aaQ",
        "reason": {
          "type": "query_shard_exception",
          "reason": "failed to create query: Cannot search on field [city] since it is not indexed.",
          "index_uuid": "-Sab2eSJTJuOERTgxnvIwQ",
          "index": "study-mappings",
          "caused_by": {
            "type": "illegal_argument_exception",
            "reason": "Cannot search on field [city] since it is not indexed."
          }
        }
      }
    ]
  },
  "status": 400
}

Last updated