Elasticsearch常用语句

1.新建Mapping,相当于mysql中的字段,区别是ES已有的mapping字段建立后不允许删除和修改

 PUT /your_index/
{
  "mappings": {
    "_doc": { //默认类型为_doc,可看作mysql的表
      "properties": {
        "id": {
          "type": "keyword" //数据类型 (text、keyword、date、object、geo等) 
        },
	     "name": {
          "type": "keyword"
        }
      }
    }
  },
  "settings": {
    "index": {
      "number_of_shards": 5,
      "number_of_replicas": 1
    }
  }
}

说明:
类型默认为“_doc”,可自定义名称,在elasticsearch 7.0.0版本必须使用单index,单type,多type结构则会完全移除。
数据类型:

1.text:字符串类型,用于全文索引的字段,例如一篇文章或评论,大小写不敏感
2.long, integer, short, byte, double, float, half_float, scaled_float:数字类型
3.data:时间类型 :常用的时间类型 format 有:epoch_millis(时间戳,精确到毫秒)、epoch_second(时间戳,精确到秒)
4.boolean:布尔类型
5.binary:二进制类型
6.array:json 中的数组,里面可以包含 string、integers、array、objects,如果是 array object,里面包含的对象或数组不会被索引。
7.object:对象
8.nested:array 对象,里面的包含的对象字段会被索引。
9.keyword:精确匹配关键字,大小写敏感

number_of_shards:是指索引要做多少个分片,默认为5,只能在创建索引时指定,后期无法修改,分片类似于数据库中的分库分表,在集群中,同一个索引的分片会均匀的分布在集群的节点中。
number_of_replicas:每个主分片的副本数(备份),默认值是 1,可在后续修改
修改分片:

PUT /your_index/_settings
{
    "number_of_replicas": 1
}

refresh_interval:es刷新周期。
设置刷新周期:

PUT /your_index/_settings
{
  "refresh_interval": 1s //毫秒:ms、秒:s、分:m
} 

重置refresh_interval:(把刷新周期变为默认,即1s)

PUT /your_index/_settings
{
  "refresh_interval": null
} 

禁止刷新:

PUT /your_index/_settings
{
  "refresh_interval": -1
} 

手动刷新:

POST /your_index/_doc?refresh
{
  //写入、修改数据
} 

注意:ES是近实时刷新,如果需要写入大量数据,可以先把刷新周期禁用,写完后再刷新;如果写入日志这种实时性不是很高的数据,可以把刷新周期设置长些。

添加mapping字段(已建立好mapping)

PUT /your_index/_mapping/_doc
{
  "properties": {
    "your_name": {
          "type": "your_type"
        }
  }
}

删除索引

DELETE your_index/

2.查询语句
match查询:属于全文匹配,ElasticSearch引擎在处理全文搜索时,首先分析(analyze)查询字符串,使用索引映射中定义的分析器对字符串分词,ElasticSearch选择合适的分析器(analyzer),该analyzer和建立索引时使用的分析器相同,然后根据分词构建查询,最终返回查询结果。,

GET your_index/_search
{
  "query": {
    "match": {
      "name": "value"
    }
  }
}

短语匹配match_phrase和match_phrase_prefix:将查询条件作为一个短语进行匹配,例如:匹配“I am Hero” 这个短语,首先将查询字符串解析成一个词项列表,然后对这些词项进行搜索,但只保留那些包含 全部 搜索词项,且 位置 与搜索词项相同的文档。只会匹配所有词和顺序都一致都结果。

GET your_index/_search
{
  "query": {
    "match_phrase": {
      "name": "value"
    }
  }
}

多个短语匹配(且关系)

GET your_index/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match_phrase": {
            "remarkA": "AA"
          }
        },
        {
          "match_phrase": {
            "remarkB": "BB"
          }
        }
      ]
    }
  }
}

term和terms:term是精确查询,也就是完全匹配,通常用于对keyword和有精确值的字段进行查询,搜索前不会再对搜索词进行分词拆解。

GET your_index/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "term": {
            "name": {
              "value": "VALUE"
            }
          }
        },
        {
          "terms": {
            "name": [
              "VALUE1",
              "VALUE2"
            ]
          }
        }
      ]
    }
  }
}

must\must_not和should:且和或的区别。must:所有条件都要成立;must_not:所有条件都不能成立;should:只要有一个条件成立。

GET your_index/_search
{
  "query": {
    "bool": {
      "should": [//A或B成立
        {
          "term": {
            "A": {
              "value": "VALUE"
            }
          }
        },
        {
          "term": {
            "B": {
              "value": "VALUE"
            }
          }
        }
      ]
    }
  }
}

range查询:范围查询
1.查询时间范围

GET your_index/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "range": {
            "time": {
              "from": "2021-10-01 00:00:00",
              "to": "2021-12-31 23:59:59",
              "include_lower": true,
              "include_upper": true,
              "boost": 1
            }
          }
        }
      ]
    }
  }
}

