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

}
相关推荐
Natural_yz5 小时前
大数据学习09之Hive基础
大数据·hive·学习
Natural_yz5 小时前
大数据学习10之Hive高级
大数据·hive·学习
AI服务老曹5 小时前
建立更及时、更有效的安全生产优化提升策略的智慧油站开源了
大数据·人工智能·物联网·开源·音视频
Mephisto.java5 小时前
【大数据学习 | HBASE高级】storeFile文件的合并
大数据·sql·oracle·json·hbase·database
这样の我5 小时前
hbase集成phoenix
大数据·数据库·hbase
思通数据6 小时前
AI与OCR:数字档案馆图像扫描与文字识别技术实现与项目案例
大数据·人工智能·目标检测·计算机视觉·自然语言处理·数据挖掘·ocr
CoderJia程序员甲6 小时前
重学SpringBoot3-整合 Elasticsearch 8.x (三)使用Repository
java·大数据·spring boot·elasticsearch
东方巴黎~Sunsiny6 小时前
如何优化Elasticsearch的查询性能?
大数据·elasticsearch·搜索引擎
杭州的物联网专家6 小时前
涉密载体管控系统:DW-S402全方位守护信息安全的利器
大数据
guanpinkeji7 小时前
搭子小程序定制开发:全新找搭子之旅
大数据·小程序·小程序开发·小程序制作·找搭子·搭子小程序