ES(ElasticSearch)索引笔记

2019-12-17  本文已影响0人  依弗布德甘

1.索引脚本新增字段:

## 创建索引   
PUT user_info_index_2
{ 
    "mappings": {
      "user_info_index": {
        "properties": {
          "user.csId": {
            "type": "long",
            "null_value": 0
          },
          "user.csName": {
            "type": "keyword",
            "null_value":""
          },
          "userBase.csAmount": {
            "type": "double",
            "null_value": 0
          },
          "userBase.csSalary": {
            "type": "double",
            "null_value": 0
          },
          "userBase.csTime": {
            "type": "date",
            "null_value": "-62167420800000",
            "format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"
          },
          "userBase.csLabel": {
            "type": "text",
            "fields": {
              "row": {
                "type": "keyword",
                "null_value": ""
              }
            },
            "analyzer": "ngram_analyzer"
          } 
        }
      }
    },
    "settings": {
      "index": {
        "number_of_shards": "3",
        "routing_partition_size": "1", 
        "max_inner_result_window": "50000",
        "max_result_window": "50000", 
        "analysis": {
          "analyzer": {
            "ngram_analyzer": {
              "tokenizer": "ngram_tokenizer"
            }
          },
          "tokenizer": {
            "ngram_tokenizer": {
              "token_chars": [
                "letter",
                "digit",
                "punctuation"
              ],
              "type": "ngram",
              "max_gram": "1"
            }
          }
        },
        "number_of_replicas": "1"
      }
    }
}
POST localhost:9200/_aliases  
{
  "actions":[{
      "add":{
        "index":"user_info_index_2",
        "alias":"user_info_index"
      }
   }]
}

2.索引关键字


3.索引搜索

## 查询名称等于张三的数据
GET user_info_index_2/_search
{
  "query":{
    "term":{
      "user.csName":"张三"
    }
  }
}

##搜索csAmount等于200.5的数据
GET user_info_index_2/_search
{
  "query": { 
     "term": { "userBase.csAmount" : 200.5 }
  }
}

##搜索csAmount等于200或211的数据
GET user_info_index_2/_search
{
  "query": { 
     "terms": { "userBase.csAmount" : [200,211] }
  }
}
## 查询名称等于张三的数据,以JSON格式返回
POST _xpack/sql?format=json
{
  "query":""" 
  select 
    user.csId,
    user.csName
  from user_info_index
  where user.csName= '张三' 
  """
}

## 查询名称不为空的数量,以txt方式返回
POST _xpack/sql?format=txt
{
  "query":""" 
  select 
     count(1)
  from user_info_index
  where  user.csName is not null  
  """
}


##搜索csTime时间范围内,csAmount与csSalary相等的数据
GET user_info_index_2/_search
{
    "query": {
        "bool": {
            "must": [{
              "range": {
                "userBase.csTime":{
                  "gt": "1572581133000", 
                  "lt": "1575129600000"
                }
              }
            }],
            "filter": [{
                "script": {
                    "script": {
                        "inline": "doc['userBase.csAmount'].value - doc['userBase.csSalary'].value == 0",
                        "lang": "painless"
                    }
                }
            }],
            "must_not": [],
            "should": []
        }
    },
    "from": 0,
    "size": 10
}
## 删除名称等于李四的数据
POST user_info_index_2/_delete_by_query
{
  "query":{
    "term":{
      "user.csName":"李四"
    }
  }
}

注意事项

索引结构中csAmount字段类型为数值,默认值为0
  "userBase.csAmount": {
     "type": "double",
     "null_value": 0
  },
搜索索引脚本
  GET user_info_index_2/_search
  {
    "query": { 
       "term": { "userBase.csAmount" : 0 }
    }
  }
查询结果数据空数据也会查询出来
  {
    "userBase":{
       "csAmount":null
    }
  }

扩展

DELETE user_info_index_2
POST localhost:9200/_reindex
{
  "source": {
    "index": "indexName"
  },
  "dest": {
    "index": "newIndexName"
  }
}
1.执行脚本查询索引结构
GET user_info_index_2

2.拷贝出原索引结构JSON进行编辑
1)删除mapping以上所有配置
2)删除索引名
3)删除别名
4)删除settings节点下的【provided_name,creation_date,uuid,version】

3.执行脚本创建索引
PUT user_info_index_3
{
    .... 索引的结构JSON格式
}

4.删除原索引的别名

5.为新索引创建别名

6.拷贝老索引的数据至新索引结构

7.数据补偿

上一篇下一篇

猜你喜欢

热点阅读