您当前的位置: 首页 >  ar

IT之一小佬

暂无认证

  • 1浏览

    0关注

    1192博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

ElasticSearch中的映射(mapping)

IT之一小佬 发布时间:2022-07-31 23:27:26 ,浏览量:1

        前面博客中创建的索引以及插入数据,都是由Elasticsearch进行自动判断类型,有些时候是需要进行明确字段类型的,否则,自动判断的类型和实际需求有时候是不相符的。

自动判断的规则如下:

JSON typeField typeBoolean: true or false"boolean"Whole number: 123"long"Floating point: 123.45"double"String, valid date: "2014-09-15""date"String: "foo bar""string"

Elasticsearch中支持的类型如下:

类型表示的数据类型Stringstring , text , keywordWhole numberbyte , short , integer , longFloating pointfloat , doubleBooleanbooleanDatedate

注意:

  • string类型在ElasticSearch 旧版本中使用较多,从ElasticSearch 5.x开始不再支持string,由text和 keyword类型替代。
  • text 类型,当一个字段是要被全文搜索的,比如Email内容、产品描述,应该使用text类型。设置text类型以后,字段内容会被分析,在生成倒排索引以前,字符串会被分析器分成一个一个词项。text类型的字段不用于排序,很少用于聚合。
  • keyword类型适用于索引结构化的字段,比如email地址、主机名、状态码和标签。如果字段需要进行过滤(比如查找已发布博客中status属性为published的文章)、排序、聚合。keyword类型的字段只能通过精确值搜索到。
创建明确类型的索引:
PUT /study
# 请求数据
{
    "settings": {
        "index": {
            "number_of_shards": "2",
            "number_of_replicas": "0"
        }
    },
    "mappings": {
        "properties": {
            "name": {
                "type": "text"
            },
            "age": {
                "type": "integer"
            },
            "mail": {
                "type": "keyword"
            },
            "hobby": {
                "type": "text"
            }
        }
    }
}

# 响应数据
{
    "acknowledged": true,
    "shards_acknowledged": true,
    "index": "study"
}

这儿可能存在的错误,参考博文:ElasticSearch 7.x 报错:Root mapping definition has unsupported parameters_Sunny Feng的博客-CSDN博客

查看映射:
GET /study/_mapping

# 响应数据
{
    "study": {
        "mappings": {
            "properties": {
                "age": {
                    "type": "integer"
                },
                "hobby": {
                    "type": "text"
                },
                "mail": {
                    "type": "keyword"
                },
                "name": {
                    "type": "text"
                }
            }
        }
    }
}

插入数据:
POST  /study/_bulk
# 请求数据
{"index":{"_index":"study"}} 
{"name":"张三","age": 20,"mail": "111@qq.com","hobby":"羽毛球、乒乓球、足球"} 
{"index":{"_index":"study"}} 
{"name":"李四","age": 21,"mail": "222@qq.com","hobby":"羽毛球、乒乓球、足球、篮球"} 
{"index":{"_index":"study"}} 
{"name":"王五","age": 22,"mail": "333@qq.com","hobby":"羽毛球、篮球、游泳、听音乐"} 
{"index":{"_index":"study"}} 
{"name":"赵六","age": 23,"mail": "444@qq.com","hobby":"跑步、游泳"} 
{"index":{"_index":"study"}} 
{"name":"孙七","age": 24,"mail": "555@qq.com","hobby":"听音乐、看电影"}


# 响应结果
{
    "took": 25,
    "errors": false,
    "items": [
        {
            "index": {
                "_index": "study",
                "_type": "_doc",
                "_id": "SHjOVIIBpyNh4YQ4CVSN",
                "_version": 1,
                "result": "created",
                "_shards": {
                    "total": 1,
                    "successful": 1,
                    "failed": 0
                },
                "_seq_no": 0,
                "_primary_term": 1,
                "status": 201
            }
        },
        {
            "index": {
                "_index": "study",
                "_type": "_doc",
                "_id": "SXjOVIIBpyNh4YQ4CVSN",
                "_version": 1,
                "result": "created",
                "_shards": {
                    "total": 1,
                    "successful": 1,
                    "failed": 0
                },
                "_seq_no": 0,
                "_primary_term": 1,
                "status": 201
            }
        },
        {
            "index": {
                "_index": "study",
                "_type": "_doc",
                "_id": "SnjOVIIBpyNh4YQ4CVSN",
                "_version": 1,
                "result": "created",
                "_shards": {
                    "total": 1,
                    "successful": 1,
                    "failed": 0
                },
                "_seq_no": 1,
                "_primary_term": 1,
                "status": 201
            }
        },
        {
            "index": {
                "_index": "study",
                "_type": "_doc",
                "_id": "S3jOVIIBpyNh4YQ4CVSN",
                "_version": 1,
                "result": "created",
                "_shards": {
                    "total": 1,
                    "successful": 1,
                    "failed": 0
                },
                "_seq_no": 1,
                "_primary_term": 1,
                "status": 201
            }
        },
        {
            "index": {
                "_index": "study",
                "_type": "_doc",
                "_id": "THjOVIIBpyNh4YQ4CVSN",
                "_version": 1,
                "result": "created",
                "_shards": {
                    "total": 1,
                    "successful": 1,
                    "failed": 0
                },
                "_seq_no": 2,
                "_primary_term": 1,
                "status": 201
            }
        }
    ]
}

注意:通过上面的数据库中数据可以看到,默认生成的是_doc类型的数据。

测试搜索:
POST /study/_search

# 请求数据
{
    "query": {
        "match": {
            "hobby": "音乐"
        }
    }
}

# 响应数据
{
    "took": 2,
    "timed_out": false,
    "_shards": {
        "total": 2,
        "successful": 2,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 2,
            "relation": "eq"
        },
        "max_score": 1.0470967,
        "hits": [
            {
                "_index": "study",
                "_type": "_doc",
                "_id": "THjOVIIBpyNh4YQ4CVSN",
                "_score": 1.0470967,
                "_source": {
                    "name": "孙七",
                    "age": 24,
                    "mail": "555@qq.com",
                    "hobby": "听音乐、看电影"
                }
            },
            {
                "_index": "study",
                "_type": "_doc",
                "_id": "SnjOVIIBpyNh4YQ4CVSN",
                "_score": 0.8527901,
                "_source": {
                    "name": "王五",
                    "age": 22,
                    "mail": "333@qq.com",
                    "hobby": "羽毛球、篮球、游泳、听音乐"
                }
            }
        ]
    }
}

关注
打赏
1665675218
查看更多评论
立即登录/注册

微信扫码登录

0.0564s