ElasticSearch基础入门四

查询与聚合API

query DSL

filter DSL

full-text查询或查询,结果依赖于相关度分值

精确值(extac-value)查询或查询结果仅有“yes”或“no”两种结果

重量级的查询、不会被缓存

计算及过滤速度较快,且适于缓存

match_all、match 、match_phrase及bool Query

term Filter、terms Filter、range Filter、exists and missing Filters和bool Filter。

 

1、增删改

1.1、添加

①添加或更新 :指定ID

JSON文档插入ID为1的“index”索引

PUT index/_doc/1

{

    "user" : "kimchy",

    "post_date" : "2009-11-15T14:12:12",

    "message" : "trying out Elasticsearch"

}

添加文档: 索引id自动生成

POST index/_doc/

{

    "user" : "kimchy",

    "post_date" : "2009-11-15T14:12:12",

    "message" : "trying out Elasticsearch"

}

③添加文档  直接指定分片:“kimchy”

POST index/_doc?routing=kimchy

{

    "user" : "kimchy",

    "post_date" : "2009-11-15T14:12:12",

    "message" : "trying out Elasticsearch"

}

1.2、更新 

指定更新的文档索引,类型及文档ID即可,更新成功Version增加

PUT index/_doc/1

{

    "user" : "kimchy",

    "post_date" : "2019-11-15 14:12:12",

    "message" : "trying out Elasticsearch"

}

 

1.3、删除

1、删除数据

①、根据主键删除数据:【DELETE  /索引名称/类型名称/主键编号】

Delete /索引名称/类型名称/主键编号

 

②、根据匹配条件删除数据:(注意请求方式是Post)

POST /索引名称/类型名称/_delete_by_query   

{"query":{"term":{"_id":100000100}}}

 

③、删除所有数据:(PSOT请求,只删除数据,不删除表结构)

POST /index/type/_delete_by_query?pretty

{ "query": {"match_all": {}}}

 

④、指定分片删除

POST index/type/_delete_by_query?routing=1

{"query":{"range":{"age":{"gte":10}}}}

 

⑤、批量删除数据大小

POST twitter/_delete_by_query?scroll_size=5000

{ "query": {"term": {"user": "kimchy"}}}

 

2、删除索引

①、删除单个索引可以使用命令 【DELETE /索引名称】

Delete 索引名称

 

②、删除多个索引可以使用命令 【DELETE /索引1,索引2】

Delete 索引名称1,索引名称2

 

③、删除指定索引开头的所有索引文件【DELETE /testindex*】 [可在配置中禁止];

Delete 索引名称*

 

④、删除全部索引命令 【DELETE /_all 或者 DELETE /*】[可在配置中禁止]

Delete _all

注意:单个命令来删除所有的数据不安全,所以,可在elasticsearch.yml 配置文件中修改 action.destructive_requires_name: true

 

2、URL搜索

例:查name=lisi

GET /index/type/_search?q=name:lisi

 

查询interests=changge 对age进行降序排列

GET /index/type/_search?q=interests:changge&sort=age:desc

 

3、多索引,多类型

例:GET /Index1,index2/type1,type2/_search?q=tag:wow

 

/_search

在所有的索引中搜索所有的类型

 

/index/_search

在 index 索引中搜索所有的类型

 

/index,index2/_search

在 index和index2 索引中搜索所有的文档

 

通配符 *

/g*,u*/_search

在任何以 g 或者 u 开头的索引中搜索所有的类型

 

//*号为通配符,查询结尾是3和4的索引下的所有文档