2.数字范围查询

GET your_index/_search
{
    "query": {
        "range" : {
            "age" : {
                "gte" : 10,
                "lte" : 20,
                "boost" : 2.0
            }
        }
    }
}

3.多个范围查询

GET your_index/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "range": {
            "Time1": {
              "gte": "2021-11-12 09:53:46"
            }
          }
        },
        {
          "range": {
            "Time2": {
              "gte": "2021-11-15 10:14:55"
            }
          }
        }
      ]
    }
  }
}

模糊匹配:ES都多种方法可以支持模糊查询,比如wildcard,query_string等
wildcard:wildcard查询只针对text类型和keyword类型才会匹配

GET your_index/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "wildcard": {
            "name": "value*"
          }
        }
      ]
    }
  }
}

query_string: 是一种高级且灵活的模糊查询方式,可以多个field和多喝query值进行查询
1.

GET your_index/_search
{
  "query": {
    "bool": {
      "must": [
        {
         "query_string": {
           "default_field": "name",
           "query": "value*"//可以多个条件"(value1*) or (value2*)"
         }
        }
      ]
    }
  }
}
GET your_index/_search
{
  "query": {
    "bool": {
      "must": [
        {
         "query_string": {
           "fields": ["content", "name"],//查询content和name字段中都包含value前缀的数据
           "query": "value*"
         }
        }
      ]
    }
  }
}

3.排序与自定义返回字段

GET your_index/_search
{
  "query": {
    "match_all": {}
  },
  "sort": [
    {
      "createTime": {
        "order": "desc"//字段升序或降序
      }
    }
  ],
  "_source": "{field1,field2}"//只返回这里定义好的字段值
}

4.根据查询修改数据
1.把字段name=张三的值改为李四

POST your_index/_update_by_query
{
  "script": {
    "source": "ctx._source['name']='李四'"
  },
  "query": {
    "match": {
      "id": "111"
    }
  }
}

2.删除文档中字段name

POST your_index/_update_by_query
{
  "script": {
    "source": "ctx._source.remove(\"name\")",
    "lang": "painless"
  },
 "query": {
    "bool": {
      "must": [
        {
          "term": {
            "id": "111",
            "boost": 1
          }
        }
      ],
      "adjust_pure_negative": true,
      "boost": 1
    }
  }
}

更新二级字段

POST your_index/_update_by_query
{
  "script": {
     "source": "ctx._source.location = params.location",
    "params": {
      "location": {
          "lat": "22.531514",
          "lon":  "113.954506"
      }
    }
  },
  "query": {
    "match": {
      "id": "28637"
    }
  }
}
//或者
POST twitter/_update_by_query
{
  "query": {
    "match": {
      "id": "28637"
    }
  }"script": {
    "source": "ctx._source.location.lon = params.location.lon",
    "params": {
      "location": {
          "lat": "39.970788",
          "lon":  "119.970718"
      }
    }
  }
}
//或者
POST crm_shop_public/_update_by_query
{
  "script": {
    "source": "ctx._source.glat = 22.531513;ctx._source.glng = 113.954507"
  },
  "query": {
    "match": {
      "id": "28637"
    }
  }
}

5.根据查询删除数据

POST your_index/_delete_by_query
{
  "query": {
    "bool": {
      "must": [
        {
          "term": {
            "id": "111",
            "boost": 1
          }
        }
      ]
    }
  }
}
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Elasticsearch是一个开源的分布式搜索和分析引擎,它提供了丰富的查询语句来进行数据检索和分析。下面是一些常用Elasticsearch查询语句的介绍: 1. Match查询:用于执行全文搜索,它会将查询字符串与指定字段进行匹配。例如: ``` GET /index/_search { "query": { "match": { "field": "query_string" } } } ``` 2. Term查询:用于精确匹配某个字段的值。例如: ``` GET /index/_search { "query": { "term": { "field": "value" } } } ``` 3. Range查询:用于匹配指定范围内的值。例如: ``` GET /index/_search { "query": { "range": { "field": { "gte": "start_value", "lte": "end_value" } } } } ``` 4. Bool查询:用于组合多个查询条件,支持must、must_not、should等逻辑操作符。例如: ``` GET /index/_search { "query": { "bool": { "must": [ { "match": { "field1": "value1" } }, { "match": { "field2": "value2" } } ], "must_not": [ { "term": { "field3": "value3" } } ], "should": [ { "term": { "field4": "value4" } } ] } } } ``` 5. Aggregation聚合查询:用于对数据进行分组和统计分析。例如: ``` GET /index/_search { "aggs": { "group_by_field": { "terms": { "field": "field" }, "aggs": { "stats": { "stats": { "field": "numeric_field" } } } } } } ``` 这些只是Elasticsearch查询语句的一小部分,Elasticsearch还提供了更多的查询语句和功能,如模糊查询、通配符查询、正则表达式查询、地理位置查询等。你可以根据具体的需求选择合适的查询语句来进行数据检索和分析。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值