elasticsearch拼音分词器

复制代码
PUT /yx 
{
  "settings": {
    "analysis": {
      "analyzer": {
        "my_analyzer":{
          "tokenizer":"ik_max_word",
          "filter":"py"
        },
        "completion_analyzer":{
          "tokenizer":"keyword",
          "filter":"py"
        }
      },
      "filter": {
        "py": {
           "type":"pinyin",
           "keep_full_pinyin":false,
           "keep_joined_full_pinyin":true,
           "keep_original":true,
           "limit_first_letter_length":16,
           "remove_duplicated_term":true,
           "none_chinese_pinyin_tokenize":false
           
          }
      }
    }
  }, 
	"mappings": {
	  "messaged":{
		"properties": {
			"uuid": {
				"type": "keyword"
				 
			},
			"username": {
	  		"type": "completion",
				"analyzer": "completion_analyzer"
			},
			"messaged": {
			"type": "completion",
				"analyzer": "completion_analyzer"
				 
			},
			"tabled": {
				"type": "completion",
				"analyzer": "completion_analyzer"
			 
			},
			"datetime": {
				"type": "date"
				 
			},
			"state": {
				"type": "text",
				"analyzer": "my_analyzer",
				"search_analyzer": "ik_smart" 
			}
		}
	  }
	}
}

然后加入一条数据后,就可以开始查询了

java 复制代码
package com;
import com.example.demo.pojo.Messaged;
import com.google.gson.Gson;
import org.apache.http.HttpHost;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.aggregations.Aggregations;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightField;
import org.elasticsearch.search.suggest.Suggest;
import org.elasticsearch.search.suggest.SuggestBuilder;
import org.elasticsearch.search.suggest.SuggestBuilders;
import org.elasticsearch.search.suggest.completion.CompletionSuggestion;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.io.IOException;
import java.util.Map;
import java.util.TreeSet;

@SpringBootTest
public class ElasticsearchTests {

 private RestHighLevelClient restHighLevelClient1;
 // JSON⼯具
 private Gson gson = new Gson();
 /** 初始化客户端 */
 @Before
 public void init() {
 RestClientBuilder restClientBuilder = RestClient.builder(
 new HttpHost("139.196.166.21", 9200, "http")

 );
 restHighLevelClient1 = new RestHighLevelClient(restClientBuilder);
 }
 
 /** 关闭客户端 */
 @After
 public void close() throws IOException {
 // 关闭客户端
 restHighLevelClient1.close();
 }
 /** 删除⽂档 */
 @Test
 public void delete() throws IOException {
  // 1.创建request
  SearchRequest request = new SearchRequest("yx");
  SuggestBuilder suggestBuilder = new SuggestBuilder();
  suggestBuilder.addSuggestion("messaged", SuggestBuilders.completionSuggestion("messaged")
          .prefix("s")
          //跳过
          .skipDuplicates(true)
          .size(10));
  // 2.准备DSL参数
  request.source().suggest(suggestBuilder);
  // 3.发送请求
  SearchResponse response = restHighLevelClient1.search(request, RequestOptions.DEFAULT);
     // 4.解析响应
  Suggest suggest = response.getSuggest();
  TreeSet<String> treeSet = new TreeSet<String>();
  CompletionSuggestion suggestion1 = suggest.getSuggestion("messaged1");
  for (CompletionSuggestion.Entry.Option option : suggestion1.getOptions()) {
   String string = option.getText().toString();
   System.out.println(string);
   SearchHit hit = option.getHit();
   String sourceAsString = hit.getSourceAsString();
   System.out.println(sourceAsString);
   
  }
 
 }

}
相关推荐
玖米GEO运营王总监1 小时前
成都AI数字人视频怎么批量生产?流程与合规标注
大数据·人工智能·音视频
龙腾AI白云3 小时前
数字孪生与计算机视觉:让机器看懂现场
大数据·数据库·机器学习·知识图谱·pygame
starzy19905 小时前
Structured Streaming 案例剖析:一个 8 万 QPS 实时大屏的完整落地与三次线上事故复盘
大数据·spark
大大大大晴天15 小时前
浅聊维度建模中的星型模型与雪花模型
大数据
数字新视界20 小时前
能耗管理系统提升企业能源效率的实践与展望
大数据·人工智能·数据中心·机房管理·模块化机房
大大大大晴天20 小时前
别让金额越算越多:事实表与维度表设计避坑指南
大数据
如夏倾城1 天前
入侵检测系统(IDS)原理、部署模式与 IPS/防火墙对比
大数据·网络·人工智能·网络安全·云原生·架构·kubernetes
wuyk5551 天前
Spider-第20讲-综合实战可上线爬虫
大数据·爬虫·python·elasticsearch
培培说证1 天前
大数据与会计专业性价比高的证书有哪些?
大数据
伞伞悦读1 天前
【第90期】KNN分类回归流水线与K值对照的实验与排查
大数据·人工智能·python·深度学习·机器学习