1.put和post都是提交数据,两者的区别:put必须带ID如果不带ID直接报错,如果ID已存在则是更新,ID不存在则是新增,POST可以不带ID,不带ID则是新增。如果带了ID,ID已存在则是修改,ID不存在则是新增
2.get是查询数据,在返回的结果中有_seq_no,并发控制字段,每次更新都会+1,用来做乐观锁;_primary_term:同上,主分片重新分配,每次重启都会分配新的数值,怎么使用呢,就是在url后面拼接上着两个参数,例?if_seq_no=1&id_primary_term=1 如果这两个数值都正确那么就会进行更新,不正确就不会更新
3.更新操作post带_update,如果和原来的数据一模一样这样就不进行更新操作,seq_no,version_id都不会改变,而且请求体中要在更新的内容外面加一层"doc": {};put和post更新都不会检查元素直接更新
4.批量操作post请求 url后面添加_bulk 请求体格式:
{action:{metadata}}
{requestbody};action指的是操作,metadata是指的是元数据(_index,_id等)requestbody是数据体;批量操作没有事物的说法,每个操作都是独立的
4.查询,可以在url中直接添加要查询的参数,也可将要查询的条件写到请求体中
get bank/_search
{
"query":{
"match_all": {}
},
"sort":[
{
"account_number": "asc"
},
{
"balance": "desc"
}
],
"from": 10,
"size": 10
"_source":["balance","firstname"] ###指定要查询的列
}
根据某个字段进行查询,支持模糊查询
"query": {
"match": {
"address": "kings"
}
}
进行短语匹配,把后面的当做一个短语一块匹配
GET /bank/_search
{
"query": {
"match_phrase": {
"address": "mill road"
}
}
}
多字段匹配,类似于数据库中or,多字段中也是进行分词的
GET /bank/_search
{
"query": {
"multi_match": {
"query": "mill",
"fields": ["address","city"]
}
}
}
联合查询,这里面的should之后影响搜索的得分,并不会影响结果。而must_not不会影响得分,这个相当于一个过滤器,只会对结果有影响
GET /bank/_search
{
"query": {
"bool": {
"must": [
{"match": {
"gender": "M"
}},
{
"match": {
"address": "mill"
}
}
],
"must_not": [
{"match": {
"lastname": "wa"
}}
],
"should": [
{"match": {
"address": "1"
}}
]
}
}
}
找出某个字段在某个范围内的记录,这个结果是有得分的,当把must换成filter时是没有得分的
GET bank/_search
{
"query": {
"bool": {
"must": [
{
"range": {
"age": {
"gte": 10,
"lte": 20
}
}
}
]
}
}
}
如果是精准的值的话,就用term(像年龄等)如果是文本的全文搜索的话用match,某个字段.keywords也是精确匹配的
聚合查询,即对结果进行一些分析比如求平均值,获取数据分布等
格式:
"aggs": {
"agg_name": {
"agg_类型(term:获取数据的分布,avg平均值等)": {
"field": "age",
"size": 100
}
}
可以agg可以多个同级,也可以作为下属,将结果作为两一个agg的入参
例:获取每个年龄的人数和工资的平均值
GET bank/_search
{
"query": {
"match_all": {}
},
"aggs": {
"ageAgg": {
"terms": {
"field": "age",
"size": 100
},
"aggs": {
"ageAvg": {
"avg": {
"field": "balance"
}
}
}
}
}
}
映射:指的是索引中的数据的类型
GET bank/_mapping
创建一个索引的映射(keyword只能精确匹配,text可以全局索引,integer只能用term查询)
PUT /my_index
{
"mappings": {
"properties": {
"age":{"type": "integer"},
"email":{"type": "keyword"},
"name":{"type": "text"}
}
}
}
给一个index添加一个映射,index如果为false则是不让根据这个字段进行检索
PUT /my_index/_mapping
{
"properties": {
"employee_id":{
"type": "integer",
"index": false
}
}
}
想要修改映射,只能进行数据迁移,先创建一个新的索引,然后进行数据迁移
数据迁移(type在his中有):
POST _reindex
{
"source": {
"index": "bank",
"type": "account"
},
"dest": {
"index": "newbank"
}
}
springboot 整合 elasticsearch client
引入依赖
org.elasticsearch.client
elasticsearch-rest-high-level-client
7.4.2
这里要记得修改elasticsearch的依赖,因为这个依赖springboot本身就有这个依赖,必须要替换成和客户端相同的才行
org.springframework.boot
spring-boot-parent
2.1.8.RELEASE
7.4.2
进行相关的配置
package com.yuchen.mailsearch.config;
import org.apache.http.HttpHost;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class MailElasticsearchConfig {
public static final RequestOptionsCOMMON_OPTIONS;
static {
RequestOptions.Builder builder = RequestOptions.DEFAULT.toBuilder();
// builder.addHeader("Authorization", "Bearer " + TOKEN);
// builder.setHttpAsyncResponseConsumerFactory(
// new HttpAsyncResponseConsumerFactory
// .HeapBufferedResponseConsumerFactory(30 * 1024 * 1024 * 1024));
COMMON_OPTIONS = builder.build();
}
@Bean
public RestHighLevelClient esRestClient(){
RestHighLevelClient restHighLevelClient =new RestHighLevelClient(
RestClient.builder(
new HttpHost("192.168.56.10",9200,"http")
)
);
return restHighLevelClient;
}
}
进行操作
package com.yuchen.mailsearch;
import com.alibaba.fastjson.JSON;
import com.yuchen.mailsearch.config.MailElasticsearchConfig;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RestHighLevelClient;
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.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.aggregations.metrics.Avg;
import org.elasticsearch.search.aggregations.metrics.AvgAggregationBuilder;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.hibernate.validator.resourceloading.AggregateResourceBundleLocator;
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.Map;
@RunWith(SpringRunner.class)
@SpringBootTest
public class MailSearchApplicationTests {
@Autowired
RestHighLevelClientrestHighLevelClient;
@Test
public void searchData()throws IOException {
SearchRequest searchRequest =new SearchRequest();
searchRequest.indices("bank");
SearchSourceBuilder searchSourceBuilder =new SearchSourceBuilder();
searchSourceBuilder.query(QueryBuilders.matchQuery("address","mill"));
//求年龄的值分布
TermsAggregationBuilder ageAgg = AggregationBuilders.terms("ageAgg").field("age").size(10);
searchSourceBuilder.aggregation(ageAgg);
//求平均工资
AvgAggregationBuilder balanceAvg = AggregationBuilders.avg("balanceAvg").field("balance");
searchSourceBuilder.aggregation(balanceAvg);
System.out.println(searchSourceBuilder.toString());
searchRequest.source(searchSourceBuilder);
SearchResponse search =restHighLevelClient.search(searchRequest, MailElasticsearchConfig.COMMON_OPTIONS);
System.out.println(search.toString());
//分析结果
SearchHits hits = search.getHits();
SearchHit[] searchHits = hits.getHits();
for (SearchHit searchHit : searchHits){
String index = searchHit.getIndex();
String id = searchHit.getId();
float score = searchHit.getScore();
String sourceAsString = searchHit.getSourceAsString();
System.out.println("index"+index+"id"+id+"score"+score+"sourceAsString"+sourceAsString);
}
Aggregations aggregations = search.getAggregations();
Terms aggregation = aggregations.get("ageAgg");
for (Terms.Bucket bucket : aggregation.getBuckets()){
String keyAsString = bucket.getKeyAsString();
System.out.println("年龄" + keyAsString);
}
Avg aggregation1 = aggregations.get("balanceAvg");
System.out.println("平均薪资" + aggregation1.getValueAsString());
}
}