• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

elasticsearch 常用数据类型和范例

武飞扬头像
张同学tty
帮助1

主要内容

  • elasticsearch 中的字符串(keyword)类型 的详解和范例

  • elasticsearch 中的字符串/文本(text)类型 的详解和范例

  • elasticsearch 中的数字(数值)类型 的详解和范例

  • elasticsearch 中的布尔(boolean)类型 的详解和范例

  • elasticsearch 中的日期(date)类型 的详解和范例

  • elasticsearch 中的地理(geo_point、geo_shape)类型 的详解和范例

  • elasticsearch 中的对象类型 的详解和范例

  • elasticsearch 中的数组类型 的详解和范例

概要

简述

在Elasticsearch的映射关系中,每个字段都对应一个数据类型或者字段类型,这些类型规范了字段存储的值和用途。例如,可以将字符串索引到text和keyword字段。text字段的值用于全文搜索;keyword字段的值存储时不会被分词建立索引,主要用于统计计算等操作。

内容

字符串(keyword)类型 详解

  • keyword类型用于存储结构化的内容

  • keyword类型是不进行切分的字符串类型

  • 不进行切分

  • 在索引时,对keyword类型的数据不进行切分,直接构建倒排索引

  • 在搜索时,对该类型的查询字符串不进行切分后的部分匹配

  • keyword类型数据一般用于对文档的过滤、排序和聚合

  • 在现实场景中,keyword经常用于描述ID、电子邮件、主机名、邮政编码、标签、姓名、产品类型、用户ID、URL和状态码等

  • keyword类型数据一般用于比较字符串是否相等,不对数据进行部分匹配,因此一般查询这种类型的数据时使用term查询

字符串(keyword)类型 范例

1.创建user索引库并插入一条数据

  1.  
    #建立一个人名索引,设定姓名字段为keyword字段
  2.  
    PUT /user
  3.  
    {
  4.  
    "mappings": {
  5.  
    "properties": {
  6.  
    "user_name": {
  7.  
    "type": "keyword"
  8.  
    }
  9.  
    }
  10.  
    }
  11.  
    }
  12.  
     
  13.  
    #插入数据
  14.  
    PUT /user/_doc/001
  15.  
    {
  16.  
    "user_name": "张三"
  17.  
    }

2.使用term查询刚刚写入的数据

  1.  
    #使用term查询刚刚写入的数据
  2.  
    GET /user/_search
  3.  
    {
  4.  
    "query": {
  5.  
    "term": {
  6.  
    "user_name": {
  7.  
    "value": "张三"
  8.  
    }
  9.  
    }
  10.  
    }
  11.  
    }
  12.  
     
  13.  
    #返回的结果如下
  14.  
    {
  15.  
    "took": 0,
  16.  
    "timed_out": false,
  17.  
    "_shards": {
  18.  
    "total": 1,
  19.  
    "successful": 1,
  20.  
    "skipped": 0,
  21.  
    "failed": 0
  22.  
    },
  23.  
    "hits": {
  24.  
    "total": {
  25.  
    "value": 1,
  26.  
    "relation": "eq"
  27.  
    },
  28.  
    "max_score": 0.2876821,
  29.  
    "hits": [
  30.  
    {
  31.  
    "_index": "user",
  32.  
    "_type": "_doc",
  33.  
    "_id": "001",
  34.  
    "_score": 0.2876821,
  35.  
    "_source": {
  36.  
    "user_name": "张三"
  37.  
    }
  38.  
    }
  39.  
    ]
  40.  
    }
  41.  
    }
  • 由搜索结果可以看出,使用term进行全字符串匹配"张三"可以搜索到命中文档。

3.使用match查询刚刚写入的数据中带有"张"的记录

  1.  
    #使用match查询刚刚写入的数据中带有"张"的记录
  2.  
    GET /user/_search
  3.  
    {
  4.  
    "query": {
  5.  
    "match": {
  6.  
    "user_name": "张"
  7.  
    }
  8.  
    }
  9.  
    }
  10.  
     
  11.  
    #返回的结果如下
  12.  
    {
  13.  
    "took": 0,
  14.  
    "timed_out": false,
  15.  
    "_shards": {
  16.  
    "total": 1,
  17.  
    "successful": 1,
  18.  
    "skipped": 0,
  19.  
    "failed": 0
  20.  
    },
  21.  
    "hits": {
  22.  
    "total": {
  23.  
    "value": 0,
  24.  
    "relation": "eq"
  25.  
    },
  26.  
    "max_score": null,
  27.  
    "hits": []
  28.  
    }
  29.  
    }
  • 由搜索结果可以看出,对keyword类型使用match搜索进行匹配是不会命中文档的。

字符串/文本(text)类型 的详解

  • text类型是可进行切分的字符串类型。

    • 可切分

      • 在索引时,可按照相应的切词算法对文本内容进行切分,然后构建倒排索引

      • 在搜索时,对该类型的查询字符串按照用户的切词算法进行切分,然后对切分后的部分匹配打分

  • text类型用于进行全文搜索(也称为全文检索),text类型允许用户在每个全文字段中搜索单个单词

  • 在现实场景中,text经常用于电子邮箱正文或产品描述的全文等

  • text不适合进行排序,也不适合进行聚合计算。如果字段需要聚合计算或者排序,推荐使用keyword类型

字符串/文本(text)类型 的范例(一)

1.创建一个hotel索引库,并插入一条数据

  1.  
    #建立一个hotel索引,可以设定title字段为text字段
  2.  
    PUT /hotel
  3.  
    {
  4.  
    "mappings": {
  5.  
    "properties": {
  6.  
    "title": {
  7.  
    "type": "text"
  8.  
    }
  9.  
    }
  10.  
    }
  11.  
    }
  12.  
     
  13.  
    #插入数据
  14.  
    POST /hotel/_doc/001
  15.  
    {
  16.  
    "title":"文雅酒店"
  17.  
    }

2.使用term查询刚刚写入的数据

  1.  
    #按照普通的term进行搜索
  2.  
    GET /hotel/_search
  3.  
    {
  4.  
    "query": {
  5.  
    "term": {
  6.  
    "title": {
  7.  
    "value": "文雅酒店"
  8.  
    }
  9.  
    }
  10.  
    }
  11.  
    }
  12.  
     
  13.  
    #返回的结果如下
  14.  
    {
  15.  
    "took" : 570,
  16.  
    "timed_out" : false,
  17.  
    "_shards" : {
  18.  
    "total" : 1,
  19.  
    "successful" : 1,
  20.  
    "skipped" : 0,
  21.  
    "failed" : 0
  22.  
    },
  23.  
    "hits" : {
  24.  
    "total" : {
  25.  
    "value" : 0,
  26.  
    "relation" : "eq"
  27.  
    },
  28.  
    "max_score" : null,
  29.  
    "hits" : [ ]
  30.  
    }
  31.  
    }
  • 根据返回的结果可知,上面的请求并没有搜索到文档。

  • term搜索用于搜索值和文档对应的字段是否完全相等,而对于text类型的数据,在建立索引时ES已经进行了切分并建立了倒排索引,因此使用term没有查询到数据。一般情况下,搜索text类型的数据时应使用match搜索。

3.使用match查询刚刚写入的数据中带有"文雅"的记录

  1.  
    #按照普通的match进行搜索
  2.  
    GET /hotel/_search
  3.  
    {
  4.  
    "query": {
  5.  
    "match": {
  6.  
    "title": "文雅"
  7.  
    }
  8.  
    }
  9.  
    }
  10.  
     
  11.  
    #返回结果如下
  12.  
    {
  13.  
    "took" : 1,
  14.  
    "timed_out" : false,
  15.  
    "_shards" : {
  16.  
    "total" : 1,
  17.  
    "successful" : 1,
  18.  
    "skipped" : 0,
  19.  
    "failed" : 0
  20.  
    },
  21.  
    "hits" : {
  22.  
    "total" : {
  23.  
    "value" : 1,
  24.  
    "relation" : "eq"
  25.  
    },
  26.  
    "max_score" : 0.5753642,
  27.  
    "hits" : [
  28.  
    {
  29.  
    "_index" : "hotel",
  30.  
    "_type" : "_doc",
  31.  
    "_id" : "001",
  32.  
    "_score" : 0.5753642,
  33.  
    "_source" : {
  34.  
    "title" : "文雅酒店"
  35.  
    }
  36.  
    }
  37.  
    ]
  38.  
    }
  39.  
    }

