本文介绍: 然后加入一条数据后,就可以开始查询了。
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" 
			}
		}
	  }
	}
}

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

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

}

 

原文地址:https://blog.csdn.net/weixin_62907807/article/details/135426641

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。

如若转载,请注明出处:http://www.7code.cn/show_54364.html

如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱:suwngjj01@126.com进行投诉反馈,一经查实,立即删除!

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注