ES支持中文&&全拼&&拼音首字母搜索

环境

  • Ubuntu18.04
  • ES 6.6.1

搜索模板

新建模板,便于后续创建索引,直接使用模板,省事方便

PUT _template/goods
{
  "index_patterns":"goods*",
  "settings": {
    "index.number_of_replicas": "1",
    "index.number_of_shards": "5",
    "index.translog.flush_threshold_size": "512mb",
    "index.translog.sync_interval": "60s",
    "index.codec": "best_compression",
    "analysis": {
      "filter": {
        "edge_ngram_filter": {
          "type": "edge_ngram",
          "min_gram": 1,
          "max_gram": 50
        },
        "simple_pinyin_filter": {
          "type": "pinyin",
          "keep_first_letter": true,
          "keep_separate_first_letter": false,
          "keep_full_pinyin": false,
          "keep_original": false,
          "limit_first_letter_length": 50,
          "lowercase": true
        },
        "full_pinyin_filter": {
          "type": "pinyin",
          "keep_first_letter": false,
          "keep_separate_first_letter": false,
          "keep_full_pinyin": true,
          "none_chinese_pinyin_tokenize": true,
          "keep_original": false,
          "limit_first_letter_length": 50,
          "lowercase": true
        }
      },
      "char_filter": {
        "charconvert": {
          "type": "mapping",
          "mappings_path": "char_filter_text.txt"
        }
      },
      "tokenizer": {
        "ik_max_word": {
          "type": "ik_max_word",
          "use_smart": true
        }
      },
      "analyzer": {
        "ngramIndexAnalyzer": {
          "type": "custom",
          "tokenizer": "keyword",
          "filter": [
            "edge_ngram_filter",
            "lowercase"
          ],
          "char_filter": [
            "charconvert"
          ]
        },
        "ngramSearchAnalyzer": {
          "type": "custom",
          "tokenizer": "keyword",
          "filter": [
            "lowercase"
          ],
          "char_filter": [
            "charconvert"
          ]
        },
        "ikIndexAnalyzer": {
          "type": "custom",
          "tokenizer": "ik_max_word",
          "char_filter": [
            "charconvert"
          ]
        },
        "ikSearchAnalyzer": {
          "type": "custom",
          "tokenizer": "ik_max_word",
          "char_filter": [
            "charconvert"
          ]
        },
        "simplePinyinIndexAnalyzer": {
          "tokenizer": "keyword",
          "filter": [
            "simple_pinyin_filter",
            "edge_ngram_filter",
            "lowercase"
          ]
        },
        "simplePinyinSearchAnalyzer": {
          "tokenizer": "keyword",
          "filter": [
            "simple_pinyin_filter",
            "lowercase"
          ]
        },
        "fullPinyinIndexAnalyzer": {
          "tokenizer": "keyword",
          "filter": [
            "full_pinyin_filter",
            "edge_ngram_filter",
            "lowercase"
          ]
        },
        "fullPinyinSearchAnalyzer": {
          "tokenizer": "keyword",
          "filter": [
            "full_pinyin_filter",
            "lowercase"
          ]
        }
      }
    }
  }
}

新建 char_filter_text.txt 存于 ES_HOME/config 目录下。可以把相关部分删除。

新建索引

goods_v1 将使用上述模板(goods*),可直接使用模板中定义的 analyzer

DELETE goods_v1

PUT /goods_v1
{
  "mappings": {
    "doc":{
      "properties" : {
        "id":{
          "type":"long"
        },
        "name" : {
          "type": "text", 
          "analyzer": "ikIndexAnalyzer",
          "fields": {
            "ngram": {
              "type": "text", 
              "analyzer": "ngramIndexAnalyzer"
            },
            "SPY": {
              "type": "text", 
              "analyzer": "simplePinyinIndexAnalyzer"
            },
            "FPY": {
              "type": "text", 
              "analyzer": "fullPinyinIndexAnalyzer"
            }
          }
        },
        "update_time" : {
          "type":"date"
        },
        "deleted" : {
          "type":"boolean"
        }
      }
    }
  }
}

PUT /goods_v1/_alias/goods
{ 
  "is_write_index":true
}

filter&&结果id倒序

本搜索只做简单过滤,并且按id倒序返回,不需要计算相似度

GET goods/_search
{
  "query": {
    "constant_score": {
      "filter": {
        "bool": {
          "filter": [
            {
              "term": {
                "deleted": {
                  "value": false,
                  "boost": 1
                }
              }
            },
            {
              "bool": {
                "should": [
                  {
                    "match": {
                      "name.ngram": {
                        "query": "水果",
                        "operator": "OR",
                        "analyzer": "ngramSearc
  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值