字符串/文本(text)类型 的范例(二)

1.创建myindex-2_13索引库,并插入数据

  1.  
    #创建索引映射并指定tagname字段的字段类型为text类型
  2.  
    PUT myindex-2_13
  3.  
    {
  4.  
    "mappings": {
  5.  
    "properties": {
  6.  
    "tagname": {
  7.  
    "type": "text"
  8.  
    }
  9.  
    }
  10.  
    }
  11.  
    }
  12.  
     
  13.  
    #插入文档数据
  14.  
    PUT myindex-2_13/_doc/1
  15.  
    {
  16.  
    "tagname":"江苏省"
  17.  
    }
  18.  
     
  19.  
    #插入文档数据
  20.  
    PUT myindex-2_13/_doc/2
  21.  
    {
  22.  
    "tagname":"河北省"
  23.  
    }

2.根据tagname字段内容分词,然后对所有分词进行匹配搜索

  1.  
    #根据tagname字段内容分词,然后对所有分词进行匹配搜索
  2.  
    GET myindex-2_13/_doc/_search
  3.  
    {
  4.  
    "query": {
  5.  
    "match": {
  6.  
    "tagname": "河南省"
  7.  
    }
  8.  
    }
  9.  
    }
  10.  
     
  11.  
    #返回的结果如下
  12.  
    {
  13.  
    "took" : 2,
  14.  
    "timed_out" : false,
  15.  
    "_shards" : {
  16.  
    "total" : 1,
  17.  
    "successful" : 1,
  18.  
    "skipped" : 0,
  19.  
    "failed" : 0
  20.  
    },
  21.  
    "hits" : {
  22.  
    "total" : {
  23.  
    "value" : 2,
  24.  
    "relation" : "eq"
  25.  
    },
  26.  
    "max_score" : 0.8754687,
  27.  
    "hits" : [
  28.  
    {
  29.  
    "_index" : "myindex-2_13",
  30.  
    "_type" : "_doc",
  31.  
    "_id" : "2",
  32.  
    "_score" : 0.8754687,
  33.  
    "_source" : {
  34.  
    "tagname" : "河北省"
  35.  
    }
  36.  
    },
  37.  
    {
  38.  
    "_index" : "myindex-2_13",
  39.  
    "_type" : "_doc",
  40.  
    "_id" : "1",
  41.  
    "_score" : 0.18232156,
  42.  
    "_source" : {
  43.  
    "tagname" : "江苏省"
  44.  
    }
  45.  
    }
  46.  
    ]
  47.  
    }
  48.  
    }
  • 以上搜索结果中把"江苏省"和"河北省"这两行数据都返回了,这是因为目前默认的分词器把"河南省"分了"河"、"南"、"省"三个词,而"河北省"和"江苏省"分别分成"河"、"北"、"省"和"江"、"苏"、"省",这两个词被分词后都有一个"省"字,所以搜索时被全文匹配到了。在实际业务中,如果我们要对字段的内容进行全文搜索,可以使用text类型;如果要聚合查询或者精准匹配,则尽量使用keyword类型。

字符串/文本(text)类型 的范例(三)

  • 对于大多数想要对文本字段执行更过操作的用户,也可以使用多字段映射,其中既有text类型可以用于全文检索,又有keyword类型可以用于聚合分析,语法如下:

    1.  
      PUT 索引库名称
    2.  
      {
    3.  
      "mappings": {
    4.  
      "properties": {
    5.  
      "my_field": {
    6.  
      "type": "text",
    7.  
      "fields": {
    8.  
      "keyword": {
    9.  
      "type": "keyword"
    10.  
      }
    11.  
      }
    12.  
      }
    13.  
      }
    14.  
      }
    15.  
      }
  • 由以上语句可知,my_field字段的映射关系是:父字段类型是text类型,子字段类型是keyword类型。

1.创建myindex-2_14索引库,并插入数据

  1.  
    #创建索引映射
  2.  
    PUT myindex-2_14
  3.  
    {
  4.  
    "mappings": {
  5.  
    "properties": {
  6.  
    "tagname": {
  7.  
    "type": "text",
  8.  
    "fields": {
  9.  
    "keyword": {
  10.  
    "type": "keyword"
  11.  
    }
  12.  
    }
  13.  
    }
  14.  
    }
  15.  
    }
  16.  
    }
  17.  
     
  18.  
    #插入文档数据
  19.  
    PUT myindex-2_14/_doc/1
  20.  
    {
  21.  
    "tagname":"江苏省"
  22.  
    }
  23.  
     
  24.  
    #插入文档数据
  25.  
    PUT myindex-2_14/_doc/2
  26.  
    {
  27.  
    "tagname":"河北省"
  28.  
    }

2.根据父字段(text类型)搜索符合要求的文档数据

  1.  
    #根据父字段(text类型)搜索符合要求的文档数据
  2.  
    GET myindex-2_14/_doc/_search
  3.  
    {
  4.  
    "query": {
  5.  
    "match": {
  6.  
    "tagname": "河南省"
  7.  
    }
  8.  
    }
  9.  
    }
  10.  
     
  11.  
    #返回的结果为
  12.  
    {
  13.  
    "took" : 1029,
  14.  
    "timed_out" : false,
  15.  
    "_shards" : {
  16.  
    "total" : 1,
  17.  
    "successful" : 1,
  18.  
    "skipped" : 0,
  19.  
    "failed" : 0
  20.  
    },
  21.  
    "hits" : {
  22.  
    "total" : {
  23.  
    "value" : 2,
  24.  
    "relation" : "eq"
  25.  
    },
  26.  
    "max_score" : 0.8754687,
  27.  
    "hits" : [
  28.  
    {
  29.  
    "_index" : "myindex-2_14",
  30.  
    "_type" : "_doc",
  31.  
    "_id" : "2",
  32.  
    "_score" : 0.8754687,
  33.  
    "_source" : {
  34.  
    "tagname" : "河北省"
  35.  
    }
  36.  
    },
  37.  
    {
  38.  
    "_index" : "myindex-2_14",
  39.  
    "_type" : "_doc",
  40.  
    "_id" : "1",
  41.  
    "_score" : 0.18232156,
  42.  
    "_source" : {
  43.  
    "tagname" : "江苏省"
  44.  
    }
  45.  
    }
  46.  
    ]
  47.  
    }
  48.  
    }
  • 其中,使用tagname字段的父字段(text类型)进行搜索,因为父类型是text类型。所以搜索时会进行分词。结果返回了包含"河北省"和"江苏省"的文档信息

3.利用tagname字段的子字段(keyword类型)进行匹配查询

  1.  
    #利用tagname字段的子字段(keyword类型)进行匹配查询
  2.  
    GET myindex-2_14/_search
  3.  
    {
  4.  
    "query": {
  5.  
    "match": {
  6.  
    "tagname.keyword": "江苏省"
  7.  
    }
  8.  
    }
  9.  
    }
  10.  
     
  11.  
    #返回结果为
  12.  
    {
  13.  
    "took" : 1,
  14.  
    "timed_out" : false,
  15.  
    "_shards" : {
  16.  
    "total" : 1,
  17.  
    "successful" : 1,
  18.  
    "skipped" : 0,
  19.  
    "failed" : 0
  20.  
    },
  21.  
    "hits" : {
  22.  
    "total" : {
  23.  
    "value" : 1,
  24.  
    "relation" : "eq"
  25.  
    },
  26.  
    "max_score" : 0.6931471,
  27.  
    "hits" : [
  28.  
    {
  29.  
    "_index" : "myindex-2_14",
  30.  
    "_type" : "_doc",
  31.  
    "_id" : "1",
  32.  
    "_score" : 0.6931471,
  33.  
    "_source" : {
  34.  
    "tagname" : "江苏省"
  35.  
    }
  36.  
    }
  37.  
    ]
  38.  
    }
  39.  
    }
  • 其中,tagname.keyword代表使用了tagname字段的子字段(keyword类型)进行了不分词搜索,需要保证搜索的内容和字段存储的内容完全匹配,所以从当前索引库中匹配到了数据。

