(14) Elasticsearch-Pinyin, smart reminder

This chapter is divided into two parts, pinyin word segmentation and smart reminder

1. Pinyin word segmentation

Direct reference: https://github.com/medcl/elasticsearch-analysis-pinyin

The description of the parameters, github has already said very clearly:

2. Smart reminder

Reference: https://blog.csdn.net/baifanwudi/article/details/88662561

First build an index with custom pinyin participles

PUT /steven_piny/
{
  "settings": {
    "index": {
      "analysis": {
        "analyzer": {
          "pinyin_analyzer": {
            "tokenizer": "my_pinyin"
          }
        },
        "tokenizer": {
          "my_pinyin": {
            "type": "pinyin",
            "keep_first_letter":true,
            "keep_separate_first_letter": true,
            "keep_full_pinyin": true,
            "keep_original": true,
            "limit_first_letter_length": 16,
            "lowercase": true
          }
        }
      }
    }
  },
  "mappings": {
      "properties": {
        "station_name": {
          "type": "text",
          "analyzer": "ik_max_word",
          "fields": {
            "s-pinyin": {
              "type": "completion",
              "analyzer": "pinyin_analyzer"
            }
          }
        },
        "station_code": {
          "type": "completion"
        }
      
    }
  }
}

Of course, the above can also be created separately setting and mapping.

Add data:


PUT /steven_piny/_doc/1
{
  "station_code": "VAP",
  "station_name": "北京北"
}

PUT /steven_piny/_doc/2
{
  "station_code": "BOP",
  "station_name": "北京东"
}

PUT /steven_piny/_doc/3
{
  "station_code": "GGQ",
  "station_name": "广州南"
}

PUT /steven_piny/_doc/4
{
  "station_code": "SHH",
  "station_name": "上海"
}

Inquire


POST /steven_piny/_search
{
  "suggest":{
    "text":"bj",
    "code-suggest" : {
      "completion" : {
        "field" : "station_code"
      }
    },
    "pinyin-suggest" : {
      "completion" : {
        "field" : "station_name.s-pinyin"
      }
    }
  }
}

--------------------------------------------------

POST /steven_piny/_search
{
  "suggest":{
    "text":"广",
    "code-suggest" : {
      "completion" : {
        "field" : "station_code"
      }
    },
    "pinyin-suggest" : {
      "completion" : {
        "field" : "station_name.s-pinyin"
      }
    }
  }
}

 

Guess you like

Origin blog.csdn.net/allensandy/article/details/109369805