es的一些记录

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

        elasticsearch/docs at main · elastic/elasticsearch · GitHubhttps://github.com/elastic/elasticsearch/tree/main/docs

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;
    }
}

文档

https://www.elastic.co/guide/en/elasticsearch/client/java-rest/current/java-rest-high-document-index.html

17.要range的字段,不能为keyword,要为double

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值