elasticsearch 中的数字(数值)类型 的详解

  • elasticsearch支持的数据类型有long、integer、short、byte、double、float、scaled_float、half_float和unsigned_long等。各类型所表达的数值范围可以参考官方文档,网址为Numeric field types | Elasticsearch Guide [8.7] | Elastic

  • 为节约存储空间并提升搜索和索引的效率,在实际应用中,在满足需求的情况下应尽可能选择范围小的数据类型。比如,年龄字段的取值最大值不会超过200,因此选择byte类型即可

  • 数值类型的数据也可用于对进行过滤、排序和聚合

  • 对于数值型数据,一般使用term搜索或者范围搜索

elasticsearch 中的数字(数值)类型 的范例(一)

1.更新hotel索引库的mapping,并为hotel索引库定义价格、星级和评论数等字段;更新后再插入数据。

  1.  
    #一个酒店搜索项目,酒店的索引除了包含酒店名称和城市之外,还需要定义价格、星级和评论数等。
  2.  
    PUT /hotel/_mapping
  3.  
    {
  4.  
    "properties": {
  5.  
    "city": {
  6.  
    "type": "keyword"
  7.  
    },
  8.  
    "price": {
  9.  
    "type": "double"
  10.  
    },
  11.  
    "start": {
  12.  
    "type": "byte"
  13.  
    },
  14.  
    "comment_count": {
  15.  
    "type": "integer"
  16.  
    }
  17.  
    }
  18.  
    }
  19.  
     
  20.  
    #插入数据
  21.  
    POST /hotel/_doc/001
  22.  
    {
  23.  
    "title":"文雅酒店",
  24.  
    "city":"上海",
  25.  
    "price":270,
  26.  
    "start":10,
  27.  
    "comment_count":2
  28.  
    }

2.搜索价格为200~300(包含200和300)的酒店

  1.  
    #搜索价格为200~300(包含200300)的酒店
  2.  
    GET /hotel/_search
  3.  
    {
  4.  
    "query": {
  5.  
    "range": {
  6.  
    "price": {
  7.  
    "gte": 200,
  8.  
    "lte": 300
  9.  
    }
  10.  
    }
  11.  
    }
  12.  
    }
  13.  
     
  14.  
    #返回结果如下
  15.  
    {
  16.  
    "took" : 596,
  17.  
    "timed_out" : false,
  18.  
    "_shards" : {
  19.  
    "total" : 1,
  20.  
    "successful" : 1,
  21.  
    "skipped" : 0,
  22.  
    "failed" : 0
  23.  
    },
  24.  
    "hits" : {
  25.  
    "total" : {
  26.  
    "value" : 1,
  27.  
    "relation" : "eq"
  28.  
    },
  29.  
    "max_score" : 1.0,
  30.  
    "hits" : [
  31.  
    {
  32.  
    "_index" : "hotel",
  33.  
    "_type" : "_doc",
  34.  
    "_id" : "001",
  35.  
    "_score" : 1.0,
  36.  
    "_source" : {
  37.  
    "title" : "文雅酒店",
  38.  
    "city" : "上海",
  39.  
    "price" : 270,
  40.  
    "start" : 10,
  41.  
    "comment_count" : 2
  42.  
    }
  43.  
    }
  44.  
    ]
  45.  
    }
  46.  
    }

elasticsearch 中的数字(数值)类型 的范例(二)

1.创建索引并创建字段映射关系

  1.  
    #创建索引并创建字段映射关系
  2.  
    PUT myindex-2_09
  3.  
    {
  4.  
    "mappings": {
  5.  
    "properties": {
  6.  
    "number": {
  7.  
    "type": "integer"
  8.  
    },
  9.  
    "time_in_secondes": {
  10.  
    "type": "float"
  11.  
    },
  12.  
    "price": {
  13.  
    "type": "scaled_float",
  14.  
    "scaling_factor": 100
  15.  
    }
  16.  
    }
  17.  
    }
  18.  
    }
  19.  
     
  20.  
    #插入文档数据
  21.  
    PUT myindex-2_09/_doc/1?refresh
  22.  
    {
  23.  
    "number":1,
  24.  
    "time_in_secondes":1.001,
  25.  
    "price":1.11
  26.  
    }
  • 在以上语句创建的索引映射中,"scaling_factor"参数表示数值在存储时使用了缩放因子,该值在存储时乘以缩放因子并四舍五入到最接近long类型的值(比如1.11实际存储的数据是111).注意,这个参数是必不可少的。

  • 就上面范例中的数字类型而言,他们可以存储任何数字,但是我们在使用时尽量选择可以满足需求的最小数值类型,这样可以更有效地编制索引和进行搜索,同时也可以节省一部分的存储空间。

2.精确查询price为1.11的数据

  1.  
    #精确查询price为1.11的数据
  2.  
    GET myindex-2_09/_search
  3.  
    {
  4.  
    "query": {
  5.  
    "term": {
  6.  
    "price": {
  7.  
    "value": "1.11"
  8.  
    }
  9.  
    }
  10.  
    }
  11.  
    }

elasticsearch 中的布尔(boolean)类型 的详解

  • 布尔字段接受JSON格式的true和false,但也可以接受解释为真或假的字符串,false,"false",true,"true"

  • 布尔类型使用boolean定义,用于业务中的二值表示,如商品是否售罄,房屋是否已租,酒店房间是否满房等。

elasticsearch 中的布尔(boolean)类型 的范例(一)

1.一个酒店搜索项目,酒店的索引除了包含酒店名称、城市、价格、星级、评论数之外,还需要定义是否 满房等。其次插入数据

  1.  
    #一个酒店搜索项目,酒店的索引除了包含酒店名称、城市、价格、星级、评论数之外,还需要定义是否
  2.  
    满房等。
  3.  
    PUT /hotel/_mapping
  4.  
    {
  5.  
    "properties": {
  6.  
    "full_room": {
  7.  
    "type": "boolean"
  8.  
    }
  9.  
    }
  10.  
    }
  11.  
     
  12.  
    #插入数据
  13.  
    POST /hotel/_doc/001
  14.  
    {
  15.  
    "title":"文雅酒店",
  16.  
    "city":"上海",
  17.  
    "price":270,
  18.  
    "start":10,
  19.  
    "comment_count":2,
  20.  
    "full_room":true
  21.  
    }

2.查询满房的酒店

  1.  
    #查询满房的酒店
  2.  
    GET hotel/_search
  3.  
    {
  4.  
    "query": {
  5.  
    "term": {
  6.  
    "full_room": {
  7.  
    "value": "true"
  8.  
    }
  9.  
    }
  10.  
    }
  11.  
    }
  12.  
     
  13.  
    #返回结果如下
  14.  
    {
  15.  
    "took" : 0,
  16.  
    "timed_out" : false,
  17.  
    "_shards" : {
  18.  
    "total" : 1,
  19.  
    "successful" : 1,
  20.  
    "skipped" : 0,
  21.  
    "failed" : 0
  22.  
    },
  23.  
    "hits" : {
  24.  
    "total" : {
  25.  
    "value" : 1,
  26.  
    "relation" : "eq"
  27.  
    },
  28.  
    "max_score" : 0.2876821,
  29.  
    "hits" : [
  30.  
    {
  31.  
    "_index" : "hotel",
  32.  
    "_type" : "_doc",
  33.  
    "_id" : "001",
  34.  
    "_score" : 0.2876821,
  35.  
    "_source" : {
  36.  
    "title" : "文雅酒店",
  37.  
    "city" : "上海",
  38.  
    "price" : 270,
  39.  
    "start" : 10,
  40.  
    "comment_count" : 2,
  41.  
    "full_room" : true
  42.  
    }
  43.  
    }
  44.  
    ]
  45.  
    }
  46.  
    }

elasticsearch 中的布尔(boolean)类型 的范例(二)

