【Elasticsearch教程9】Mapping keyword的ignore_above

首先随意往ES插一条数据:

put my_index/_doc/1
{
  "name": "李星云"
}

查看ES自动生成的mapping,name是text类型,其下还有keyword子类型,且"ignore_above" : 256

GET /my_index/_mapping

name定义如下:
"properties" : {
  "name" : {
    "type" : "text",
    "fields" : {
      "keyword" : {
        "type" : "keyword",
        "ignore_above" : 256
      }
    }
  }
}

对于keyword类型, 可设置ignore_above限定字符长度。超过 ignore_above 的字符会被存储,但不会被倒排索引。比如ignore_above=4,”abc“,”abcd“,”abcde“都能存进ES,但是不能根据”abcde“检索到数据。

【1】创建一个keyword类型的字段,ignore_above=4

PUT test_index
{
  "mappings": {
    "_doc": {
      "properties": {
        "message": {
          "type": "keyword",
          "ignore_above": 4
        }
      }
    }
  }
}

【2】向索引插入3条数据:

PUT /test_index/_doc/1
{
  "message": "abc"
}

PUT /test_index/_doc/2
{
  "message": "abcd"
}

PUT /test_index/_doc/3
{
  "message": "abcde"
}

此时ES倒排索引是:

词项文档ID
abc1
abcd2

【3】根据message进行terms聚合

GET /test_index/_search
{
  "size": 0, 
  "aggs": {
    "term_message": {
      "terms": {
        "field": "message",
        "size": 10
      }
    }
  }
}

返回结果:

{
  "took" : 2,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : 3,
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "test_index",
        "_type" : "_doc",
        "_id" : "2",
        "_score" : 1.0,
        "_source" : {
          "message" : "abcd"
        }
      },
      {
        "_index" : "test_index",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 1.0,
        "_source" : {
          "message" : "abc"
        }
      },
      {
        "_index" : "test_index",
        "_type" : "_doc",
        "_id" : "3",
        "_score" : 1.0,
        "_source" : {
          "message" : "abcde"
        }
      }
    ]
  },
  "aggregations" : {
    "term_message" : {
      "doc_count_error_upper_bound" : 0,
      "sum_other_doc_count" : 0,
      "buckets" : [#注意这分组里没有”abcde“
        {
          "key" : "abc",
          "doc_count" : 1
        },
        {
          "key" : "abcd",
          "doc_count" : 1
        }
      ]
    }
  }
}

【4】根据”abcde“进行term精确查询,结果为空

GET /test_index/_search
{
  "query": {
    "term": {
      "message": "abcde"
    }
  }
}

然后结果:
  "hits" : {
    "total" : 0,
    "max_score" : null,
    "hits" : [ ]
  }

通过上面结果能知道”abcde“已经存入ES,也可以搜索出来,但是不存在词项”abcde“,不能根据”abcde“作为词项进行检索。
对于已存在的keyword字段,其ignore_above子属性可以修改,但只对新数据有效。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

瑟 王

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值