elasticsearch同时支持精确和模糊搜索

我们进行检索时,通常需要既支持模糊搜索也支持精确搜索,下面是elasticsearch的实现方式

情况1: 空索引

这种情况,非常的简单,只要给予一个字段两种解析方式即可

“properties”: {

    “name”: {

        “analyzer”:”ik_smart”,

        “type”:”text”,

        “fields”: {

            “keyword”: {

                “type”:”keyword”,

                “ignore_above”:256

            } 

         } 

     }

}


情况2: 非空索引

1. 此时,首先修改mapping的配置

curl -H “Content-Type:application/json” -X PUT http://127.0.0.1:9220/my_index/_doc/_mapping -d ‘{

“properties”:{

    “name”:{

        “analyzer”:”ik_smart”,

        “type”:”text”,

        “fields”:{

            “keyword”:{

                “type”:”keyword”,

                “ignore_above”:256

            } 

         } 

     } 

 }

}’

2. 更新历史数据 因为此时索引中已有数据,修改mapping后仅只对之后的数据生效,对于历史数据是不会生效的,所以需要我们对历史数据进行更新,因为我们并不改变历史数据的source,所以可通过_update_by_query实现

创建任务:

curl -H “Content-Type:application/json” -X POST -d ‘{

  “query”: {

    “bool”: {

      “must_not”: {

        “exists”: {

          “field”: “name.keyword”

        }

      }

    }

  }

} ‘http://127.0.0.1:9220/my_index/_update_by_query?conflicts=proceed&scroll_size=5000&wait_for_completion=false

查看任务完成状态:

curl -X GET http://127.0.0.1:9220/_tasks/6xUTqL-pRqmqo_Qxw5PfEQ:830225002

0 评论
最新
最旧 最多投票
内联反馈
查看所有评论
滚动至顶部