1.创建索引myindex-2_03映射并指定is_published字段类型为布尔类型

  1.  
    #创建索引映射并指定is_published字段类型为布尔类型
  2.  
    PUT myindex-2_03
  3.  
    {
  4.  
    "mappings": {
  5.  
    "properties": {
  6.  
    "is_published": {
  7.  
    "type": "boolean"
  8.  
    }
  9.  
    }
  10.  
    }
  11.  
    }
  12.  
     
  13.  
    #新增数据,字段值必须和映射类型匹配
  14.  
    POST myindex-2_03/_doc/1?pretty
  15.  
    {
  16.  
    "is_published": "true"
  17.  
    }
  18.  
     
  19.  
    POST myindex-2_03/_doc/2?pretty
  20.  
    {
  21.  
    "is_published": true
  22.  
    }

2.查询索引库中is_publish字段的值是true的数据

  1.  
    #查询索引库中is_publish字段的值是true的数据
  2.  
    GET myindex-2_03/_search
  3.  
    {
  4.  
    "query": {
  5.  
    "term": {
  6.  
    "is_published": {
  7.  
    "value": "true"
  8.  
    }
  9.  
    }
  10.  
    }
  11.  
    }
  12.  
    #返回结果如下
  13.  
    {
  14.  
    "took" : 0,
  15.  
    "timed_out" : false,
  16.  
    "_shards" : {
  17.  
    "total" : 1,
  18.  
    "successful" : 1,
  19.  
    "skipped" : 0,
  20.  
    "failed" : 0
  21.  
    },
  22.  
    "hits" : {
  23.  
    "total" : {
  24.  
    "value" : 1,
  25.  
    "relation" : "eq"
  26.  
    },
  27.  
    "max_score" : 0.6931471,
  28.  
    "hits" : [
  29.  
    {
  30.  
    "_index" : "myindex-2_03",
  31.  
    "_type" : "_doc",
  32.  
    "_id" : "1",
  33.  
    "_score" : 0.6931471,
  34.  
    "_source" : {
  35.  
    "is_published" : "true"
  36.  
    }
  37.  
    }
  38.  
    ]
  39.  
    }
  40.  
    }

3.在使用布尔类型字段时需要注意的是,布尔类型的查询不能使用0或者1代替,否则会抛出异常

  1.  
    #在使用布尔类型字段时需要注意的是,布尔类型的查询不能使用0或者1代替,否则会抛出异常
  2.  
    POST myindex-2_03/_doc/3?pretty
  3.  
    {
  4.  
    "is_published": 1
  5.  
    }
  6.  
     
  7.  
    #返回结果如下
  8.  
    {
  9.  
    "error" : {
  10.  
    "root_cause" : [
  11.  
    {
  12.  
    "type" : "mapper_parsing_exception",
  13.  
    "reason" : "failed to parse field [is_published] of type [boolean] in
  14.  
    document with id '3'. Preview of field's value: '1'"
  15.  
    }
  16.  
    ],
  17.  
    "type" : "mapper_parsing_exception",
  18.  
    "reason" : "failed to parse field [is_published] of type [boolean] in document
  19.  
    with id '3'. Preview of field's value: '1'",
  20.  
    "caused_by" : {
  21.  
    "type" : "json_parse_exception",
  22.  
    "reason" : "Current token (VALUE_NUMBER_INT) not of boolean type\n at [Source:
  23.  
    (ByteArrayInputStream); line: 2, column: 20]"
  24.  
    }
  25.  
    },
  26.  
    "status" : 400
  27.  
    }
  28.  
     
  29.  
    #可以看到,使用1作为筛选值进行查询时不能正确地转换为布尔类型的值。如果需要对布尔值进行转换,可以使用
  30.  
    "运行时"脚本来处理
  31.  
    GET myindex-2_03/_search
  32.  
    {
  33.  
    "fields": [
  34.  
    {
  35.  
    "field": "weight"
  36.  
    }
  37.  
    ],
  38.  
    "runtime_mappings": {
  39.  
    "weight": {
  40.  
    "type": "long",
  41.  
    "script": "emit(doc['is_published'].value?1:0)"
  42.  
    }
  43.  
    }
  44.  
    }
  45.  
    #返回结果如下
  46.  
    {
  47.  
    "took" : 1,
  48.  
    "timed_out" : false,
  49.  
    "_shards" : {
  50.  
    "total" : 1,
  51.  
    "successful" : 1,
  52.  
    "skipped" : 0,
  53.  
    "failed" : 0
  54.  
    },
  55.  
    "hits" : {
  56.  
    "total" : {
  57.  
    "value" : 2,
  58.  
    "relation" : "eq"
  59.  
    },
  60.  
    "max_score" : 1.0,
  61.  
    "hits" : [
  62.  
    {
  63.  
    "_index" : "myindex-2_03",
  64.  
    "_type" : "_doc",
  65.  
    "_id" : "2",
  66.  
    "_score" : 1.0,
  67.  
    "_source" : {
  68.  
    "is_published" : false
  69.  
    },
  70.  
    "fields" : {
  71.  
    "weight" : [
  72.  
    0
  73.  
    ]
  74.  
    }
  75.  
    },
  76.  
    {
  77.  
    "_index" : "myindex-2_03",
  78.  
    "_type" : "_doc",
  79.  
    "_id" : "1",
  80.  
    "_score" : 1.0,
  81.  
    "_source" : {
  82.  
    "is_published" : "true"
  83.  
    },
  84.  
    "fields" : {
  85.  
    "weight" : [
  86.  
    1
  87.  
    ]
  88.  
    }
  89.  
    }
  90.  
    ]
  91.  
    }
  92.  
    }

elasticsearch 中的日期(date)类型 的详解

  • JSON格式规范中没有对日期数据类型进行定义。

  • elasticsearch一般使用如下形式表示日期类型数据

    • 格式化的日期字符串,例如 2015-01-01 或 2015/01/01 12:10:30

    • 毫秒级的长整型(一个表示自纪元以来毫秒数的长整形数字),表示从1970年1月1日0点到现在的毫秒数

    • 秒级别的整形(表示从纪元开始的秒数的整数),表示从1970年1月1日0点到现在的秒数

  • 在Elasticsearch内部,日期转换为UTC(如果指定了时区),并存储为毫秒数时间戳。

  • 日期类型的默认格式为strict_date_optional_time||epoch_millis。其中,strict_date_optional_time的含义是严格的时间类型,支持yyyy-MM-dd、yyyyMMdd、yyyyMMddHHmmss、yyyy-MM-ddTHH:mm:ss、yyyy-MM-ddTHH:mm:ss.SSS和yyyy-MM-ddTHH:mm:ss:SSSZ等格式;epoch_millis的含义是从1970年1月1日0点到现在的毫秒数。

  • Elasticsearch中的日期类型可以时包含日期格式的字符串,例如"2021-01-01"或"2021/01/01 12:10:30"等格式,也可以使用自纪元以来的毫秒数来表示(注:在Unix中,纪元是指UTC时间1970年1月1日00:00:00)。

  • 对日期的查询在内部转换为范围查询,聚合和存储字段的结果将根据与字段关联的日期格式转换回字符串。

  • 日期类型默认不支持yyyy-MM-dd HH:mm:ss格式,如果经常使用这种格式,可以在索引的mapping中设置日期字段的 format属性为自定义格式。

  • 搜索日期数据时,一般使用范围查询。

elasticsearch 中的日期(date)类型 的范例(一)

1.一个酒店搜索项目,酒店的索引除了包含酒店名称、城市、价格、星级、评论数、是否满房之外,还需要定义日期等。

  1.  
    #一个酒店搜索项目,酒店的索引除了包含酒店名称、城市、价格、星级、评论数、是否满房之外,还需要定义日期等。
  2.  
    PUT /hotel/_mapping
  3.  
    {
  4.  
    "properties": {
  5.  
    "create_time": {
  6.  
    "type": "date"
  7.  
    }
  8.  
    }
  9.  
    }
  10.  
     
  11.  
    #插入数据
  12.  
    POST /hotel/_doc/001
  13.  
    {
  14.  
    "title":"文雅酒店",
  15.  
    "city":"上海",
  16.  
    "price":270,
  17.  
    "start":10,
  18.  
    "comment_count":2,
  19.  
    "full_room":true,
  20.  
    "create_time":"20210115"
  21.  
    }

