mapping屬性
mapping是對索引庫中文檔的約束, 常見的mapping屬性包括:
-
type
: 字段數(shù)據(jù)類型,常見的簡單類型有:-
字符串
: text(可分詞的文本), keyword(精確值, 例如: 品牌,國家) -
數(shù)值
: long, integer, short, byte, double, float -
布爾
: boolean -
日期
: date -
對象
: object
-
-
index
: 是否創(chuàng)建索引, 默認(rèn)為true -
analyzer
: 使用哪種分詞器 -
properties
: 該字段的子字段
索引庫操作
創(chuàng)建索引庫
PUT /zyw
{
"mappings": {
"properties": {
"info": {
"type": "text",
"analyzer": "ik_smart"
},
"email": {
"type": "keyword",
"index": false
},
"name": {
"type": "object",
"properties": {
"firstName": {
"type": "keyword"
},
"lastName":{
"type":"keyword"
}
}
}
}
}
}
查看索引庫
GET /zyw
GET /zyw/_mapping
# 查看索引庫列表和健康狀態(tài)
GET /_cat/indices
刪除索引庫
DELETE /zyw
修改索引庫, 添加新字段
索引庫和mapping一旦創(chuàng)建無法修改, 但是可以添加新字段
PUT /zyw/_mapping
{
"properties": {
"age": {
"type": "integer"
}
}
}
文檔操作
新增文檔
POST /zyw/_doc/1
{
"info": "java是最好的語言",
"email": "zy@163.com",
"name": {
"firstName": "云",
"lastName": "趙"
}
}
可以不指定id, es會自動生成id
查詢文檔
GET /zyw/_doc/1
刪除文檔
DELETE /zyw/_doc/1
修改文檔
- 全量修改, 會刪除舊文檔, 添加新文檔
PUT /zyw/_doc/1
{
"info": "java是最好的語言",
"email": "zy@163.com",
"name": {
"firstName": "云",
"lastName": "趙"
}
}
- 局部修改
POST /zyw/_update/1
{
"doc": {
"email": "test@163.com"
}
}
RestClient操作索引庫
- 引入依賴
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>7.12.1</version>
</dependency>
注意:
springboot管理了elasticsearch的部分依賴, 查看springboot的依賴管理
我們需要在pom文件中定義這個版本值,覆蓋springboot的
- HotelDoc.java
@Data
@NoArgsConstructor
public class HotelDoc {
private Long id;
private String name;
private String address;
private Integer price;
private Integer score;
private String brand;
private String city;
private String starName;
private String business;
private String location;
private String pic;
public HotelDoc(Hotel hotel) {
this.id = hotel.getId();
this.name = hotel.getName();
this.address = hotel.getAddress();
this.price = hotel.getPrice();
this.score = hotel.getScore();
this.brand = hotel.getBrand();
this.city = hotel.getCity();
this.starName = hotel.getStarName();
this.business = hotel.getBusiness();
this.location = hotel.getLatitude() + ", " + hotel.getLongitude();
this.pic = hotel.getPic();
}
}
- hotel索引庫
{
"mappings": {
"properties": {
"id": {
"type": "keyword"
},
"name": {
"type": "text",
"analyzer": "ik_max_word",
"copy_to": "all"
},
"address": {
"type": "keyword",
"index": false
},
"price": {
"type": "integer"
},
"score": {
"type": "integer"
},
"brand": {
"type": "keyword",
"copy_to": "all"
},
"city": {
"type": "keyword"
},
"starName": {
"type": "keyword"
},
"business": {
"type": "keyword",
"copy_to": "all"
},
"location": {
"type": "geo_point"
},
"pic": {
"type": "keyword",
"index": false
},
"all": {
"type": "text",
"analyzer": "ik_max_word"
}
}
}
}
基于elasticsearch的規(guī)則, id用keyword
- 操作索引庫
import com.zyw.elasticsearchdemo.constants.HotelConstants;
import org.apache.http.HttpHost;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.GetIndexRequest;
import org.elasticsearch.common.xcontent.XContentType;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.io.IOException;
public class ElasticsearchDemoApplicationTests {
private RestHighLevelClient client;
/**
* 刪除索引庫
*/
@Test
void deleteHotelIndex() throws IOException {
DeleteIndexRequest request = new DeleteIndexRequest("hotel");
client.indices().delete(request, RequestOptions.DEFAULT);
}
/**
* 判斷索引庫是否存在
*/
@Test
void existHotelIndex() throws IOException {
GetIndexRequest request = new GetIndexRequest("hotel");
boolean exists = client.indices().exists(request, RequestOptions.DEFAULT);
System.out.println(exists ? "索引庫已經(jīng)存在" : "索引庫不存在");
}
/**
* 創(chuàng)建索引庫
*/
@Test
void createHotelIndex() throws IOException {
// 1.創(chuàng)建request對象
CreateIndexRequest request = new CreateIndexRequest("hotel");
// 2.準(zhǔn)備請求的參數(shù), DSL語句
request.source(HotelConstants.MAPPING_TEMPLATE, XContentType.JSON);
// 3. 發(fā)送請求
client.indices().create(request, RequestOptions.DEFAULT);
}
@BeforeEach
void setUp() {
this.client = new RestHighLevelClient(RestClient.builder(HttpHost.create("http://82.114.174.50:9200")));
}
@AfterEach
void tearDown() throws IOException {
client.close();
}
}
RestClient操作文檔
import cn.hutool.json.JSONUtil;
import com.zyw.elasticsearchdemo.mapper.HotelMapper;
import com.zyw.elasticsearchdemo.pojo.Hotel;
import com.zyw.elasticsearchdemo.pojo.HotelDoc;
import org.apache.http.HttpHost;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.io.IOException;
import java.util.List;
@SpringBootTest
public class ElasticsearchDemoApplicationTests1 {
private RestHighLevelClient client;
@Autowired
private HotelMapper hotelMapper;
/**
* 刪除文檔
*/
@Test
void deleteDocument() throws IOException {
DeleteRequest request = new DeleteRequest("hotel", "200216665");
client.delete(request, RequestOptions.DEFAULT);
}
/**
* 修改文檔-局部更新, 全量和創(chuàng)建一樣
*/
@Test
void updateDocument() throws IOException {
UpdateRequest request = new UpdateRequest("hotel", "200216665");
request.doc("price", 2600, "starName", "六鉆");
client.update(request, RequestOptions.DEFAULT);
}
/**
* 查詢文檔
*/
@Test
void getDocument() throws IOException {
// 準(zhǔn)備request對象
GetRequest request = new GetRequest("hotel", "200216665");
// 發(fā)送請求
GetResponse response = client.get(request, RequestOptions.DEFAULT);
String json = response.getSourceAsString();
HotelDoc hotelDoc = JSONUtil.toBean(json, HotelDoc.class);
System.out.println(hotelDoc);
}
/**
* 新增文檔
*/
@Test
void addDocument() throws IOException {
// 根據(jù)id查詢酒店數(shù)據(jù)
Hotel hotel = hotelMapper.selectById(200216665);
// 轉(zhuǎn)換為文檔對象
HotelDoc hotelDoc = new HotelDoc(hotel);
// 準(zhǔn)備request對象
IndexRequest request = new IndexRequest("hotel").id(hotel.getId().toString());
// 準(zhǔn)備json文檔
request.source(JSONUtil.toJsonStr(hotelDoc), XContentType.JSON);
// 發(fā)送請求
client.index(request, RequestOptions.DEFAULT);
}
/**
* 批量導(dǎo)入文檔
*/
@Test
void batchAddDocument() throws IOException {
List<Hotel> hotels = hotelMapper.selectList(null);
BulkRequest request = new BulkRequest();
for (Hotel hotel : hotels) {
HotelDoc hotelDoc = new HotelDoc(hotel);
request.add(new IndexRequest("hotel").id(hotelDoc.getId().toString())
.source(JSONUtil.toJsonStr(hotelDoc), XContentType.JSON));
}
// 發(fā)送
client.bulk(request, RequestOptions.DEFAULT);
}
@BeforeEach
void setUp() {
this.client = new RestHighLevelClient(RestClient.builder(HttpHost.create("http://82.114.174.50:9200")));
}
@AfterEach
void tearDown() throws IOException {
client.close();
}
}
DSL查詢語法
分類和基本語法
全文檢索查詢
全文檢索查詢, 會對用戶輸入內(nèi)容分詞, 常用于搜索框搜索
建議把多個字段copy到一個字段里
精確查詢
地理查詢
復(fù)合查詢
- 復(fù)合(compound)查詢: 復(fù)合查詢可以將其他簡單查詢組合起來, 實現(xiàn)更復(fù)雜的搜索邏輯.
-
function score
: 復(fù)分函數(shù)查詢, 可以控制文檔相關(guān)性算分, 控制文檔排名. 例如百度競價
-
搜索結(jié)果處理
排序
分頁
高亮
默認(rèn)字段要一致, 可以用require_field_match
取消一致
RestClient查詢文檔–高級查詢
import cn.hutool.json.JSONUtil;
import com.zyw.elasticsearchdemo.pojo.HotelDoc;
import org.apache.http.HttpHost;
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.RestHighLevelClient;
import org.elasticsearch.common.geo.GeoPoint;
import org.elasticsearch.common.lucene.search.function.CombineFunction;
import org.elasticsearch.common.unit.DistanceUnit;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.functionscore.FunctionScoreQueryBuilder;
import org.elasticsearch.index.query.functionscore.ScoreFunctionBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightField;
import org.elasticsearch.search.sort.SortBuilders;
import org.elasticsearch.search.sort.SortOrder;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.io.IOException;
import java.util.Map;
public class QueryDocumentTest {
private RestHighLevelClient client;
/**
* 廣告置頂
*/
@Test
void adScore() throws IOException {
SearchRequest request = new SearchRequest("hotel");
BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();
boolQuery.must(QueryBuilders.termQuery("city", "上海"));
// 算分控制
FunctionScoreQueryBuilder functionScoreQuery = QueryBuilders.functionScoreQuery(boolQuery, new FunctionScoreQueryBuilder.FilterFunctionBuilder[]{
new FunctionScoreQueryBuilder.FilterFunctionBuilder(QueryBuilders.termQuery("isAd", true),
// 分?jǐn)?shù)10
ScoreFunctionBuilders.weightFactorFunction(10))
}).boostMode(CombineFunction.SUM); // 用加法 --> 分?jǐn)?shù)+10
request.source().query(functionScoreQuery);
SearchResponse response = client.search(request, RequestOptions.DEFAULT);
handleResponse(response);
}
/**
* 高亮
*/
@Test
void testHighlight() throws IOException {
SearchRequest request = new SearchRequest("hotel");
request.source().query(QueryBuilders.matchQuery("all", "維也納"));
// 高亮設(shè)置
request.source().highlighter(new HighlightBuilder().field("name").requireFieldMatch(false));
SearchResponse response = client.search(request, RequestOptions.DEFAULT);
handleResponse(response);
}
/**
* 排序和分頁
*/
@Test
void sortAndPage() throws IOException {
SearchRequest request = new SearchRequest("hotel");
request.source().sort("price", SortOrder.ASC).from(20).size(5);
request.source().query(QueryBuilders.matchAllQuery());
SearchResponse response = client.search(request, RequestOptions.DEFAULT);
handleResponse(response);
}
/**
* 根據(jù)地理坐標(biāo)排序
*/
@Test
void sortByLocation() throws IOException {
SearchRequest request = new SearchRequest("hotel");
request.source().sort(SortBuilders.geoDistanceSort("location",
// 坐標(biāo)字符串前面是緯度,后面是經(jīng)度
new GeoPoint("31.21, 121.5")).order(SortOrder.ASC).unit(DistanceUnit.KILOMETERS));
request.source().query(QueryBuilders.matchQuery("all", "如家"));
// 高亮設(shè)置
request.source().highlighter(new HighlightBuilder().field("name").requireFieldMatch(false));
SearchResponse response = client.search(request, RequestOptions.DEFAULT);
handleResponse(response);
}
/**
* bool查詢
* @throws IOException
*/
@Test
void testBool() throws IOException {
SearchRequest request = new SearchRequest("hotel");
BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();
// 添加term
boolQuery.must(QueryBuilders.termQuery("city", "上海"));
// 添加range
boolQuery.filter(QueryBuilders.rangeQuery("price").lte(500));
request.source().query(boolQuery);
SearchResponse response = client.search(request, RequestOptions.DEFAULT);
handleResponse(response);
}
/**
* bool查詢 --should
* @throws IOException
*/
@Test
void testBool() throws IOException {
SearchRequest request = new SearchRequest("hotel");
BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();
BoolQueryBuilder shouldQuery = QueryBuilders.boolQuery();
shouldQuery.should(QueryBuilders.matchQuery("name", "上海")).should(QueryBuilders.matchQuery("name","北京"));
shouldQuery.minimumShouldMatch(1); // name中有上?;蛘弑本?滿足一個
boolQuery.must(shouldQuery);
// 添加range
boolQuery.filter(QueryBuilders.rangeQuery("price").lte(180));
request.source().query(boolQuery);
SearchResponse response = client.search(request, RequestOptions.DEFAULT);
handleResponse(response);
}
/**
* match查詢
*/
@Test
void testMatch() throws IOException {
SearchRequest request = new SearchRequest("hotel");
request.source().query(QueryBuilders.matchQuery("all", "如家"));
SearchResponse response = client.search(request, RequestOptions.DEFAULT);
handleResponse(response);
}
/**
* 處理結(jié)果
* @param response
*/
private static void handleResponse(SearchResponse response) {
SearchHits searchHits = response.getHits();
// 查詢的總條數(shù)
long total = searchHits.getTotalHits().value;
System.out.println("total = " + total);
// 查詢的結(jié)果數(shù)組
SearchHit[] hits = searchHits.getHits();
for (SearchHit hit : hits) {
// 得到source
String json = hit.getSourceAsString();
HotelDoc hotelDoc = JSONUtil.toBean(json, HotelDoc.class);
// 獲取高亮結(jié)果
Map<String, HighlightField> highlightFields = hit.getHighlightFields();
if(!highlightFields.isEmpty()){
// 根據(jù)字段名獲取高亮結(jié)果
HighlightField highlightField = highlightFields.get("name");
// 獲取高亮值
String name = highlightField.getFragments()[0].toString();
// 覆蓋非高亮結(jié)果
hotelDoc.setName(name);
}
// 獲取location距離排序值 --> 距離4.5km
Object[] sortValues = hit.getSortValues();
if (sortValues.length != 0) {
hotelDoc.setDistance(sortValues[0]);
}
System.out.println("hotelDoc = " + hotelDoc);
}
}
/**
* 查詢所有
* @throws IOException
*/
@Test
void testMatchAll() throws IOException {
SearchRequest request = new SearchRequest("hotel");
request.source().query(QueryBuilders.matchAllQuery());
SearchResponse response = client.search(request, RequestOptions.DEFAULT);
handleResponse(response);
}
@BeforeEach
void setUp() {
this.client = new RestHighLevelClient(RestClient.builder(HttpHost.create("http://82.114.174.50:9200")));
}
@AfterEach
void tearDown() throws IOException {
client.close();
}
}
數(shù)據(jù)聚合
Bucket聚合
Metrics聚合
- 指定排序字段
RestClient實現(xiàn)聚合
import org.apache.http.HttpHost;
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.RestHighLevelClient;
import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.elasticsearch.search.aggregations.Aggregations;
import org.elasticsearch.search.aggregations.bucket.terms.Terms;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class AggregationTest {
private RestHighLevelClient client;
/**
* 品牌, 城市聚合
* example --> {品牌=[7天酒店, 如家, 皇冠假日, 速8, 萬怡, 華美達, 和頤, 萬豪, 喜來登, 希爾頓], 城市=[上海, 北京, 深圳]}
*/
@Test
void name() throws IOException {
Map<String, List<String>> result = new HashMap<>();
SearchRequest request = new SearchRequest("hotel");
// TODO 可以增加查詢條件過濾,條件和前面一樣,對滿足的文檔進行聚合
request.source().size(0); // 去掉文檔,只看聚合結(jié)果
request.source().aggregation(AggregationBuilders
.terms("brandAgg") // 名稱自己定
.field("brand")
.size(10)); // 結(jié)果的前十條
request.source().aggregation(AggregationBuilders
.terms("cityAgg") // 名稱自己定
.field("city")
.size(10)); // 結(jié)果的前十條
SearchResponse response = client.search(request, RequestOptions.DEFAULT);
// 處理結(jié)果
Aggregations aggregations = response.getAggregations();
List<String> brandList = getAggByName(aggregations, "brandAgg");
result.put("品牌", brandList);
List<String> cityList = getAggByName(aggregations, "cityAgg");
result.put("城市", cityList);
System.out.println(result);
}
private static List<String> getAggByName(Aggregations aggregations, String aggName) {
Terms brandterms = aggregations.get(aggName);
List<? extends Terms.Bucket> buckets = brandterms.getBuckets();
List<String> list = new ArrayList<>();
for (Terms.Bucket bucket : buckets) {
list.add(bucket.getKeyAsString());
}
return list;
}
@BeforeEach
void setUp() {
this.client = new RestHighLevelClient(RestClient.builder(HttpHost.create("http://82.114.174.50:9200")));
}
@AfterEach
void tearDown() throws IOException {
client.close();
}
}
拼音分詞器
github地址: https://github.com/infinilabs/analysis-pinyin
解壓放到elasticsearch插件目錄
重啟elasticsearch
- 測試
POST /_analyze
{
"text": "我們都是祖國的花朵",
"analyzer": "pinyin"
}
自定義分詞器
測試自定義分詞器
GET /test/_analyze
{
"text": "java是最好的語言",
"analyzer": "my_analyzer"
}
自動補全
RestApi實現(xiàn)自動補全
文章來源:http://www.zghlxwxcb.cn/news/detail-844094.html
- hotel索引庫
PUT /hotel
{
"settings": {
"analysis": {
"analyzer": {
"text_anlyzer": {
"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": {
"properties": {
"id":{
"type": "keyword"
},
"name":{
"type": "text",
"analyzer": "text_anlyzer",
"search_analyzer": "ik_smart",
"copy_to": "all"
},
"address":{
"type": "keyword",
"index": false
},
"price":{
"type": "integer"
},
"score":{
"type": "integer"
},
"brand":{
"type": "keyword",
"copy_to": "all"
},
"city":{
"type": "keyword"
},
"starName":{
"type": "keyword"
},
"business":{
"type": "keyword",
"copy_to": "all"
},
"location":{
"type": "geo_point"
},
"pic":{
"type": "keyword",
"index": false
},
"all":{
"type": "text",
"analyzer": "text_anlyzer",
"search_analyzer": "ik_smart"
},
"suggestion":{
"type": "completion",
"analyzer": "completion_analyzer",
"search_analyzer": "ik_smart"
}
}
}
}
- HotelDoc.java
增加suggestion
字段文章來源地址http://www.zghlxwxcb.cn/news/detail-844094.html
import lombok.Data;
import lombok.NoArgsConstructor;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
@Data
@NoArgsConstructor
public class HotelDoc {
private Long id;
private String name;
private String address;
private Integer price;
private Integer score;
private String brand;
private String city;
private String starName;
private String business;
private String location;
private String pic;
private Object distance;
private Boolean isAd;
private List<String> suggestion;
public HotelDoc(Hotel hotel) {
this.id = hotel.getId();
this.name = hotel.getName();
this.address = hotel.getAddress();
this.price = hotel.getPrice();
this.score = hotel.getScore();
this.brand = hotel.getBrand();
this.city = hotel.getCity();
this.starName = hotel.getStarName();
this.business = hotel.getBusiness();
this.location = hotel.getLatitude() + ", " + hotel.getLongitude();
this.pic = hotel.getPic();
if(this.business.contains("/")){
// business有多個值,切割
String[] arr = this.business.split("/");
this.suggestion = new ArrayList<>();
this.suggestion.add(this.brand);
Collections.addAll(this.suggestion, arr);
}else{
this.suggestion = Arrays.asList(this.brand,this.business);
}
}
}
- 查詢和解析結(jié)果
import org.apache.http.HttpHost;
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.RestHighLevelClient;
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.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.io.IOException;
import java.util.List;
public class SuggestionTest {
private RestHighLevelClient client;
/**
* 自動補全
*/
@Test
void test() throws IOException {
SearchRequest request = new SearchRequest("hotel");
request.source().suggest(new SuggestBuilder()
.addSuggestion("suggestions", // 名稱自定義 解析結(jié)果時要與此保持一致
SuggestBuilders.completionSuggestion("suggestion") // HotelDoc定義的字段
.prefix("sd") // 關(guān)鍵字
.skipDuplicates(true).size(10))
);
SearchResponse response = client.search(request, RequestOptions.DEFAULT);
Suggest suggest = response.getSuggest();
CompletionSuggestion suggestions = suggest.getSuggestion("suggestions"); // 名稱和上面一致
List<CompletionSuggestion.Entry.Option> options = suggestions.getOptions();
for (CompletionSuggestion.Entry.Option option : options) {
String text = option.getText().toString();
System.out.println(text);
}
}
@BeforeEach
void setUp() {
this.client = new RestHighLevelClient(RestClient.builder(HttpHost.create("http://82.114.174.50:9200")));
}
@AfterEach
void tearDown() throws IOException {
client.close();
}
}
補充
- 分詞
POST /_analyze
{
"text": "java是最好的語言",
"analyzer": "ik_smart"
}
- 查所有
GET /hotel/_search
到了這里,關(guān)于javaAPI操作Elasticsearch的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!