壹:入門
一、新增數(shù)據(jù)
/coustomer/external/1 在coustomer索引下external類型中保留1號數(shù)據(jù)
1.POST:如果不指定id,會自動生成id泪勒,指定id就會修改沒有就新增
2.PUT:必須指定id 不指定會報錯盗冷,指定一般用于修改操作
二均澳、查詢數(shù)據(jù)
1.GET:/coustomer/external/1 查詢id為1的數(shù)據(jù)
三曲掰、修改數(shù)據(jù)
1.PUT樂觀鎖做并發(fā)修改:每次修改_seq_no都會發(fā)生改變
2.PUT POST: 帶_update 更新會對比源數(shù)據(jù),如果沒做改變胎许,那么什么都不變
不帶 _update 不會檢查元數(shù)據(jù)
四峻呛、刪除數(shù)據(jù)
1.DETELE:/coustomer/external/id刪除為id的數(shù)據(jù)
貳:進階
一罗售、SearchAPI
ES支持兩種檢索:
1.一個是通過使用REST request uri 發(fā)送搜索數(shù)據(jù)(檢索參數(shù))
GET bank/_search?q=*&sort=number:asc
2.另一個是通過 REST request body來發(fā)送(請求體)
GET bank/_search
{
"query": {
"match_all": {}
},
"sort":[
{
"number:asc"
}
]
}
二、QueryDSL基本使用
match_all:
match全文檢索 匹配查詢
GET bank/_search
{
"query": {
"match": {
"account_number": "20"
}
}
}
也可以模糊
GET bank/_search
{
"query": {
"match": {
"address": "Kings"
}
}
}
match_phrase短語匹配
multi_match多字段匹配
bool復合查詢:
加入多種條件
GET bank/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"gender": "M"
}
},
{
"match": {
"address": "mill"
}
}
],
"must_not": [
{
"match": {
"age": "30"
}
}
],
"should": [
{
"match": {
"lastname": "Wallace"
}
}
]
}
}
}
總結(jié):must 必須滿足 must_not 必須不滿足 should 可以不滿足也可以滿足
filter
GET bank/_search
{
"query": {
" filter":{
"range": {
"age": {
"gte": 10,
"lte": 30
}
}
}
}
}
用filter過濾區(qū)間就不會如上查詢獲得相關性得分
term查詢
term和match一樣是查詢
term不適合文本字段查詢杀饵,精確數(shù)組查詢推薦用term,文本字段檢索推薦使用match
.keyword
精準匹配 代表匹配文本字段的整個精確值(不分詞匹配)
aggregations聚合
查出所有年齡分布莽囤,并且這些年齡中M的平均薪資和F的平均薪資以及這個年齡段的總體平均薪資
GET bank/_search
{
"query": {"match_all": {}},
"aggs": {
"ageAgg": {
"terms": {
"field": "age",
"size": 100
},
"aggs": {
"genderAvg": {
"terms": {
"field": "gender.keyword",
"size": 10
},"aggs": {
"genderBanlance": {
"avg": {
"field": "balance"
}
}
}
},
"allBalanceAvg":{
"avg": {
"field": "balance"
}
}
}
}
},
"size": 0
}
mappings映射
可以在第一次保存數(shù)據(jù)前可以給索引指定映射,創(chuàng)建索引時指定映射
PUT /my_index
{
"mappings":{
"properties" : {
"account_number" : {
"type" : "long"
}
}
}
}
數(shù)據(jù)遷移
老數(shù)據(jù)有Type的切距,需要指定type 沒有的就不用指定
POST _reindex
{
"source":{
"index":"bank",
"type" :"account"
},
"dest":{
"index":"newbank"
}
}
分詞
分詞器自定義文字朽缎,減少檢索時間
使用nginx配置分詞器,
進入 ES的plugins 中修改 ik分詞的配置
range 區(qū)間查詢
GET /_search
{
"query":{
"bool":{
"filter":{
"range":{
"age":{
"gte": 0,
"lte":200
}
}
}
}
}
}
整合SpringBoot
一谜悟、導入依賴
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>7.12.1</version>
</dependency>
二话肖、創(chuàng)建bean容器
@Configuration
public class GulimallElasticSearchConfig {
public static final RequestOptions COMMON_OPTIONS;
static {
RequestOptions.Builder builder = RequestOptions.DEFAULT.toBuilder();
COMMON_OPTIONS = builder.build();
}
@Bean
public RestHighLevelClient esRestClint(){
RestClientBuilder restClientBuilder = null;
restClientBuilder = RestClient.builder(new HttpHost("192.168.56.22", 9200, "http"));
RestHighLevelClient restHighLevelClient = new RestHighLevelClient(restClientBuilder);
return restHighLevelClient;
}
}
三、實現(xiàn)代碼案例
package com.atguigu.gulimail.search;
import com.alibaba.fastjson.JSON;
import com.atguigu.gulimail.search.config.GulimallElasticSearchConfig;
import lombok.Data;
import lombok.ToString;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.aggregations.Aggregation;
import org.elasticsearch.search.aggregations.AggregationBuilder;
import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.elasticsearch.search.aggregations.Aggregations;
import org.elasticsearch.search.aggregations.bucket.terms.Terms;
import org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import java.io.IOException;
import java.util.List;
@RunWith(SpringRunner.class)
@SpringBootTest
public class GulimailSearchApplicationTests {
@Autowired
RestHighLevelClient restHighLevelClient;
@Data
@ToString
static class searchAs{
private int account_number;
private int balance;
private String firstname;
private String lastname;
private int age;
private String gender;
private String address;
private String employer;
private String email;
private String city;
private String state;
}
@Test
public void indexEs() throws IOException {
IndexRequest indexRequest = new IndexRequest("user");
indexRequest.id("1");
User user = new User();
user.setName("張三");
user.setAddUrl("杭州市");
String s = JSON.toJSONString(user);
indexRequest.source(s, XContentType.JSON);
IndexResponse index = restHighLevelClient.index(indexRequest, GulimallElasticSearchConfig.COMMON_OPTIONS);
System.out.println(index.toString());
}
@Test
public void search() throws IOException {
//創(chuàng)建檢索條件
SearchRequest searchRequest = new SearchRequest();
//指定索引
searchRequest.indices("bank");
//檢索條件
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
searchSourceBuilder.query(QueryBuilders.matchQuery("address","Bristol"));
TermsAggregationBuilder ageAgg = AggregationBuilders.terms("ageAgg").field("age").size(10);
searchSourceBuilder.aggregation(ageAgg);
searchRequest.source(searchSourceBuilder);
//執(zhí)行檢索
SearchResponse search = restHighLevelClient.search(searchRequest, GulimallElasticSearchConfig.COMMON_OPTIONS);
//獲取檢索數(shù)據(jù)
SearchHits hits = search.getHits();
SearchHit[] hits1 = hits.getHits();
for (SearchHit documentFields : hits1) {
String sourceAsString = documentFields.getSourceAsString();
searchAs searchAs = JSON.parseObject(sourceAsString, searchAs.class);
System.out.println(searchAs.toString());
}
Aggregations aggregations = search.getAggregations();
Terms terms = aggregations.get("ageAgg");
List<? extends Terms.Bucket> buckets = terms.getBuckets();
}
@Data
class User{
private String name;
private String addUrl;
}
@Test
public void contextLoads() {
System.out.println(restHighLevelClient);
}
}