2.搜索创建日期为2015年的酒店

  1.  
    #搜索创建日期为2015年的酒店
  2.  
    GET hotel/_search
  3.  
    {
  4.  
    "query": {
  5.  
    "range": {
  6.  
    "create_time": {
  7.  
    "gte": "20150101",
  8.  
    "lt": "20220112"
  9.  
    }
  10.  
    }
  11.  
    }
  12.  
    }
  13.  
     
  14.  
    #返回结果如下
  15.  
    {
  16.  
    "took" : 14,
  17.  
    "timed_out" : false,
  18.  
    "_shards" : {
  19.  
    "total" : 1,
  20.  
    "successful" : 1,
  21.  
    "skipped" : 0,
  22.  
    "failed" : 0
  23.  
    },
  24.  
    "hits" : {
  25.  
    "total" : {
  26.  
    "value" : 1,
  27.  
    "relation" : "eq"
  28.  
    },
  29.  
    "max_score" : 1.0,
  30.  
    "hits" : [
  31.  
    {
  32.  
    "_index" : "hotel",
  33.  
    "_type" : "_doc",
  34.  
    "_id" : "001",
  35.  
    "_score" : 1.0,
  36.  
    "_source" : {
  37.  
    "title" : "文雅酒店",
  38.  
    "city" : "上海",
  39.  
    "price" : 270,
  40.  
    "start" : 10,
  41.  
    "comment_count" : 2,
  42.  
    "full_room" : true,
  43.  
    "create_time" : "20210115"
  44.  
    }
  45.  
    }
  46.  
    ]
  47.  
    }
  48.  
    }

3.删除原来的hotel索引库,构建新的hotel索引库,并设置create_time字段的格式为yyyy-MM-dd HH:mm:ss

  1.  
    #设置create_time字段的格式为yyyy-MM-dd HH:mm:ss
  2.  
    PUT /hotel
  3.  
    {
  4.  
    "mappings": {
  5.  
    "properties": {
  6.  
    "title": {
  7.  
    "type": "text"
  8.  
    },
  9.  
    "city": {
  10.  
    "type": "keyword"
  11.  
    },
  12.  
    "price": {
  13.  
    "type": "double"
  14.  
    },
  15.  
    "start": {
  16.  
    "type": "byte"
  17.  
    },
  18.  
    "comment_count": {
  19.  
    "type": "integer"
  20.  
    },
  21.  
    "full_room": {
  22.  
    "type": "boolean"
  23.  
    },
  24.  
    "create_time": {
  25.  
    "type": "date",
  26.  
    "format": "yyyy-MM-dd HH:mm:ss"
  27.  
    }
  28.  
    }
  29.  
    }
  30.  
    }
  31.  
     
  32.  
    #插入数据
  33.  
    POST /hotel/_doc/001
  34.  
    {
  35.  
    "title":"文雅酒店",
  36.  
    "city":"上海",
  37.  
    "price":270,
  38.  
    "start":10,
  39.  
    "comment_count":2,
  40.  
    "full_room":true,
  41.  
    "create_time":"20210115"
  42.  
    }
  43.  
     
  44.  
    #返回结果如下
  45.  
    {
  46.  
    "error" : {
  47.  
    "root_cause" : [
  48.  
    {
  49.  
    "type" : "mapper_parsing_exception",
  50.  
    "reason" : "failed to parse field [create_time] of type [date] in document
  51.  
    with id '001'. Preview of field's value: '20210115'"
  52.  
    }
  53.  
    ],
  54.  
    "type" : "mapper_parsing_exception",
  55.  
    "reason" : "failed to parse field [create_time] of type [date] in document
  56.  
    with id '001'. Preview of field's value: '20210115'",
  57.  
    "caused_by" : {
  58.  
    "type" : "illegal_argument_exception",
  59.  
    "reason" : "failed to parse date field [20210115] with format [yyyy-MM-dd
  60.  
    HH:mm:ss]",
  61.  
    "caused_by" : {
  62.  
    "type" : "date_time_parse_exception",
  63.  
    "reason" : "Text '20210115' could not be parsed at index 0"
  64.  
    }
  65.  
    }
  66.  
    },
  67.  
    "status" : 400
  68.  
    }
  • 根据错误信息可知,错误的原因是写入的数据格式和定义的数据格式不同

3.插入create_time的格式为yyyy-MM-dd HH:mm:ss数据

  1.  
    #插入create_time的格式为yyyy-MM-dd HH:mm:ss数据
  2.  
    POST /hotel/_doc/001
  3.  
    {
  4.  
    "title":"文雅酒店",
  5.  
    "city":"上海",
  6.  
    "price":270,
  7.  
    "start":10,
  8.  
    "comment_count":2,
  9.  
    "full_room":true,
  10.  
    "create_time":"2021-01-15 01:23:30"
  11.  
    }

elasticsearch 中的日期(date)类型 的范例(二)

  1.  
    #创建索引映射并指定date字段的字段类型为日期类型
  2.  
    PUT myindex-2_04
  3.  
    {
  4.  
    "mappings": {
  5.  
    "properties": {
  6.  
    "date":{
  7.  
    "type": "date"
  8.  
    }
  9.  
    }
  10.  
    }
  11.  
    }
  12.  
     
  13.  
    #插入文档数据
  14.  
    PUT myindex-2_04/_doc/1
  15.  
    {
  16.  
    "date":"2015-01-01"
  17.  
    }
  18.  
     
  19.  
    #插入文档数据
  20.  
    PUT myindex-2_04/_doc/2
  21.  
    {
  22.  
    "date": "2015-01-01T12:10:30Z"
  23.  
    }
  24.  
     
  25.  
    #插入文档数据
  26.  
    PUT myindex-2_04/_doc/3
  27.  
    {
  28.  
    "date":1420070400001
  29.  
    }
  30.  
    #以上3种不同格式的日期数据都被插入到索引库中。
  31.  
     
  32.  
    #创建索引并为日期类型的字段指定具体的日期格式
  33.  
    PUT myindex-2_05
  34.  
    {
  35.  
    "mappings": {
  36.  
    "properties": {
  37.  
    "date": {
  38.  
    "type": "date",
  39.  
    "format": "yyyy-MM-dd HH:mm:ss"
  40.  
    }
  41.  
    }
  42.  
    }
  43.  
    }
  44.  
    #以下语句插入文档数据时将会抛出异常,因为日期内容不符合映射格式
  45.  
    PUT myindex-2_05/_doc/1
  46.  
    {
  47.  
    "date":"2015-01-01"
  48.  
    }
  49.  
     
  50.  
    #以下语句插入文档数据时将会抛出异常,因为日期内容不符合映射格式
  51.  
    PUT myindex-2_05/_doc/2
  52.  
    {
  53.  
    "date":"2015-01-01T12:10:30Z"
  54.  
    }
  55.  
     
  56.  
    #以下语句插入文档数据时将会抛出异常,因为日期内容不符合映射格式
  57.  
    PUT myindex-2_05/_doc/3
  58.  
    {
  59.  
    "date":1420070400001
  60.  
    }
  61.  
    #以下语句文档数据正常插入,因为日期内容符合字段指定的日期格式
  62.  
    PUT myindex-2_05/_doc/4
  63.  
    {
  64.  
    "date":"2015-01-01 12:02:56"
  65.  
    }

elasticsearch 中的地理(geo_point、geo_shape)类型 的详解

  • 地理位置(geo)是用于存储经纬度的字段类型。

  • 用例场景如下

    • 在边界框内、中心点的特定距离内或多边形内查找地理点

    • 按地理位置或距中心点的距离聚合文档

    • 将距离整合到文档的相关性得分中

    • 按距离对文档排序

  • 在生活中,我们可能会遇到根据当前所在的位置找到离自己最近的符合条件的一些商店、酒店之类的情况。在elasticsearch中也支持这种业务的查询,它主要支持两种类型的地理查询:一种是地理点(geo_point)查询,即经纬度查询;另一种是地理形状(geo_shape)查询,支持点、线、圈、多边形查询等。

