调整elasticsearch分词器,达到like搜索效果 -基于elasticsearch 6.8

基于elasticsearch 6.8

如果发现es里索引某字段,搜索时会使用默认的Standard分词器,会拆成单字搜索,此时想达到类似mysql like搜索的效果,需要更换分词器为keyword,更新已有的分词器会比较麻烦,步骤如下:

1.新建个新索引,这里把它叫做new_index
2.copy老索引数据到新索引
3.删除老索引
4.建立个别名,名字与老索引相同,指向新索引
5.测试效果

下面操作的DSL语句
1.操作语句

new_index   PUT
{
  "mappings": {
    "_doc": {
      "properties": {
          "customerName": {
            "type": "text",
            "analyzer": "keyword"
          },
          "name": {
            "type": "text",
            "analyzer": "keyword"
          }
       }
    }
  }
}

注意这里,分词器要设为keyword,这里可达到模糊搜索效果

2.操作语句
复制索引

_reindex   POST
{
  "source": {
    "index": "old_index"
  },
  "dest": {
    "index": "new_index"
  }
}

3.操作语句

old_index DELETE

4.操作语句

_aliases   POST
{
  "actions": [
    {
      "add": {
        "index": "new_index",
        "alias": "old_index"
      }
    }
  ]
}

上面DSL语句在es的谷哥浏览器head插件页面操作,可成功实现模糊搜索

es的学习推荐官方文档,比较详细,可按版本来阅读.

其它DSL语句

索引加字段值

new_index/_doc   POST
{
  "customerName": "这是个测试名称"
}

单查

new_index/_search     GET
{
  "query": {
    "match": {
      "customerName": "这是"
    }
  }
}

测试索引分词器类型

new_index/_analyze  POST

{
"analyzer":"ik_smart",
"text":"测试客户1"
}

查询索引字段

new_index/_mappings GET

删除别名

_aliases   POST

{
  "actions": [
    {
      "remove": {
        "index": "new_index",
        "alias": "single_customer"
      }
    }
  ]
}

设置date类型

{
  "mappings": {
    "_doc": {
      "properties": {
          "customerName": {
            "type": "text",
            "analyzer": "keyword"
          },
		  "lastTime": {
            "type": "date",
		    "format": "yyyy-MM-dd HH:mm:ss"
          },
		  "createTime": {
            "type": "date",
			"format": "yyyy-MM-dd HH:mm:ss"
          },
		  "nextTime": {
            "type": "date",
		    "format": "yyyy-MM-dd HH:mm:ss"
          },
		  "updateTime": {
            "type": "date",
		    "format": "yyyy-MM-dd HH:mm:ss"
          }
       }
    }
  }
}

9.复制索引,并处理空时间串
_reindex POST

{
  "source": {
    "index": "test2"
  },
  "dest": {
    "index": "test3"
  },
  "script": {
    "source": "def dateField = ctx._source.nextTime;  if (dateField == '') { ctx._source.nextTime= '1970-01-01 00:00:00';}",
    "lang": "painless"
  }
}

10.设置反转索引,查询排序时有此字段需要用

wk_customer_new/_mapping/_doc  PUT

wk_single_leads

{
  "properties": {
    "nextTime": {
      "type": "text",
      "fielddata": true
    }
  }
}

异常解决

Fielddata is disabled on text fields by default. Set fielddata=true on [updateTime] in order to load fielddata in memory by uninverting the inverted index. Note that this can however use significant memory. Alternatively use a keyword field instead."}}],"caused_by":{"type":"illegal_argument_exception","reason":"Fielddata is disabled on text fields by default. Set fielddata=true on [updateTime] in order to load fielddata in memory by uninverting the inverted index. Note that this can however use significant memory. Alternatively use a keyword field instead.","caused_by":{"type":"illegal_argument_exception","reason":"Fielddata is disabled on text fields by default. Set fielddata=true on [updateTime] in order to load fielddata in memory by uninverting the inverted index. Note that this can however use significant memory

new_index/_mapping/_doc  PUT

{"properties":{"updateTime":{"type":"text","fielddata":true}}}
posted @ 2023-12-25 14:19  水滴aym  阅读(133)  评论(0编辑  收藏  举报