ES操作整理文档

一、索引配置

1、查询setting

GET /saas-mp-promotion-search_activity/_settings

2、创建setting

// 可以根据5拿到的配置创建索引
// number_of_shards-分片
// number_of_replicas-备份数
// analysis-分词分析器

PUT es_create_test
{
    "settings":{
        "index":{
            "number_of_shards":"3",
            "number_of_replicas":"0",
            "analysis":{
                "analyzer":{
                    "my_analyzer":{
                        "tokenizer":"my_tokenizer"
                    }
                },
                "tokenizer":{
                    "my_tokenizer":{
                        "token_chars":[
                            "letter",
                            "digit",
                            "whitespace",
                            "punctuation",
                            "symbol"
                        ],
                        "type":"ngram"
                    }
                }
            }
        }
    }
}

3、查询mapping

GET /saas-mp-promotion-search_activity/_mapping

4、创建mapping

type注意大小写

PUT saas-mp-promotion-goods-sync-test/_doc/_mapping?include_type_name=true
{
    "properties":{
        "activityId":{
            "type":"long"
        },
        "activityIdUnique":{
            "type":"keyword"
        },
        "activityLevelType":{
            "type":"integer"
        },
        "activityName":{
            "type":"text",
            "fields":{
                "cn":{
                    "type":"text",
                    "analyzer":"ik_max_word"
                },
                "en":{
                    "type":"text",
                    "analyzer":"english"
                },
                "raw":{
                    "type":"keyword"
                }
            },
            "analyzer":"my_analyzer"
        },
        "startTime":{
            "type":"date",
            "format":"yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"
        },
        "timeEffect":{
            "type":"nested",
            "properties":{
                "repeatDay":{
                    "type":"keyword"
                },
                "repeatEndInterval":{
                    "type":"keyword"
                },
                "repeatGmt":{
                    "type":"integer"
                },
                "repeatStartInterval":{
                    "type":"keyword"
                },
                "repeatType":{
                    "type":"integer"
                },
                "searchRepeatEndInterval":{
                    "type":"keyword"
                },
                "searchRepeatStartInterval":{
                    "type":"keyword"
                }
            }
        },
        "update_time":{
            "type":"date",
            "format":"yyyy-MM-dd HH:mm:ss"
        }
    }
}

二、操作doc

1、insert

POST /saas-mp-promotion-search_activity/_doc
{
	"activityIdUnique": "999-1721299"
}

2、delete

2.1、根据query删除

DELETE /saas-mp-promotion-search_activity/_doc/id

2.2、根据id删除

POST /saas-mp-promotion-search_activity/_delete_by_query
{
    "query":{
        "term":{
            "activityIdUnique":{
                "value":"3-1721299"
            }
        }
    }
}

3、update

update 请求最简单的一种形式是接收文档的一部分作为 doc 的参数, 它只是与现有的文档进行合并。对象被合并到一起,覆盖现有的字段,增加新的字段

3.1、根据id修改

POST /saas-mp-promotion-search_activity_scope/_update/101_3-21000950540215
{
    "doc":{
        "storeId":[
            1
        ]
    }
}

3.2、根据query修改

POST /saas-mp-promotion-search_activity_scope/_update_by_query
{
  "query": {
    "bool": {
      "must": [
        {
          "term": {
            "activityId": {
              "value": 2161
            }
          }
        }
      ]
    }
  },
  "script": {
    "source": "ctx._source.payType = 1;ctx._source.deliveryType = [1,2]"
  }
}

3.3、给已有字段为空的设置默认值

POST index_test/_doc/_update_by_query
{
    "script":{
        "lang":"painless",
        "source":"if (ctx._source.skuNumber == null) {ctx._source.skuNumber = 'sss'}"
    }
}

3.4、移除已有字段

POST saas-mp-promotion-search_goods/_update_by_query
{
  "script": {
    "lang": "painless",
    "inline": """ctx._source.remove("titleList.vid")"""
  },
  "query": {
    "ids": {
      "values" : ["103_120000_100313970052000"]
    }
  }
}

3.5、给字段加一个对象

POST test/_update_by_query
{
  "query": {
    "ids": {
      "values": [
        "103_120000_100313947052000"
      ]
    }
  },
  "script": {
    "lang": "painless",
    "source": "if(ctx._source.goodsTitleList==null){List ls = new ArrayList();ls.add(params.member);ctx._source.goodsTitleList=ls;}else{ctx._source.goodsTitleList.add(params.member)}",
    "params": {
      "member": {
        "belongVid": 0,
        "title": ""
      }
    }
  }
}

3.6、给已有list对象修改值(for循环使用)

POST test/_update_by_query
{
  "query": {
    "ids": {
      "values": ["103_120000_100313970052000"]
    }
  },
  "script": {
    "lang": "painless",
    "source": "for(e in ctx._source.goodsTitleList){e.belongVid = ctx._source.goodsBelongVid;e.title = ctx._source.title;}"
  }
}

3.7、删除已有list的其中一个对象

POST test/_update_by_query
{
  "query": {
    "ids": {
      "values": ["103_120000_100313970052000"]
    }
  },
  "script": {
    "lang": "painless",
    "source": "ctx._source.titleList.removeIf(list_item -> list_item.belongVid==120000)"
  }
}

4、query