geo_shape

  • geo_shape(空间位置)类型支持地理形状的搜索,即点、线、圈、多边形搜索等。比如我们想要找到最接近给定位置的路线,就可以使用此类型。语法如下

  1.  
    PUT /索引库名称
  2.  
    {
  3.  
    "mappings": {
  4.  
    "properties": {
  5.  
    "location": {
  6.  
    "type": "geo_shape"
  7.  
    }
  8.  
    }
  9.  
    }
  10.  
    }

geo_point

  • 在移动互联网时代,用户借助移动设备产生的消费越来越多。例如:用户要根据某个地理位置来搜索酒店,此时可以把酒店的经纬度数据设置为地理数据类型。该类型的定义需要在mapping中指定目标字段的数据类型为geo_point类型

  • elasticseach也提供了地理点查询的类型,即geo_point类型。使用语法如下

  1.  
    PUT 索引库名称
  2.  
    {
  3.  
    "mappings": {
  4.  
    "properties": {
  5.  
    "location": {
  6.  
    "type": "geo_point"
  7.  
    }
  8.  
    }
  9.  
    }
  10.  
    }

elasticsearch 中的地理(geo_point)类型 范例

  1.  
    #一个酒店搜索项目,酒店的索引除了包含酒店名称、城市、价格、星级、评论数、是否满房、日期之外,还需要定义位
  2.  
    置等。
  3.  
    PUT /hotel/_mapping
  4.  
    {
  5.  
    "properties": {
  6.  
    "location": {
  7.  
    "type": "geo_point"
  8.  
    }
  9.  
    }
  10.  
    }
  11.  
     
  12.  
    #插入数据
  13.  
    POST /hotel/_doc/001
  14.  
    {
  15.  
    "title":"文雅酒店",
  16.  
    "city":"上海",
  17.  
    "price":270,
  18.  
    "start":10,
  19.  
    "comment_count":2,
  20.  
    "full_room":true,
  21.  
    "create_time":"2021-01-15 01:23:30",
  22.  
    "location":{
  23.  
    "lat":40.012134,
  24.  
    "lon":116.497553
  25.  
    }
  26.  
    }
  27.  
     
  28.  
    #搜索指定的两个地理位置形成的矩形范围中包含的酒店信息
  29.  
    GET hotel/_search
  30.  
    {
  31.  
    "query": {
  32.  
    "bool": {
  33.  
    "must": [
  34.  
    {
  35.  
    "match_all": {}
  36.  
    }
  37.  
    ],
  38.  
    "filter": {
  39.  
    "geo_bounding_box": {
  40.  
    "location": {
  41.  
    "top_left": {
  42.  
    "lat": 40.73,
  43.  
    "lon": -74.1
  44.  
    },
  45.  
    "bottom_right": {
  46.  
    "lat": 40.01,
  47.  
    "lon": -71.12
  48.  
    }
  49.  
    }
  50.  
    }
  51.  
    }
  52.  
    }
  53.  
    }
  54.  
    }
  55.  
     
  56.  
    #搜索指定的多个地理位置形成的多边形范围中包含酒店信息
  57.  
    GET /hotel/_search
  58.  
    {
  59.  
    "query": {
  60.  
    "bool": {
  61.  
    "must": [
  62.  
    {
  63.  
    "match_all": {}
  64.  
    }
  65.  
    ],
  66.  
    "filter": {
  67.  
    "geo_polygon": {
  68.  
    "location": {
  69.  
    "points": [
  70.  
    {
  71.  
    "lat": 40.73,
  72.  
    "lon": -74.1
  73.  
    },
  74.  
    {
  75.  
    "lat": 40.83,
  76.  
    "lon": -75.1
  77.  
    },
  78.  
    {
  79.  
    "lat": 40.93,
  80.  
    "lon": -76.1
  81.  
    }
  82.  
    ]
  83.  
    }
  84.  
    }
  85.  
    }
  86.  
    }
  87.  
    }
  88.  
    }
  89.  
     
  90.  
    #搜索指定位置1000km范围内的酒店数据
  91.  
    GET /hotel/_search
  92.  
    {
  93.  
    "query": {
  94.  
    "bool": {
  95.  
    "must": [
  96.  
    {
  97.  
    "match_all": {}
  98.  
    }
  99.  
    ],
  100.  
    "filter":{
  101.  
    "geo_distance": {
  102.  
    "distance": "1000km",
  103.  
    "location": {
  104.  
    "lat": 40.73,
  105.  
    "lon": -74.1
  106.  
    }
  107.  
    }
  108.  
    }
  109.  
    }
  110.  
    }
  111.  
    }
  112.  
     
  113.  
    #搜索距离指定位置一定范围内有多少个酒店
  114.  
    GET /hotel/_search
  115.  
    {
  116.  
    "size": 0,
  117.  
    "aggs": {
  118.  
    "count_by_distinct": {
  119.  
    "geo_distance": {
  120.  
    "field": "location",
  121.  
    "origin": {
  122.  
    "lat": 52.376,
  123.  
    "lon": 4.894
  124.  
    },
  125.  
    "ranges": [
  126.  
    {
  127.  
    "from": 100,
  128.  
    "to": 300
  129.  
    }
  130.  
    ],
  131.  
    "unit": "mi",
  132.  
    "distance_type": "arc"
  133.  
    }
  134.  
    }
  135.  
    }
  136.  
    }

elasticsearch 中的地理(geo_shape)类型 范例

1.创建索引映射并指定location字段的字段类型为geo_shape类型

  1.  
    #创建索引映射并指定location字段的字段类型为geo_shape类型
  2.  
    PUT myindex-geo_shape
  3.  
    {
  4.  
    "mappings": {
  5.  
    "properties": {
  6.  
    "location": {
  7.  
    "type": "geo_shape"
  8.  
    }
  9.  
    }
  10.  
    }
  11.  
    }
  12.  
     
  13.  
    #插入地点相关信息
  14.  
    POST /myindex-geo_shape/_doc?refresh
  15.  
    {
  16.  
    "name": "Wind & Wetter,Berlin,Germany",
  17.  
    "location": {
  18.  
    "type": "point",
  19.  
    "coordinates": [
  20.  
    13.400544,
  21.  
    52.530286
  22.  
    ]
  23.  
    }
  24.  
    }

2.搜索指定的两个位置范围内的地点

  1.  
    #搜索指定的两个位置范围内的地点
  2.  
    GET /myindex-geo_shape/_search
  3.  
    {
  4.  
    "query": {
  5.  
    "bool": {
  6.  
    "must": [
  7.  
    {
  8.  
    "match_all": {}
  9.  
    }
  10.  
    ],
  11.  
    "filter": {
  12.  
    "geo_shape": {
  13.  
    "location": {
  14.  
    "shape": {
  15.  
    "type": "envelope",
  16.  
    "coordinates": [
  17.  
    [
  18.  
    13,
  19.  
    53
  20.  
    ],
  21.  
    [
  22.  
    14,
  23.  
    52
  24.  
    ]
  25.  
    ]
  26.  
    },
  27.  
    "relation": "within"
  28.  
    }
  29.  
    }
  30.  
    }
  31.  
    }
  32.  
    }
  33.  
    }
  34.  
     
  35.  
    #返回的结果
  36.  
    {
  37.  
    "took" : 3,
  38.  
    "timed_out" : false,
  39.  
    "_shards" : {
  40.  
    "total" : 1,
  41.  
    "successful" : 1,
  42.  
    "skipped" : 0,
  43.  
    "failed" : 0
  44.  
    },
  45.  
    "hits" : {
  46.  
    "total" : {
  47.  
    "value" : 1,
  48.  
    "relation" : "eq"
  49.  
    },
  50.  
    "max_score" : 1.0,
  51.  
    "hits" : [
  52.  
    {
  53.  
    "_index" : "myindex-geo_shape",
  54.  
    "_type" : "_doc",
  55.  
    "_id" : "oKSWWYcB7w4YX5_iGU4l",
  56.  
    "_score" : 1.0,
  57.  
    "_source" : {
  58.  
    "name" : "Wind & Wetter,Berlin,Germany",
  59.  
    "location" : {
  60.  
    "type" : "point",
  61.  
    "coordinates" : [
  62.  
    13.400544,
  63.  
    52.530286
  64.  
    ]
  65.  
    }
  66.  
    }
  67.  
    }
  68.  
    ]
  69.  
    }
  70.  
    }
  • 由以上语句可知,根据需求返回了两个地理位置范围内符合条件的地点。

