木子李的个人博客

Good Luck To You!

Elasticsearch中的查询(查询二)分词器

一个简单的检索语句通过上述几种方式即可实现了,但是实际工作中,肯定还是用到分词器的,下面就以常用的IK分词器进行说明

3.1、查询时指定分词器

首先是第一种查询时指定分词器的形式进行检索

GET /zfc-doc-000001/_search
{
  "query": {
    "match": {
      "content": {
        "query": "es",
        "analyzer": "ik_max_word"
      }
    }
  }
}

通过上面指定分词器的形式,我们在查询时就可以使用ik_max_word来作为检索分词来处理了,我们也可以在创建索引时增加字段分词的配置,方便检索时默认使用指定分词器检索,下面就是创建索引指定分词器的步骤

3.2、初始化默认分词器

1、删除刚才创建的索引

DELETE zfc-doc-000001

2、修改content字段,加入分词器ik_max_word,修改部分如下

"content":{
        "type":"text",
        "analyzer": "ik_max_word"
      }

完整的语句如下所示

PUT /zfc-doc-000001
{
  "settings": {
    "index":{
      "number_of_shards":3,
      "number_of_replicas":2
    }
  },
  "mappings": {
    "properties": {
      "title":{
        "type":"keyword"
      },
      "content":{
        "type":"text",
        "analyzer": "ik_max_word"
      },
      "createTime":{
        "type": "date",
        "format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis" 
      },
      "agreeNum":{
        "type": "integer"
      },
      "comment":{
        "type": "nested",
        "properties": {
          "content":{
            "type":"text"
          },
          "name":{
            "type":"keyword"
          },
          "time":{
            "type":"date",
            "format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"
          }
        }
      }
    }
  }
}

通过上面语句创建完索引之后,content字段就是使用ik_max_word分词的了,这样使用普通的match查询即可,这里也就不再演示了

GET zfc-doc-000001/_search
{
  "query": {
    "match":{
      "content":"es"
    }
  }
}


发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

«    2026年3月    »
1
2345678
9101112131415
16171819202122
23242526272829
3031
控制面板
您好,欢迎到访网站!
  查看权限
网站分类
搜索
最新留言
    文章归档
    网站收藏
    友情链接

    Powered By Z-BlogPHP 1.7.3