"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
* 操作索引庫(kù)
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;
/\*\*
* 刪除索引庫(kù)
*/
@Test
void deleteHotelIndex() throws IOException {
DeleteIndexRequest request = new DeleteIndexRequest(“hotel”);
client.indices().delete(request, RequestOptions.DEFAULT);
}
/\*\*
* 判斷索引庫(kù)是否存在
*/
@Test
void existHotelIndex() throws IOException {
GetIndexRequest request = new GetIndexRequest(“hotel”);
boolean exists = client.indices().exists(request, RequestOptions.DEFAULT);
System.out.println(exists ? “索引庫(kù)已經(jīng)存在” : “索引庫(kù)不存在”);
}
/\*\*
* 創(chuàng)建索引庫(kù)
*/
@Test
void createHotelIndex() throws IOException {
// 1.創(chuàng)建request對(duì)象
CreateIndexRequest request = new CreateIndexRequest(“hotel”);
// 2.準(zhǔn)備請(qǐng)求的參數(shù), DSL語(yǔ)句
request.source(HotelConstants.MAPPING_TEMPLATE, XContentType.JSON);
// 3. 發(fā)送請(qǐng)求
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對(duì)象
GetRequest request = new GetRequest(“hotel”, “200216665”);
// 發(fā)送請(qǐng)求
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)換為文檔對(duì)象
HotelDoc hotelDoc = new HotelDoc(hotel);
// 準(zhǔn)備request對(duì)象
IndexRequest request = new IndexRequest(“hotel”).id(hotel.getId().toString());
// 準(zhǔn)備json文檔
request.source(JSONUtil.toJsonStr(hotelDoc), XContentType.JSON);
// 發(fā)送請(qǐng)求
client.index(request, RequestOptions.DEFAULT);
}
/\*\*
* 批量導(dǎo)入文檔
*/
@Test
void batchAddDocument() throws IOException {
List 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查詢語(yǔ)法
---
### 分類和基本語(yǔ)法
#### 全文檢索查詢
全文檢索查詢, 會(huì)對(duì)用戶輸入內(nèi)容分詞, 常用于搜索框搜索
建議把多個(gè)字段copy到一個(gè)字段里
#### 精確查詢
#### 地理查詢
#### 復(fù)合查詢
* 復(fù)合(compound)查詢: 復(fù)合查詢可以將其他簡(jiǎn)單查詢組合起來(lái), 實(shí)現(xiàn)更復(fù)雜的搜索邏輯.
+ `function score`: 復(fù)分函數(shù)查詢, 可以控制文檔相關(guān)性算分, 控制文檔排名. 例如百度競(jìng)價(jià)
## 搜索結(jié)果處理
---
### 排序
### 分頁(yè)
### 高亮
默認(rèn)字段要一致, 可以用`require_field_match` 取消一致
## RestClient查詢文檔–高級(jí)查詢
---
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);
}
/\*\*
* 排序和分頁(yè)
*/
@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(“l(fā)ocation”,
// 坐標(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中有上?;蛘弑本?滿足一個(gè)
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) {
自我介紹一下,小編13年上海交大畢業(yè),曾經(jīng)在小公司待過(guò),也去過(guò)華為、OPPO等大廠,18年進(jìn)入阿里一直到現(xiàn)在。
深知大多數(shù)Linux運(yùn)維工程師,想要提升技能,往往是自己摸索成長(zhǎng)或者是報(bào)班學(xué)習(xí),但對(duì)于培訓(xùn)機(jī)構(gòu)動(dòng)則幾千的學(xué)費(fèi),著實(shí)壓力不小。自己不成體系的自學(xué)效果低效又漫長(zhǎng),而且極易碰到天花板技術(shù)停滯不前!
因此收集整理了一份《2024年Linux運(yùn)維全套學(xué)習(xí)資料》,初衷也很簡(jiǎn)單,就是希望能夠幫助到想自學(xué)提升又不知道該從何學(xué)起的朋友,同時(shí)減輕大家的負(fù)擔(dān)。
既有適合小白學(xué)習(xí)的零基礎(chǔ)資料,也有適合3年以上經(jīng)驗(yàn)的小伙伴深入學(xué)習(xí)提升的進(jìn)階課程,基本涵蓋了95%以上Linux運(yùn)維知識(shí)點(diǎn),真正體系化!
由于文件比較大,這里只是將部分目錄大綱截圖出來(lái),每個(gè)節(jié)點(diǎn)里面都包含大廠面經(jīng)、學(xué)習(xí)筆記、源碼講義、實(shí)戰(zhàn)項(xiàng)目、講解視頻,并且后續(xù)會(huì)持續(xù)更新
如果你覺(jué)得這些內(nèi)容對(duì)你有幫助,可以添加VX:vip1024b (備注Linux運(yùn)維獲?。?/strong>
最后的話
最近很多小伙伴找我要Linux學(xué)習(xí)資料,于是我翻箱倒柜,整理了一些優(yōu)質(zhì)資源,涵蓋視頻、電子書(shū)、PPT等共享給大家!
資料預(yù)覽
給大家整理的視頻資料:
給大家整理的電子書(shū)資料:
如果本文對(duì)你有幫助,歡迎點(diǎn)贊、收藏、轉(zhuǎn)發(fā)給朋友,讓我有持續(xù)創(chuàng)作的動(dòng)力!文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-854493.html
一個(gè)人可以走的很快,但一群人才能走的更遠(yuǎn)。不論你是正從事IT行業(yè)的老鳥(niǎo)或是對(duì)IT行業(yè)感興趣的新人,都?xì)g迎掃碼加入我們的的圈子(技術(shù)交流、學(xué)習(xí)資源、職場(chǎng)吐槽、大廠內(nèi)推、面試輔導(dǎo)),讓我們一起學(xué)習(xí)成長(zhǎng)!
節(jié)點(diǎn)里面都包含大廠面經(jīng)、學(xué)習(xí)筆記、源碼講義、實(shí)戰(zhàn)項(xiàng)目、講解視頻,并且后續(xù)會(huì)持續(xù)更新**
如果你覺(jué)得這些內(nèi)容對(duì)你有幫助,可以添加VX:vip1024b (備注Linux運(yùn)維獲?。?/strong>
[外鏈圖片轉(zhuǎn)存中…(img-p8iNszVY-1712497682353)]
最后的話
最近很多小伙伴找我要Linux學(xué)習(xí)資料,于是我翻箱倒柜,整理了一些優(yōu)質(zhì)資源,涵蓋視頻、電子書(shū)、PPT等共享給大家!
資料預(yù)覽
給大家整理的視頻資料:
[外鏈圖片轉(zhuǎn)存中…(img-YmrctqjR-1712497682353)]
給大家整理的電子書(shū)資料:
[外鏈圖片轉(zhuǎn)存中…(img-oMK0Ch9E-1712497682353)]
如果本文對(duì)你有幫助,歡迎點(diǎn)贊、收藏、轉(zhuǎn)發(fā)給朋友,讓我有持續(xù)創(chuàng)作的動(dòng)力!
一個(gè)人可以走的很快,但一群人才能走的更遠(yuǎn)。不論你是正從事IT行業(yè)的老鳥(niǎo)或是對(duì)IT行業(yè)感興趣的新人,都?xì)g迎掃碼加入我們的的圈子(技術(shù)交流、學(xué)習(xí)資源、職場(chǎng)吐槽、大廠內(nèi)推、面試輔導(dǎo)),讓我們一起學(xué)習(xí)成長(zhǎng)!
[外鏈圖片轉(zhuǎn)存中…(img-TaESOOm4-1712497682354)]文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-854493.html
到了這里,關(guān)于javaAPI操作Elasticsearch_elasticsearch 修改字段 java api的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!