elasticsearch 中的对象类型 的详解

  • elasticsearch中的object类型实际就是JSON数据格式

  • 在实际业务中,一个文档需要包含其他内部对象。例如,在酒店搜索需求中,用户希望酒店信息中包含评论数据。评论数据分为好评数量和差评数量。为了支持这种业务,在ES中可以使用对象类型。

  • 对象类型和数组类型一样,对象类型也不用事先定义,在写入文档的时候ES会自动识别并转换为对象类型。

elasticsearch 中的对象类型 的范例(一)

  1.  
    #向hotel中添加一条数据
  2.  
    PUT /hotel/_doc/002
  3.  
    {
  4.  
    "title": "好再来酒店",
  5.  
    "city": "青岛",
  6.  
    "price": 578.23,
  7.  
    "comment_info": {
  8.  
    "properties": {
  9.  
    "favourable_comment": 199,
  10.  
    "negative_comment": 68
  11.  
    }
  12.  
    }
  13.  
    }
  14.  
    #执行以上DSL后,索引hotel增加了一个字段comment_info,它有两个属性,分别是favourable_comment
  15.  
    和negative_comment,二者的类型都是long。
  16.  
     
  17.  
    #查看hotel的mapping
  18.  
    GET /hotel/_mapping
  19.  
    #hotel 的mapping 响应结果为:
  20.  
    {
  21.  
    "hotel" : {
  22.  
    "mappings" : {
  23.  
    "properties" : {
  24.  
    "city" : {
  25.  
    "type" : "keyword"
  26.  
    },
  27.  
    "comment_count" : {
  28.  
    "type" : "integer"
  29.  
    },
  30.  
    "comment_info" : {
  31.  
    "properties" : {
  32.  
    "properties" : {
  33.  
    "properties" : {
  34.  
    "favourable_comment" : {
  35.  
    "type" : "long"
  36.  
    },
  37.  
    "negative_comment" : {
  38.  
    "type" : "long"
  39.  
    }
  40.  
    }
  41.  
    }
  42.  
    }
  43.  
    },
  44.  
    "create_time" : {
  45.  
    "type" : "date",
  46.  
    "format" : "yyyy-MM-dd HH:mm:ss"
  47.  
    },
  48.  
    "full_room" : {
  49.  
    "type" : "boolean"
  50.  
    },
  51.  
    "location" : {
  52.  
    "type" : "geo_point"
  53.  
    },
  54.  
    "price" : {
  55.  
    "type" : "double"
  56.  
    },
  57.  
    "start" : {
  58.  
    "type" : "byte"
  59.  
    },
  60.  
    "title" : {
  61.  
    "type" : "text"
  62.  
    }
  63.  
    }
  64.  
    }
  65.  
    }
  66.  
    }
  67.  
    #根据对象类型的属性进行搜索,可以直接用"."操作符进行指向。
  68.  
    #例如搜索hotel索引中好评数大于200的文档
  69.  
    GET /hotel/_search
  70.  
    {
  71.  
    "query": {
  72.  
    "range": {
  73.  
    "comment_info.properties.favourable_comment": {
  74.  
    "gte": 36
  75.  
    }
  76.  
    }
  77.  
    }
  78.  
    }
  79.  
    #当然,对象内部还可以包含对象
  80.  
    #评论信息字段comment_info可以增加前3条好评数据
  81.  
    POST /hotel/_doc/002
  82.  
    {
  83.  
    "title": "好再来酒店",
  84.  
    "city": "青岛",
  85.  
    "price": 578.23,
  86.  
    "comment_info": {
  87.  
    "properties": {
  88.  
    "favourable_comment": 199,
  89.  
    "negative_comment": 68,
  90.  
    "top3_favourable_comment": {
  91.  
    "top1": {
  92.  
    "content": "干净整洁的一家酒店",
  93.  
    "score":87
  94.  
    },
  95.  
    "top2": {
  96.  
    "content": "服务周到,停车方便",
  97.  
    "score":89
  98.  
    },
  99.  
    "top3": {
  100.  
    "content": "闹中取静,环境优美",
  101.  
    "score":90
  102.  
    }
  103.  
    }
  104.  
    }
  105.  
    }
  106.  
    }

elasticsearch 中的对象类型 的范例(二)

  1.  
    #创建索引映射
  2.  
    PUT myindex-object
  3.  
    {
  4.  
    "mappings": {
  5.  
    "properties": {
  6.  
    "region": {
  7.  
    "type": "keyword"
  8.  
    },
  9.  
    "manager": {
  10.  
    "properties": {
  11.  
    "age": {
  12.  
    "type": "integer"
  13.  
    },
  14.  
    "name": {
  15.  
    "properties": {
  16.  
    "first": {
  17.  
    "type": "text"
  18.  
    },
  19.  
    "last": {
  20.  
    "type": "text"
  21.  
    }
  22.  
    }
  23.  
    }
  24.  
    }
  25.  
    }
  26.  
    }
  27.  
    }
  28.  
    }
  29.  
     
  30.  
    #对于_id等于1的文档而言,manager是一个对象,该对象中又包含一个name对象,而name对象中有两个键值对。如果需
  31.  
    要向这个索引映射中插入文档,可以使用下面任何一种方式写入
  32.  
     
  33.  
    #范例一:使用嵌套的JSON数据格式进行写入
  34.  
    #使用嵌套的JSON数据格式进行写入
  35.  
    PUT myindex-object/_doc/1
  36.  
    {
  37.  
    "region":"China",
  38.  
    "manager":{
  39.  
    "age":30,
  40.  
    "name":{
  41.  
    "first":"clay",
  42.  
    "last":"zhagng"
  43.  
    }
  44.  
    }
  45.  
    }
  46.  
     
  47.  
    #范例二:使用简单的JSON数据格式进行写入
  48.  
    #使用简单的JSON数据格式进行写入
  49.  
    PUT myindex-object/_doc/2
  50.  
    {
  51.  
    "region": "US",
  52.  
    "manager.age": 30,
  53.  
    "manager.name.first": "John",
  54.  
    "manager.name.last": "Smith"
  55.  
    }
  56.  
     
  57.  
    #使用以上两种方式写入数据不会影响数据的存储,但是会影响查询返回的结果。
  58.  
    #查询数据
  59.  
    GET myindex-object/_doc/_search
  60.  
     
  61.  
    #返回的结果为
  62.  
    {
  63.  
    "took" : 1,
  64.  
    "timed_out" : false,
  65.  
    "_shards" : {
  66.  
    "total" : 1,
  67.  
    "successful" : 1,
  68.  
    "skipped" : 0,
  69.  
    "failed" : 0
  70.  
    },
  71.  
    "hits" : {
  72.  
    "total" : {
  73.  
    "value" : 2,
  74.  
    "relation" : "eq"
  75.  
    },
  76.  
    "max_score" : 1.0,
  77.  
    "hits" : [
  78.  
    {
  79.  
    "_index" : "myindex-object",
  80.  
    "_type" : "_doc",
  81.  
    "_id" : "1",
  82.  
    "_score" : 1.0,
  83.  
    "_source" : {
  84.  
    "region" : "China",
  85.  
    "manager" : {
  86.  
    "age" : 30,
  87.  
    "name" : {
  88.  
    "first" : "clay",
  89.  
    "last" : "zhagng"
  90.  
    }
  91.  
    }
  92.  
    }
  93.  
    },
  94.  
    {
  95.  
    "_index" : "myindex-object",
  96.  
    "_type" : "_doc",
  97.  
    "_id" : "2",
  98.  
    "_score" : 1.0,
  99.  
    "_source" : {
  100.  
    "region" : "US",
  101.  
    "manager.age" : 30,
  102.  
    "manager.name.first" : "John",
  103.  
    "manager.name.last" : "Smith"
  104.  
    }
  105.  
    }
  106.  
    ]
  107.  
    }
  108.  
    }
  109.  
    #以上返回信息的写入格式与查询返回的格式一致。

elasticsearch 中的数组类型 的详解

  • ES数组没有定义方式,其使用方式是开箱即用的,即无须事先声明,在写入时把数据用中括号[]括起来,由ES对该字段完成定义。当然,如果事先已经定义了字段类型,在写数据时以数组形式写入,ES也会将该类型转为数组。

