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);
   
  }
 
 }

}
相关推荐
xufwind43 分钟前
spark standlone 集群离线安装
大数据·分布式·spark
AI数据皮皮侠2 小时前
中国区域10m空间分辨率楼高数据集(全国/分省/分市/免费数据)
大数据·人工智能·机器学习·分类·业界资讯
DeepSeek大模型官方教程3 小时前
NLP之文本纠错开源大模型:兼看语音大模型总结
大数据·人工智能·ai·自然语言处理·大模型·产品经理·大模型学习
feilieren3 小时前
Docker 安装 Elasticsearch 9
运维·elasticsearch·docker·es
大数据CLUB4 小时前
基于spark的奥运会奖牌变化数据分析
大数据·hadoop·数据分析·spark
Edingbrugh.南空4 小时前
Hadoop高可用集群搭建
大数据·hadoop·分布式
智慧化智能化数字化方案4 小时前
69页全面预算管理体系的框架与落地【附全文阅读】
大数据·人工智能·全面预算管理·智慧财务·智慧预算
武子康5 小时前
大数据-33 HBase 整体架构 HMaster HRegion
大数据·后端·hbase
Java烘焙师7 小时前
架构师必备:业务扩展模式选型
mysql·elasticsearch·架构·hbase·多维度查询
诗旸的技术记录与分享18 小时前
Flink-1.19.0源码详解-番外补充3-StreamGraph图
大数据·flink