ES搜索下拉框查询,联词提示(Suggestion)

主要是看到太多人写的不全,或者不能满足。
–索引库属性构建

  1. 支持集群多索引库+多条件分组+模糊匹配+下拉联想词 (同时支持中文+拼音分词进行混合查询+识别简体和繁体切换)。 支持 IK +
    pinyin (需要在服务安装插件,安装就不说了,启动必须自己用户,记得关闭集群分片在启动)
#############################
 sms_course/_settings   PUT
{
  "index": {
    "analysis": {
      "analyzer": {
        "ik_pinyin_analyzer": {
          "type": "custom",
          "tokenizer": "ik_smart",
          "filter": [
            "my_pinyin",
			"word_delimiter"
          ]
        }
      },
      "filter": {
        "my_pinyin": {
          "type": "pinyin",
          "keep_first_letter": true,
          "limit_first_letter_length": 16,
          "keep_full_pinyin": true,
          "keep_joined_full_pinyin": true,
          "keep_none_chinese_in_first_letter": true,
          "keep_none_chinese_in_joined_full_pinyin": true,
          "none_chinese_pinyin_tokenize": false,
          "keep_original": true,
          "ignore_pinyin_offset": false
        }
      }
    }
  }
}

#############################

 sms_course/course/_mapping  POST
{
  "course": {
    "properties": {
      "id": {
        "type": "keyword"
      },
      "title": {
        "type": "text",
        "analyzer": "ik_pinyin_analyzer",
        "fields": {
          "pinyin": {
            "type": "text",
            "store": false,
            "term_vector": "with_offsets",
            "analyzer": "ik_pinyin_analyzer",
            "boost": 10,
            "index_options": "docs"
          }
        }
      },
      "filed_suggester": {
        "type": "completion",
        "search_analyzer": "ik_pinyin_analyzer",
        "analyzer": "ik_pinyin_analyzer",
        "max_input_length": 50,
        "preserve_separators": true,
        "preserve_position_increments": true
      },
      "info": {
        "type": "join",
        "relations": {
          "md_product": "sl_customer_character_order_list"
        }
      }
    }
  }
}
  1. 用kibana 也可以,这里用elasticsearch-head插件。 注
    :设置属性,需要把当前的索引库先关闭,设置完成在打开添加数据测试。

属性含义和设置地址:
https://github.com/longshanw/elasticsearch-analysis-pinyin

##########
kibana

DELETE /sms_course/course/_analyze
DELETE /sms_teacher/teacher/l3WtD1vIRiG7t_Epzp7oGw
DELETE /sms_course
DELETE /sms_teacher

#查询所有
GET _search
{
“query”: {
“match_all”: {}
}
}
#定义2个库 测试和添加数据
GET /sms_course/course/_search
{
}
GET /sms_teacher/teacher/_search
{
}
#测试pinyin
GET _analyze
{
“analyzer”: “pinyin”,
“text”: “刘德华”
}
GET /sms_teacher/teacher/_search
{
“query”: {
“match”: {
“title”:“刘德华”
}
}
}
GET /sms_course/course/_search
{
“suggest”: {
“my-suggest”:{
“prefix”:“ni”,
“completion”: {
“field”: “filed_suggester”
}
}
}
}

#添加数据测试

GET /sms_teacher/teacher
{
“id”: “666666”,
“title”: “花似有情非有情,水似无意非无意。”,
“filed_suggester”:“花似有情非有情,水似无意非无意。”
}
GET /sms_course/course
{
“id”: “666666”,
“title”: “我往事悠然壹笑間”,
“filed_suggester”:“我往事悠然壹笑間”
}

#停止集群分片分配
PUT /_cluster/settings
{
“transient” : {
“cluster.routing.allocation.enable” : “none”
}
}

#重启分片分配
PUT /_cluster/settings
{
“transient” : {
“cluster.routing.allocation.enable” : “all”
}
}

ES搜索下拉框查询,联词提示(Suggestion)_第1张图片
ES搜索下拉框查询,联词提示(Suggestion)_第2张图片
主要是对于服务以及代码实现,索引库的完善配置参考。

你可能感兴趣的:(JAVA,elasticsearch)