1.POST时带_update
对比元数据如果一样就不进行任何操作。
看场景:
- 对于大并发更新,不带_update
- 对于大并发查询偶尔更新,带_update;对比更新,重新计算分配规
主要是为了效率高
2.put与post的区别
put必须带id,post可以批量
3.批量导入数据
POST http://192.168.56.10:9200/customer/external/_bulk
两行为一个整体
{"index":{"_id":"1"}}
{"name":"a"}
{"index":{"_id":"2"}}
{"name":"b"}
注意格式json和text均不可,要去kibana里Dev Tools
4.index
动词增加 = insert。名词索引 = 数据库
5.github
6."match": { "account_number": "20" }
对非字符串进行精确匹配
对字符串,会进行全文检索
7.{ "query": { "match_phrase": { "address": "mill road"}}}
# 就是说不要匹配只有mill或只有road的,要匹配mill road一整个子串 ,不对字符串进行分词
现在要都包含才查出
8.{ "query": { "match": { "address.keyword": "990 Mill" # 字段后面加上 .keyword } } }
相当于精确匹配
9.
- must 贡献得分 must改为filter也会不贡献得分
- should 贡献得分
- must_not 不贡献得分 相当于过滤,
- filter 不贡献得分
不贡献得分速度更快
10.query/term
和match一样。匹配某个属性的值。
match:用于文本text检索
term:用于非text文本检索
11.nested对象聚合
属性是"type": “nested”,因为是内部的属性进行检索
数组类型的对象会被扁平化处理(对象的每个属性会分别存储到一起)
user.name=["aaa","bbb"]
user.addr=["ccc","ddd"]
这种存储方式,可能会发生如下错误: 错误检索到{aaa,ddd},这个组合是不存在的
12.mapping
指定字段的类型
第一次存储数据的时候es就猜出了映射
第一次存储数据前可以指定映射
指定了映射了之后,确定的映射不能被修改,可以新增加字段映射
数据迁移(想改变字段的类型)
13. es7以后 type被废除
不同type下不同mapping但相同名称的字段,会影响es内核的判断
14.es只会对于新增的数据用更新分词。历史数据是不会重新分词的。如果想要历史数据重新分词,需要执行:
POST my_index/_update_by_query?conflicts=proceed
15.聚合查询
{
"query": {
"bool": {
"must": [
{
"match": {
"skuTitle": "HUAWEI"
}
}
],
"filter": [
{
"term": {
"catalogId": 225
}
},
{
"terms": {
"brandId": [
"1"
]
}
},
{
"term": {
"hasStock": true
}
},
{
"range": {
"skuPrice": {
"gte": 100,
"lte": 9999
}
}
},
{
"nested": {
"path": "attrs",
"query": {
"bool": {
"filter": [
{
"term": {
"attrs.attrId": "2"
}
},
{
"terms": {
"attrs.attrValue": [
"华为 mate 50"
]
}
}
]
}
}
}
},
{
"nested": {
"path": "attrs",
"query": {
"bool": {
"filter": [
{
"term": {
"attrs.attrId": "3"
}
},
{
"terms": {
"attrs.attrValue": ["是"]
}
}
]
}
}
}
}
]
}
},
"aggs": {
"aggs_brand": {
"terms": {
"field": "brandId",
"size": 10
},
"aggs": {
"aggs_brand_name": {
"terms": {
"field": "brandName",
"size": 10
}
}
}
},
"aggs_attr": {
"nested": {
"path": " "
},
"aggs": {
"agg_attrId": {
"terms": {
"field": "attrs.attrId",
"size": 10
},
"aggs": {
"agg_attrName": {
"terms": {
"field": "attrs.attrName",
"size": 10
}
},
"agg_attrValue": {
"terms": {
"field": "attrs.attrValue",
"size": 10
}
}
}
}
}
}
}
}
16.springboot整合
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>7.4.2</version>
</dependency>
<properties>
<java.version>1.8</java.version>
<elasticsearch.version>7.4.2</elasticsearch.version>
</properties>
配置类
@Configuration
public class GuliESConfig {
public static final RequestOptions COMMON_OPTIONS;
static {
RequestOptions.Builder builder = RequestOptions.DEFAULT.toBuilder();
COMMON_OPTIONS = builder.build();
}
@Bean
public RestHighLevelClient esRestClient() {
RestClientBuilder builder = null;
// 可以指定多个es
builder = RestClient.builder(new HttpHost(host, 9200, "http"));
RestHighLevelClient client = new RestHighLevelClient(builder);
return client;
}
}
文档
17.要range的字段,不能为keyword,要为double