elasticsearch 中的数组类型 的范例

  1.  
    #为hotel索引增加一个标签字段,名称为tag
  2.  
    PUT /hotel/_mapping
  3.  
    {
  4.  
    "properties": {
  5.  
    "tag": {
  6.  
    "type": "keyword"
  7.  
    }
  8.  
    }
  9.  
    }
  10.  
    #查看一下索引hotel的mapping
  11.  
    GET /hotel/_mapping
  12.  
    #返回的结果如下
  13.  
    {
  14.  
    "hotel" : {
  15.  
    "mappings" : {
  16.  
    "properties" : {
  17.  
    "city" : {
  18.  
    "type" : "keyword"
  19.  
    },
  20.  
    "comment_count" : {
  21.  
    "type" : "integer"
  22.  
    },
  23.  
    "comment_info" : {
  24.  
    "properties" : {
  25.  
    "properties" : {
  26.  
    "properties" : {
  27.  
    "favourable_comment" : {
  28.  
    "type" : "long"
  29.  
    },
  30.  
    "negative_comment" : {
  31.  
    "type" : "long"
  32.  
    },
  33.  
    "top3_favourable_comment" : {
  34.  
    "properties" : {
  35.  
    "top1" : {
  36.  
    "properties" : {
  37.  
    "content" : {
  38.  
    "type" : "text",
  39.  
    "fields" : {
  40.  
    "keyword" : {
  41.  
    "type" : "keyword",
  42.  
    "ignore_above" : 256
  43.  
    }
  44.  
    }
  45.  
    },
  46.  
    "score" : {
  47.  
    "type" : "long"
  48.  
    }
  49.  
    }
  50.  
    },
  51.  
    "top2" : {
  52.  
    "properties" : {
  53.  
    "content" : {
  54.  
    "type" : "text",
  55.  
    "fields" : {
  56.  
    "keyword" : {
  57.  
    "type" : "keyword",
  58.  
    "ignore_above" : 256
  59.  
    }
  60.  
    }
  61.  
    },
  62.  
    "score" : {
  63.  
    "type" : "long"
  64.  
    }
  65.  
    }
  66.  
    },
  67.  
    "top3" : {
  68.  
    "properties" : {
  69.  
    "content" : {
  70.  
    "type" : "text",
  71.  
    "fields" : {
  72.  
    "keyword" : {
  73.  
    "type" : "keyword",
  74.  
    "ignore_above" : 256
  75.  
    }
  76.  
    }
  77.  
    },
  78.  
    "score" : {
  79.  
    "type" : "long"
  80.  
    }
  81.  
    }
  82.  
    }
  83.  
    }
  84.  
    }
  85.  
    }
  86.  
    }
  87.  
    }
  88.  
    },
  89.  
    "create_time" : {
  90.  
    "type" : "date",
  91.  
    "format" : "yyyy-MM-dd HH:mm:ss"
  92.  
    },
  93.  
    "full_room" : {
  94.  
    "type" : "boolean"
  95.  
    },
  96.  
    "location" : {
  97.  
    "type" : "geo_point"
  98.  
    },
  99.  
    "price" : {
  100.  
    "type" : "double"
  101.  
    },
  102.  
    "start" : {
  103.  
    "type" : "byte"
  104.  
    },
  105.  
    "tag" : {
  106.  
    "type" : "keyword"
  107.  
    },
  108.  
    "title" : {
  109.  
    "type" : "text"
  110.  
    }
  111.  
    }
  112.  
    }
  113.  
    }
  114.  
    }
  115.  
    #通过返回的mapping信息来看,新增的tag字段与普通的keyword类型字段没什么区别,现在写入一条数据
  116.  
    PUT /hotel/_doc/004
  117.  
    {
  118.  
    "title":"好再来酒店",
  119.  
    "city":"青岛",
  120.  
    "price":"578.23",
  121.  
    "tags":["有车位","免费WIFI"]
  122.  
    }
  123.  
     
  124.  
    #查询一下写入的数据
  125.  
    GET /hotel/_search
  126.  
     
  127.  
    #返回的数据为
  128.  
    {
  129.  
    "took" : 0,
  130.  
    "timed_out" : false,
  131.  
    "_shards" : {
  132.  
    "total" : 1,
  133.  
    "successful" : 1,
  134.  
    "skipped" : 0,
  135.  
    "failed" : 0
  136.  
    },
  137.  
    "hits" : {
  138.  
    "total" : {
  139.  
    "value" : 3,
  140.  
    "relation" : "eq"
  141.  
    },
  142.  
    "max_score" : 1.0,
  143.  
    "hits" : [
  144.  
    {
  145.  
    "_index" : "hotel",
  146.  
    "_type" : "_doc",
  147.  
    "_id" : "001",
  148.  
    "_score" : 1.0,
  149.  
    "_source" : {
  150.  
    "title" : "文雅酒店",
  151.  
    "city" : "上海",
  152.  
    "price" : 270,
  153.  
    "start" : 10,
  154.  
    "comment_count" : 2,
  155.  
    "full_room" : true,
  156.  
    "create_time" : "2021-01-15 01:23:30",
  157.  
    "location" : {
  158.  
    "lat" : 40.012134,
  159.  
    "lon" : 116.497553
  160.  
    }
  161.  
    }
  162.  
    },
  163.  
    {
  164.  
    "_index" : "hotel",
  165.  
    "_type" : "_doc",
  166.  
    "_id" : "002",
  167.  
    "_score" : 1.0,
  168.  
    "_source" : {
  169.  
    "title" : "好再来酒店",
  170.  
    "city" : "青岛",
  171.  
    "price" : 578.23,
  172.  
    "comment_info" : {
  173.  
    "properties" : {
  174.  
    "favourable_comment" : 199,
  175.  
    "negative_comment" : 68,
  176.  
    "top3_favourable_comment" : {
  177.  
    "top1" : {
  178.  
    "content" : "干净整洁的一家酒店",
  179.  
    "score" : 87
  180.  
    },
  181.  
    "top2" : {
  182.  
    "content" : "服务周到,停车方便",
  183.  
    "score" : 89
  184.  
    },
  185.  
    "top3" : {
  186.  
    "content" : "闹中取静,环境优美",
  187.  
    "score" : 90
  188.  
    }
  189.  
    }
  190.  
    }
  191.  
    }
  192.  
    }
  193.  
    },
  194.  
    {
  195.  
    "_index" : "hotel",
  196.  
    "_type" : "_doc",
  197.  
    "_id" : "004",
  198.  
    "_score" : 1.0,
  199.  
    "_source" : {
  200.  
    "title" : "好再来酒店",
  201.  
    "city" : "青岛",
  202.  
    "price" : "578.23",
  203.  
    "tags" : [
  204.  
    "有车位",
  205.  
    "免费WIFI"
  206.  
    ]
  207.  
    }
  208.  
    }
  209.  
    ]
  210.  
    }
  211.  
    }
  212.  
    #通过以上信息可以看到,写入的数据的tag字段已经是数组类型了。那么,数组类型的数据如何搜索呢?
  213.  
    #数组类型的字段适用于元素类型的搜索方式,也就是说,数组元素适用于什么搜索,数组字段就适用于什么搜索。
  214.  
    #在上面的示例中,数组元素类型是keyword,该类型可以适用于term搜索,则tag字段也可以适用于term搜索
  215.  
    GET /hotel/_search
  216.  
    {
  217.  
    "query": {
  218.  
    "term": {
  219.  
    "tags": {
  220.  
    "value": "有车位"
  221.  
    }
  222.  
    }
  223.  
    }
  224.  
    }
  225.  
     
  226.  
    #ES中的空数组可以作为missing field,即没有值的字段,下面的DSL将插入一条tag为空的数组
  227.  
    POST /hotel/_doc/003
  228.  
    {
  229.  
    "title":"环球酒店",
  230.  
    "city":"青岛",
  231.  
    "price":"530.00",
  232.  
    "tags":[]
  233.  
    }

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhfjegkg
系列文章
更多 icon
同类精品
更多 icon
继续加载