4.1、根据id查询

GET test/_doc/id

4.2、根据ids查询

GET test/_search
{
    "query":{
        "ids":{
            "values":[1]
        }
    }
}

4.3、简单查询

GET /saas-mp-promotion-search_activity/_search
{
    "query":{
        "bool":{
            "must":[
                {
                    "term":{
                        "bosId":{
                            "value":4000180336837
                        }
                    }
                },
                {
                    "range":{
                        "endTime":{
                            "gte":1642504062959
                        }
                    }
                },
                {
                    "terms":{
                        "activityStatus":[
                            "0",
                            "1"
                        ]
                    }
                }
            ],
            "must_not":[
                {
                    "exists":{
                        "field":"activityIdUnique"
                    }
                }
            ]
        }
    }
}

4.4、nested查询

GET /saas-mp-promotion-search_goods/_search
{
  "track_total_hits": true,
  "query": {
    "nested": {
      "path": "goodsStock",
      "query": {
        "exists": {
          "field": "goodsStock.sellQuantity"
        }
      }
    }
  }
}

4.5、nested判空查询

GET saas-mp-promotion-search_goods/_search
{
  "query": {
    "bool": {
      "must_not": [
        {
          "nested": {
            "path": "goodsVidInfoList",
            "query": {
              "exists": {
                "field": "goodsVidInfoList"
              }
            }
          }
        }
      ]
    }
  }
}

4.6、判断两个字段是否相等

POST /saas-mp-promotion-search_activity/_search
{
  "track_total_hits": true,
  "query": {
    "bool": {
      "must": [
        {
          "term": {
            "bizSource": {
              "value": 101
            }
          }
        },
        {
          "exists": {
            "field": "outActivityId"
          }
        },
        {
          "script": {
            "script": {
               "inline": "doc['activityId'] != doc['outActivityId']",
               "lang": "painless"
            }
          }
        }
      ]
    }
  }
}

4.7、nested嵌套sort

GET /saas-mp-promotion-search_goods/_search
{
  "track_total_hits": true, 
  "_source": ["goodsVidInfoList","goodsId", "vid","esUpdateTime"], 
  "query": {
    "bool": {
      "must": [
        {
          "term": {
            "vid": {
              "value": 6000330694837
            }
          }
        }
      ]
    }
  },
  "sort": [
    {
      "goodsVidInfoList.sort": {
        "order": "asc",
        "nested": {
          "path": "goodsVidInfoList",
          "filter": {
            "terms": {
              "goodsVidInfoList.vid": [6000330694837]
            }
          }
        }
      }
    },
    {
      "esUpdateTime": {
        "order": "desc"
      }
    }
  ]
}

5、重建索引

POST _reindex
{
    "source":{
        "index":"test1"
    },
    "dest":{
        "index":"test_20220117"
    },
    "script":{
        "inline":"ctx._routing= ctx._source.bosId",
        "lang":"painless"
    }
}

6、加别名

POST _aliases
{
    "actions":[
        {
            "add":{
                "index":"test",
                "alias":"test_alias"
            }
        }
    ]
}

7、查看后台任务

GET _tasks?detailed=true&actions=indices:data/write/update/byquery

8、修改原有字段类型

es不支持直接修改字段类型,需要通过重建索引来实现
步骤:
1、创建一个和原先索引结构一致(除了需要修改的字段)的新索引(setting/mapping)
2、通过reindex将老索引数据导入到新索引里
3、将新索引设置到现有别名
4、删除老索引

9、忽略大小写

9.1、text

通过自定义分词器my_analyzer,加filter:["lowercase"]。property里使用my_analyzer即可,如:title的analyzer。

9.2、keywords

自定义lowercase_normalizer,加filter:["lowercase"]。keywrods字段使用lowercase_normalizer即可,如title.row的normalizer

PUT saas-mp-promotion-search_goods_20220605
{
  "settings": {
    "index": {
      "analysis": {
        "analyzer": {
          "my_analyzer": {
            "filter": [
              "lowercase"
            ],
            "tokenizer": "my_tokenizer"
          }
        },
        "tokenizer": {
          "my_tokenizer": {
            "token_chars": [
              "letter",
              "digit",
              "whitespace",
              "punctuation",
              "symbol"
            ],
            "type": "ngram"
          }
        },
        "normalizer" : {
          "lowercase_normalizer": {
            "filter" : ["lowercase"]
          }
        }
      }
    }
  }
}
PUT saas-mp-promotion-search_goods_20220605/_doc/_mapping?include_type_name=true
{
    "properties":{
        "goodsVidInfoList":{
            "type":"nested",
            "properties":{
                "belongVid":{
                    "type":"long"
                },
                "sort":{
                    "type":"integer"
                },
                "title":{
                    "type":"text",
                    "fields":{
                        "cn":{
                            "type":"text",
                            "analyzer":"ik_max_word"
                        },
                        "en":{
                            "type":"text",
                            "analyzer":"english"
                        },
                        "raw":{
                            "type":"keyword",
                            "normalizer":"lowercase_normalizer"
                        }
                    },
                    "analyzer":"my_analyzer"
                },
                "vid":{
                    "type":"long"
                }
            }
        }
    }
}

posted on 2022-05-13 10:31  Iversonstear  阅读(33)  评论(0编辑  收藏  举报

导航