GET /*3,*4/_search

 

/index/type1/_search

在 index 索引中搜索 type1 类型

 

/index,index2/type1,type2/_search

在 index 和 index2 索引中搜索 type1 和 type2 类型

 

/_all/type1,type2/_search

在所有的索引中搜索 type1 和 type2 类型

 

3term 和terms精确查询

{"query": {"term": {"user": {"value": "Kimchy",}}}}

{"query": {"terms": {"user" : ["kimchy", "elasticsearch"],}}}}

 

4、布尔查询Bool Query

一个 bool 过滤器由三部分组成:

{"bool":{"must":[],"should":[],"must_not":[],}}

Must:所有的语句都 必须(must) 匹配,与 AND 等价。

must_not:所有的语句都 不能(must not) 匹配,与 NOT 等价。

should:至少有一个语句要匹配,与 OR 等价。

 

5、Range查询

{"query": {"range" : {"age" : {"gte" : 10,"lte" : 20,}}}}

gt: > 大于(greater than)

lt: < 小于(less than)

gte: >= 大于或等于(greater than or equal to)

lte: <= 小于或等于(less than or equal to)

 

6Exists 存在查询

{ "query" :{ "exists" :{ "field" :“user" } } }

 

7、Missing 查询

{ "query" :{ "missing" :{ "field" :“user" } } }

 

8 前缀查询

{ "query": {"prefix" : { "user" : "ki" }}}

 

9、 Wildcard Query通配符检索

通配符查询允许您指定匹配的模式,而不是整个词组(term)检索

?匹配任何字符

*匹配零个或多个字符

{"query": {"wildcard": {"user": {"value": "ki*y"}}}}

 

10、正则表达式检索( Regexp Query)

{"query": {"regexp":{"user": " t[a-z]*y "}}}

 

11、Fuzzy 模糊检索( Fuzzy Queries)

{ "query" :{ "fuzzy" :{ "user" :"ki" } } }

 

12match 模糊查询

1、match查询 

match查询 全文查询 模糊查询,先分词再查询text / numerics / dates

{"query":{"match":{"key":"A,B"}}}

 

match_all 匹配所有文档

match_none match_all的反面,不匹配任何文档

match_phrase短语匹配

{

    "query": {

        "match_phrase" : {

            "message" : "this is a test"

        }

    }

}

 

2、match前缀查询

match_phrase_prefix

 

match_bool_prefix

GET /_search

{

    "query": {

        "match_bool_prefix" : {

            "message" : "quick brown f"

        }

    }

}

类似

GET /_search

{

    "query": {

        "bool" : {

            "should": [

                { "term": { "message": "quick" }},

                { "term": { "message": "brown" }},

                { "prefix": { "message": "f"}}

            ]

        }

    }

}

 

multi_match 在指定字段查询指定的值 的查询

查询title,first_name和last_name 字段

{

  "query": {

    "multi_match" : {

      "query":    "Will Smith",

      "fields": [ "title", "*_name" ]

    }

  }

}

 

13、全字段模糊查询(query_string

query_string查询解析输入并在运算符周围分割文本。每个文本部分彼此独立地分析。

例如:

GET /_search

{

    "query": {

        "query_string" : {

            "default_field" : "content",

            "query" : "(new york city) OR (big apple)"

        }

    }

}

将分成new york city 或 big apple,然后通过为该字段配置的分析器独立地分析每个部分。

如果要分别查询每个术语,则需要在术语周围添加显式运算符(如new AND york AND city)

 

14聚合

1、指标聚合

对一个数据集求最大、最小、和、平均值等指标的聚合,在ES中称为指标聚合metric

max min sum avg

 

文档计数 count

POST /bank/_doc/_count

{ "query": {"match": {"age" : 24}}}

 

value_count Value count 统计某字段有值的文档数

POST /bank/_search?size=0

{

  "aggs": {

    "age_count": {

      "value_count": {

        "field": "age"

      }

    }

  }

}

 

cardinality  值去重计数

POST /bank/_search?size=0

{

  "aggs": {

    "age_count": {

      "cardinality": {

        "field": "age"

      }

    },

    "state_count": {

      "cardinality": {

        "field": "state.keyword"

      }

    }

  }

}

 

2、桶聚合

对查询出的数据进行分组group by,再在组上进行指标聚合,分组在ES中称为分桶、 桶聚合 bucketing

 

Terms 根据字段值项分组聚合

Filter   对满足过滤查询的文档进行聚合计算

Filters 多个过滤组聚合计算

Range 范围分组聚合

Date Range 时间范围分组聚合

Histogram 年、季、月、 周、 日、 时 、分 、秒等进行聚合统计

Missing 缺失聚合

GEO Distance 地理位置聚合

 

15、根据条件更新历史数据

POST my_index/_update_by_query

{

"query": {"term": {"user": {"value": "Kimchy",}}},

"script": {

     "inline": "if (ctx._source.code == null) {ctx._source.code= '02'}"

   }

}

 

16、分页查询

1、深度分页from-size

{

"from" : 0, "size" : 10,

"query" : {

"term" : { "user" : "kimchy" }

}

}

其中:from定义了目标数据的偏移值,size定义当前返回的事件数目。

默认:from为0,size为10

注意:ES使用index.max_result_window:10000作为保护措施 ,即默认 from + size 不能超过10000 

 

2、 快照分页(scroll)

POST /index/_search?scroll=1m

{

    "size": 100,

    "query": {

        "match" : {

            "title" : "elasticsearch"

        }

    }

}

返回包含 _scroll_id

 

根据scroll_id 查询数据

POST /_search/scroll

{

    "scroll" : "1m",

    "scroll_id" : "DXF1ZXJ5QW5kRmV0….."

}

 

scroll:指分页保持活动多久

 

优化:

①初始化,使用_doc去sort,适用只取数据不排序,

GET /_search?scroll=1m  {"sort": ["_doc"]}

②清除scroll,查询完成后关闭,释放资源

DELETE /_search/scroll  { "scroll_id":"12121212"}

DELETE /_search/scroll/_all

DELETE /_search/scroll/{scroll_id}

 

3分页展示最大条数设置

put /index/_settings

{ "index" : { "max_result_window" : 10000000}}

 

posted @ 2020-06-01 16:27  阿陌i  阅读(164)  评论(0编辑  收藏  举报