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

}
相关推荐
江瀚视野5 小时前
多家银行向甲骨文断贷,巨头甲骨文这是怎么了?
大数据·人工智能
asheuojj5 小时前
2026年GEO优化获客效果评估指南:如何精准衡量TOP5关
大数据·人工智能·python
callJJ5 小时前
Spring AI ImageModel 完全指南:用 OpenAI DALL-E 生成图像
大数据·人工智能·spring·openai·springai·图像模型
SAP工博科技5 小时前
SAP 公有云 ERP 多工厂多生产线数据统一管理技术实现解析
大数据·运维·人工智能
星辰_mya5 小时前
Elasticsearch线上问题之OOM
大数据·elasticsearch·搜索引擎
忆~遂愿5 小时前
Runtime 上下文管理:计算实例的生命周期、延迟最小化与上下文切换优化
java·大数据·开发语言·人工智能·docker
Elastic 中国社区官方博客5 小时前
使用 Groq 与 Elasticsearch 进行智能查询
大数据·数据库·人工智能·elasticsearch·搜索引擎·ai·全文检索
BJ_Bonree5 小时前
4月17日,博睿数据受邀出席GOPS全球运维大会2026 · 深圳站!
大数据·运维·人工智能
张彦峰ZYF5 小时前
一套「策略化 Elasticsearch 召回平台」架构设计思路
大数据·elasticsearch·搜索引擎
Giggle12185 小时前
外卖 O2O 系统怎么选?从架构到部署方式的完整拆解
大数据·架构