ELK查询命令详解

[Elasticsearch: 权威指南] https://www.elastic.co/guide/cn/elasticsearch/guide/current/search-in-depth.html

倒排索引

Elasticsearch 使用一种称为 倒排索引 的结构,它适用于快速的全文搜索。一个倒排索引由文档中所有不重复词的列表构成,对于其中每个词,有一个包含它的文档列表。

示例:

  1. 假设文档集合包含五个文档,每个文档内容如图所示,在图中最左端一栏是每个文档对应的文档编号。我们的任务就是对这个文档集合建立倒排索引。

  2. 中文和英文等语言不同,单词之间没有明确分隔符号,所以首先要用分词系统将文档自动切分成单词序列。这样每个文档就转换为由单词序列构成的数据流,为了系统后续处理方便,需要对每个不同的单词赋予唯一的单词编号,同时记录下哪些文档包含这个单词,在如此处理结束后,我们可以得到最简单的倒排索引

    “单词ID”一栏记录了每个单词的单词编号,第二栏是对应的单词,第三栏即每个单词对应的倒排列表

  3. 索引系统还可以记录除此之外的更多信息,下图还记载了单词频率信息(TF)即这个单词在某个文档中的出现次数,之所以要记录这个信息,是因为词频信息在搜索结果排序时,计算查询和文档相似度是很重要的一个计算因子,所以将其记录在倒排列表中,以方便后续排序时进行分值计算。

  4. 倒排列表中还可以记录单词在某个文档出现的位置信息.

有了这个索引系统,搜索引擎可以很方便地响应用户的查询,比如用户输入查询词“Facebook”,搜索系统查找倒排索引,从中可以读出包含这个单词的文档,这些文档就是提供给用户的搜索结果,而利用单词频率信息、文档频率信息即可以对这些候选搜索结果进行排序,计算文档和查询的相似性,按照相似性得分由高到低排序输出,此即为搜索系统的部分内部流程。

倒排索引原理

示例文本:

  1. 1.The quick brown fox jumped over the lazy dog
  2. 2.Quick brown foxes leap over lazy dogs in summer

倒排索引:

  1. Term Doc_1 Doc_2
  2. Quick | | X
  3. The | X |
  4. brown | X | X
  5. dog | X |
  6. dogs | | X
  7. fox | X |
  8. foxes | | X
  9. in | | X
  10. jumped | X |
  11. lazy | X | X
  12. leap | | X
  13. over | X | X
  14. quick | X |
  15. summer | | X
  16. the | X |

搜索quick brown :

  1. Term Doc_1 Doc_2
  2. brown | X | X
  3. quick | X |
  4. Total | 2 | 1

计算相关度分数时,文档1的匹配度高,分数会比文档2高.

问题:

  1. Quick 和 quick 以独立的词条出现,然而用户可能认为它们是相同的词。
  2. fox 和 foxes 非常相似, 就像 dog 和 dogs ;他们有相同的词根。
  3. jumped 和 leap, 尽管没有相同的词根,但他们的意思很相近。他们是同义词。
  4. 搜索含有 Quick fox的文档是搜索不到的

使用标准化规则(normalization):

建立倒排索引的时候,会对拆分出的各个单词进行相应的处理,以提升后面搜索的时候能够搜索到相关联的文档的概率

  1. Term Doc_1 Doc_2
  2. brown | X | X
  3. dog | X | X
  4. fox | X | X
  5. in | | X
  6. jump | X | X
  7. lazy | X | X
  8. over | X | X
  9. quick | X | X
  10. summer | | X
  11. the | X | X

分词器介绍及内置分词器

分词器:从一串文本中切分出一个一个的词条,并对每个词条进行标准化

包括三部分:

  • character filter:分词之前的预处理,过滤掉HTML标签,特殊符号转换等
  • tokenizer: 分词
  • token filter:标准化

内置分词器:

  • standard 分词器:(默认的)他会将词汇单元转换成小写形式,并去除停用词和标点符号,支持中文采用的方法为单字切分
  • simple 分词器:首先会通过非字母字符来分割文本信息,然后将词汇单元统一为小写形式。该分析器会去掉数字类型的字符。
  • Whitespace 分词器:仅仅是去除空格,对字符没有lowcase化,不支持中文;并且不对生成的词汇单元进行其他的标准化处理。
  • language 分词器:特定语言的分词器,不支持中文

使用ElasticSearch API 实现CRUD

添加索引:

  1. PUT /lib/
  2. {
  3. "settings":{
  4. "index":{
  5. "number_of_shards": 5,
  6. "number_of_replicas": 1
  7. }
  8. }
  9. }

查看索引信息:

  1. GET /lib/_settings
  2. GET _all/_settings

添加文档:

  1. PUT /lib/user/1
  2. {
  3. "first_name" : "jane",
  4. "last_name" : "Smith",
  5. "age" : 32,
  6. "about" : "I like to collect rock albums",
  7. "interests": [ "music" ]
  8. }

更新文档:将前面的年龄更新为22岁

  1. POST /lib/user/1
  2. {
  3. "first_name" : "jane",
  4. "last_name" : "Smith",
  5. "age" : 22,
  6. "about" : "I like to collect rock albums",
  7. "interests": [ "music" ]
  8. }

查看文档:

  1. GET /lib/user/1

命令返回

  1. #! Deprecation: [types removal] Specifying types in document get requests is deprecated, use the /{index}/_doc/{id} endpoint instead.
  2. {
  3. "_index" : "lib",
  4. "_type" : "user",
  5. "_id" : "1",
  6. "_version" : 9,
  7. "_seq_no" : 9,
  8. "_primary_term" : 1,
  9. "found" : true,
  10. "_source" : {
  11. "first_name" : "jane",
  12. "last_name" : "Smith",
  13. "age" : 22,
  14. "about" : "I like to collect rock albums",
  15. "interests" : [
  16. "music"
  17. ]
  18. }
  19. }
  20. GET /lib/user/1?_source=age,interests

覆盖文档:

  1. PUT /lib/user/1
  2. {
  3. "first_name" : "Jane",
  4. "last_name" : "Smith",
  5. "age" : 36,
  6. "about" : "I like to collect rock albums",
  7. "interests": [ "music" ]
  8. }

更新文档:

  1. POST /lib/user/1/_update
  2. {
  3. "doc":{
  4. "age":33
  5. }
  6. }

删除一个文档

  1. DELETE /lib/user/1

删除一个索引

  1. DELETE /lib

批量获取文档

使用es提供的Multi Get API:使用Multi Get API可以通过索引名、类型名、文档id一次得到一个文档集合,文档可以来自同一个索引库,也可以来自不同索引库.

使用curl命令:

  1. curl 'http://192.168.25.131:9200/_mget' -d '{
  2. "docs":[
  3. {
  4. "_index": "lib",
  5. "_type": "user",
  6. "_id": 1
  7. },
  8. {
  9. "_index": "lib",
  10. "_type": "user",
  11. "_id": 2
  12. }
  13. ]
  14. }'

在客户端工具中:

  1. GET /_mget
  2. {
  3. "docs":[
  4. {
  5. "_index": "lib",
  6. "_type": "user",
  7. "_id": 1
  8. },
  9. {
  10. "_index": "lib",
  11. "_type": "user",
  12. "_id": 2
  13. },
  14. {
  15. "_index": "lib",
  16. "_type": "user",
  17. "_id": 3
  18. }
  19. ]
  20. }

可以指定具体的字段:

  1. GET /_mget
  2. {
  3. "docs":[
  4. {
  5. "_index": "lib",
  6. "_type": "user",
  7. "_id": 1,
  8. "_source": "interests"
  9. },
  10. {
  11. "_index": "lib",
  12. "_type": "user",
  13. "_id": 2,
  14. "_source": ["age","interests"]
  15. }
  16. ]
  17. }

获取同索引同类型下的不同文档:

  1. GET /lib/user/_mget
  2. {
  3. "docs":[
  4. {
  5. "_id": 1
  6. },
  7. {
  8. "_type": "user",
  9. "_id": 2,
  10. }
  11. ]
  12. }
  13. GET /lib/user/_mget
  14. {
  15. "ids": ["1","2"]
  16. }

使用Bulk API 实现批量操作

bulk的格式:

  1. {action:{metadata}}
  2. {requstbody}
  3. action:(行为)
  4. - create:文档不存在时创建
  5. - update:更新文档
  6. - index:创建新文档或替换已有文档
  7. - delete:删除一个文档
  8. - metadata_index,_type,_id

create 和index的区别

如果数据存在,使用create操作失败,会提示文档已经存在,使用index则可以成功执行。

示例:

  1. {"delete":{"_index":"lib","_type":"user","_id":"1"}}

批量添加:

  1. POST /lib2/books/_bulk
  2. {"index":{"_id":1}}
  3. {"title":"Java","price":55}
  4. {"index":{"_id":2}}
  5. {"title":"Html5","price":45}
  6. {"index":{"_id":3}}
  7. {"title":"Php","price":35}
  8. {"index":{"_id":4}}
  9. {"title":"Python","price":50}

批量获取:

  1. GET /lib2/books/_mget
  2. {
  3. "ids": ["1","2","3","4"]
  4. }

删除:没有请求体

  1. POST /lib2/books/_bulk
  2. {"delete":{"_index":"lib2","_type":"books","_id":4}}
  3. {"create":{"_index":"tt","_type":"ttt","_id":"100"}}
  4. {"name":"lisi"}
  5. {"index":{"_index":"tt","_type":"ttt"}}
  6. {"name":"zhaosi"}
  7. {"update":{"_index":"lib2","_type":"books","_id":"4"}}
  8. {"doc":{"price":58}}

bulk一次最大处理多少数据量: bulk会把将要处理的数据载入内存中,所以数据量是有限制的,最佳的数据量不是一个确定的数值,它取决于你的硬件,你的文档大小以及复杂性,你的索引以及搜索的负载。

一般建议是1000-5000个文档,大小建议是5-15MB,默认不能超过100M,可以在es的配置文件(即$ES_HOME下的config下的elasticsearch.yml)中。

  

版本控制

ElasticSearch采用了乐观锁来保证数据的一致性,也就是说,当用户对document进行操作时,并不需要对该document作加锁和解锁的操作,只需要指定要操作的版本即可。当版本号一致时,ElasticSearch会允许该操作顺利执行,而当版本号存在冲突时,ElasticSearch会提示冲突并抛出异常(VersionConflictEngineException异常)。

  1. ElasticSearch的版本号的取值范围为12^63-1
  2. 内部版本控制:使用的是_version
  3. 外部版本控制:elasticsearch在处理外部版本号时会与对内部版本号的处理有些不同。它不再是检查_version是否与请求中指定的数值_相同_,而是检查当前的_version是否比指定的数值小。如果请求成功,那么外部的版本号就会被存储到文档中的_version中。
  4. 为了保持_version与外部版本控制的数据一致
  5. 使用version_type=external

什么是Mapping?

  1. PUT /myindex/article/1
  2. {
  3. "post_date": "2018-05-10",
  4. "title": "Java",
  5. "content": "java is the best language",
  6. "author_id": 119
  7. }
  8. PUT /myindex/article/2
  9. {
  10. "post_date": "2018-05-12",
  11. "title": "html",
  12. "content": "I like html",
  13. "author_id": 120
  14. }
  15. PUT /myindex/article/3
  16. {
  17. "post_date": "2018-05-16",
  18. "title": "es",
  19. "content": "Es is distributed document store",
  20. "author_id": 110
  21. }
  22. GET /myindex/article/_search?q=2018-05
  23. GET /myindex/article/_search?q=2018-05-10
  24. GET /myindex/article/_search?q=html
  25. GET /myindex/article/_search?q=java

查看es自动创建的mapping

  1. GET /myindex/article/_mapping

es自动创建了index,type,以及type对应的mapping(dynamic mapping).

什么是映射:mapping定义了type中的每个字段的数据类型以及这些字段如何分词等相关属性

  1. {
  2. "myindex": {
  3. "mappings": {
  4. "article": {
  5. "properties": {
  6. "author_id": {
  7. "type": "long"
  8. },
  9. "content": {
  10. "type": "text",
  11. "fields": {
  12. "keyword": {
  13. "type": "keyword",
  14. "ignore_above": 256
  15. }
  16. }
  17. },
  18. "post_date": {
  19. "type": "date"
  20. },
  21. "title": {
  22. "type": "text",
  23. "fields": {
  24. "keyword": {
  25. "type": "keyword",
  26. "ignore_above": 256
  27. }
  28. }
  29. }
  30. }
  31. }
  32. }
  33. }
  34. }

创建索引的时候,可以预先定义字段的类型以及相关属性,这样就能够把日期字段处理成日期,把数字字段处理成数字,把字符串字段处理字符串值等.

支持的数据类型:

(1) 核心数据类型(Core datatypes)

  1. 字符型:stringstring类型包括text keyword.
  2. text类型被用来索引长文本,在建立索引前会将这些文本进行分词,转化为词的组合,建立索引。允许es来检索这些词语。text类型不能用来排序和聚合。
  3. Keyword类型不需要进行分词,可以被用来检索过滤、排序和聚合。keyword 类型字段只能用本身来进行检索
  4. 数字型:long, integer, short, byte, double, float
  5. 日期型:date
  6. 布尔型:boolean
  7. 二进制型:binary

(2) 复杂数据类型(Complex datatypes)

  1. 数组类型(Array datatype):数组类型不需要专门指定数组元素的type,例如:
  2. 字符型数组: [ "one", "two" ]
  3. 整型数组:[ 1, 2 ]
  4. 数组型数组:[ 1, [ 2, 3 ]] 等价于[ 1, 2, 3 ]
  5. 对象数组:[ { "name": "Mary", "age": 12 }, { "name": "John", "age": 10 }]
  6. 对象类型(Object datatype):_ object _ 用于单个JSON对象
  7. 嵌套类型(Nested datatype):_ nested _ 用于JSON数组

(3) 地理位置类型(Geo datatypes)

  1. 地理坐标类型(Geo-point datatype):_ geo_point _ 用于经纬度坐标;
  2. 地理形状类型(Geo-Shape datatype):_ geo_shape _ 用于类似于多边形的复杂形状;

(4) 特定类型(Specialised datatypes)

  1. IPv4 类型(IPv4 datatype):_ ip _ 用于IPv4 地址;
  2. Completion 类型(Completion datatype):_ completion *提供自动补全建议;
  3. Token count 类型(Token count datatype):* token_count _ 用于统计做了标记的字段的index数目,该值会一直增加,不会因为过滤条件而减少。

mapper-murmur3 插件

类型:通过插件,可以通过 _ murmur3 _ 来计算 index 的 hash 值;

附加类型(Attachment datatype):采用 mapper-attachments

插件,可支持_ attachments _ 索引,例如 Microsoft Office 格式,Open Document 格式,ePub, HTML 等。

  1. "store":false //是否单独设置此字段的是否存储而从_source字段中分离,默认是false,只能搜索,不能获取值
  2. "index": true //分词,不分词是:false,设置成false,字段将不会被索引
  3. "analyzer":"ik" //指定分词器,默认分词器为standard analyzer
  4. "boost":1.23 //字段级别的分数加权,默认值是1.0
  5. "doc_values":false //对not_analyzed字段,默认都是开启,分词字段不能使用,对排序和聚合能提升较大性能,节约内存
  6. "fielddata":{"format":"disabled"} //针对分词字段,参与排序或聚合时能提高性能,不分词字段统一建议使用doc_value
  7. "fields":{"raw":{"type":"string","index":"not_analyzed"}} //可以对一个字段提供多种索引模式,同一个字段的值,一个分词,一个不分词
  8. "ignore_above":100 //超过100个字符的文本,将会被忽略,不被索引
  9. "include_in_all":ture //设置是否此字段包含在_all字段中,默认是true,除非index设置成no选项
  10. "index_options":"docs" //4个可选参数docs(索引文档号) ,freqs(文档号+词频),positions(文档号+词频+位置,通常用来距离查询),offsets(文档号+词频+位置+偏移量,通常被使用在高亮字段)分词字段默认是position,其他的默认是docs
  11. "norms":{"enable":true,"loading":"lazy"} //分词字段默认配置,不分词字段:默认{"enable":false},存储长度因子和索引时boost,建议对需要参与评分字段使用 ,会额外增加内存消耗量
  12. "null_value":"NULL" //设置一些缺失字段的初始化值,只有string可以使用,分词字段的null值也会被分词
  13. "position_increament_gap":0 //影响距离查询或近似查询,可以设置在多值字段的数据上火分词字段上,查询时可指定slop间隔,默认值是100
  14. "search_analyzer":"ik" //设置搜索时的分词器,默认跟ananlyzer是一致的,比如index时用standard+ngram,搜索时用standard用来完成自动提示功能
  15. "similarity":"BM25" //默认是TF/IDF算法,指定一个字段评分策略,仅仅对字符串型和分词类型有效
  16. "term_vector":"no" //默认不存储向量信息,支持参数yes(term存储),with_positions(term+位置),with_offsets(term+偏移量),with_positions_offsets(term+位置+偏移量) 对快速高亮fast vector highlighter能提升性能,但开启又会加大索引体积,不适合大数据量用.

映射的分类:

(1) 动态映射:

当ES在文档中碰到一个以前没见过的字段时,它会利用动态映射来决定该字段的类型,并自动地对该字段添加映射。

  1. 可以通过dynamic设置来控制这一行为,它能够接受以下的选项:
  2. true:默认值。动态添加字段
  3. false:忽略新字段
  4. strict:如果碰到陌生字段,抛出异常
  5. dynamic设置可以适用在根对象上或者object类型的任意字段上。

给索引lib2创建映射类型

  1. POST /lib2
  2. {
  3. "settings":{
  4. "number_of_shards" : 3,
  5. "number_of_replicas" : 0
  6. },
  7. "mappings":{
  8. "books":{
  9. "properties":{
  10. "title":{"type":"text"},
  11. "name":{"type":"text","index":false},
  12. "publish_date":{"type":"date","index":false},
  13. "price":{"type":"double"},
  14. "number":{"type":"integer"}
  15. }
  16. }
  17. }
  18. }

给索引lib2创建映射类型

  1. POST /lib2
  2. {
  3. "settings":{
  4. "number_of_shards" : 3,
  5. "number_of_replicas" : 0
  6. },
  7. "mappings":{
  8. "books":{
  9. "properties":{
  10. "title":{"type":"text"},
  11. "name":{"type":"text","index":false},
  12. "publish_date":{"type":"date","index":false},
  13. "price":{"type":"double"},
  14. "number":{
  15. "type":"object",
  16. "dynamic":true
  17. }
  18. }
  19. }
  20. }
  21. }

基本查询(Query查询)

数据准备

  1. PUT /lib3
  2. {
  3. "settings":{
  4. "number_of_shards" : 3,
  5. "number_of_replicas" : 0
  6. },
  7. "mappings":{
  8. "user":{
  9. "properties":{
  10. "name": {"type":"text"},
  11. "address": {"type":"text"},
  12. "age": {"type":"integer"},
  13. "interests": {"type":"text"},
  14. "birthday": {"type":"date"}
  15. }
  16. }
  17. }
  18. }
  19. GET /lib3/user/_search?q=name:lisi
  20. GET /lib3/user/_search?q=name:zhaoliu&sort=age:desc

term查询和terms查询

term query会去倒排索引中寻找确切的term,它并不知道分词器的存在。这种查询适合keyword 、numeric、date。

term: 查询某个字段里含有某个关键词的文档

  1. GET /lib3/user/_search/
  2. {
  3. "query": {
  4. "term": {"interests": "changge"}
  5. }
  6. }

terms: 查询某个字段里含有多个关键词的文档

  1. GET /lib3/user/_search
  2. {
  3. "query":{
  4. "terms":{
  5. "interests": ["hejiu","changge"]
  6. }
  7. }
  8. }

控制查询返回的数量

from:从哪一个文档开始

size:需要的个数

  1. GET /lib3/user/_search
  2. {
  3. "from":0,
  4. "size":2,
  5. "query":{
  6. "terms":{
  7. "interests": ["hejiu","changge"]
  8. }
  9. }
  10. }

返回版本号

  1. GET /lib3/user/_search
  2. {
  3. "version":true,
  4. "query":{
  5. "terms":{
  6. "interests": ["hejiu","changge"]
  7. }
  8. }
  9. }

match查询

match query 知道分词器的存在,会对filed进行分词操作,然后再查询

  1. GET /lib3/user/_search
  2. {
  3. "query":{
  4. "match":{
  5. "name": "zhaoliu"
  6. }
  7. }
  8. }
  9. GET /lib3/user/_search
  10. {
  11. "query":{
  12. "match":{
  13. "age": 20
  14. }
  15. }
  16. }

match_all:查询所有文档

  1. GET /lib3/user/_search
  2. {
  3. "query": {
  4. "match_all": {}
  5. }
  6. }

multi_match:可以指定多个字段

  1. GET /lib3/user/_search
  2. {
  3. "query":{
  4. "multi_match": {
  5. "query": "lvyou",
  6. "fields": ["interests","name"]
  7. }
  8. }
  9. }

match_phrase:短语匹配查询

ElasticSearch引擎首先分析(analyze)查询字符串,从分析后的文本中构建短语查询,这意味着必须匹配短语中的所有分词,并且保证各个分词的相对位置不变:

  1. GET lib3/user/_search
  2. {
  3. "query":{
  4. "match_phrase":{
  5. "interests": "duanlian,shuoxiangsheng"
  6. }
  7. }
  8. }

指定返回的字段

  1. GET /lib3/user/\_search
  2. {
  3. "_source": ["address","name"],
  4. "query": {
  5. "match": {
  6. "interests": "changge"
  7. }
  8. }
  9. }

控制加载的字段

  1. GET /lib3/user/_search
  2. {
  3. "query": {
  4. "match_all": {}
  5. },
  6. "_source": {
  7. "includes": ["name","address"],
  8. "excludes": ["age","birthday"]
  9. }
  10. }

使用通配符 *

  1. GET /lib3/user/_search
  2. {
  3. "_source": {
  4. "includes": "addr\*",
  5. "excludes": ["name","bir*"]
  6. },
  7. "query": {
  8. "match_all": {}
  9. }
  10. }

排序

使用sort实现排序:

desc:降序,asc升序

  1. GET /lib3/user/_search
  2. {
  3. "query": {
  4. "match_all": {}
  5. },
  6. "sort": [
  7. {
  8. "age": {
  9. "order":"asc"
  10. }
  11. }
  12. ]
  13. }
  1. GET /lib3/user/_search
  2. {
  3. "query": {
  4. "match_all": {}
  5. },
  6. "sort": [
  7. {
  8. "age": {
  9. "order":"desc"
  10. }
  11. }
  12. ]
  13. }

前缀匹配查询

  1. GET /lib3/user/_search
  2. {
  3. "query": {
  4. "match_phrase_prefix": {
  5. "name": {
  6. "query": "zhao"
  7. }
  8. }
  9. }
  10. }

范围查询

range:实现范围查询

参数:from,to,include_lower,include_upper,boost

include_lower:是否包含范围的左边界,默认是true

include_upper:是否包含范围的右边界,默认是true

  1. GET /lib3/user/_search
  2. {
  3. "query": {
  4. "range": {
  5. "birthday": {
  6. "from": "1990-10-10",
  7. "to": "2018-05-01"
  8. }
  9. }
  10. }
  11. }
  12. GET /lib3/user/_search
  13. {
  14. "query": {
  15. "range": {
  16. "age": {
  17. "from": 20,
  18. "to": 25,
  19. "include_lower": true,
  20. "include_upper": false
  21. }
  22. }
  23. }
  24. }

wildcard查询

允许使用通配符 *?来进行查询

* 代表0个或多个字符

代表任意一个字符

  1. GET /lib3/user/_search
  2. {
  3. "query": {
  4. "wildcard": {
  5. "name": "zhao*"
  6. }
  7. }
  8. }
  9. GET /lib3/user/_search
  10. {
  11. "query": {
  12. "wildcard": {
  13. "name": "li?i"
  14. }
  15. }
  16. }

fuzzy实现模糊查询

value:查询的关键字

boost:查询的权值,默认值是1.0

min_similarity:设置匹配的最小相似度,默认值为0.5,对于字符串,取值为0-1(包括0和1);对于数值,取值可能大于1;对于日期型取值为1d,1m等,1d就代表1天

prefix_length:指明区分词项的共同前缀长度,默认是0

max_expansions:查询中的词项可以扩展的数目,默认可以无限大

  1. GET /lib3/user/_search
  2. {
  3. "query": {
  4. "fuzzy": {
  5. "interests": "chagge"
  6. }
  7. }
  8. }
  9. GET /lib3/user/_search
  10. {
  11. "query": {
  12. "fuzzy": {
  13. "interests": {
  14. "value": "chagge"
  15. }
  16. }
  17. }
  18. }

高亮搜索结果

  1. GET /lib3/user/_search
  2. {
  3. "query":{
  4. "match":{
  5. "interests": "changge"
  6. }
  7. },
  8. "highlight": {
  9. "fields": {
  10. "interests": {}
  11. }
  12. }
  13. }

Filter查询

filter是不计算相关性的,同时可以cache。因此,filter速度要快于query。

  1. POST /lib4/items/_bulk
  2. {"index": {"_id": 1}}
  3. {"price": 40,"itemID": "ID100123"}
  4. {"index": {"_id": 2}}
  5. {"price": 50,"itemID": "ID100124"}
  6. {"index": {"_id": 3}}
  7. {"price": 25,"itemID": "ID100124"}
  8. {"index": {"_id": 4}}
  9. {"price": 30,"itemID": "ID100125"}
  10. {"index": {"_id": 5}}
  11. {"price": null,"itemID": "ID100127"}

简单的过滤查询

  1. GET /lib4/items/_search
  2. {
  3. "post_filter": {
  4. "term": {
  5. "price": 40
  6. }
  7. }
  8. }
  9. GET /lib4/items/_search
  10. {
  11. "post_filter": {
  12. "terms": {
  13. "price": [25,40]
  14. }
  15. }
  16. }
  17. GET /lib4/items/_search
  18. {
  19. "post_filter": {
  20. "term": {
  21. "itemID": "ID100123"
  22. }
  23. }
  24. }
  25. 查看分词器分析的结果:
  26. GET /lib4/_mapping
  27. 不希望商品id字段被分词,则重新创建映射
  28. DELETE lib4
  29. PUT /lib4
  30. {
  31. "mappings": {
  32. "items": {
  33. "properties": {
  34. "itemID": {
  35. "type": "text",
  36. "index": false
  37. }
  38. }
  39. }
  40. }
  41. }

bool过滤查询

可以实现组合过滤查询

格式:

  1. {
  2. "bool": {
  3. "must": [],
  4. "should": [],
  5. "must_not": []
  6. }
  7. }
  1. must:必须满足的条件 ---and
  2. should:可以满足也可以不满足的条件--or
  3. must_not:不需要满足的条件 --not
  1. GET /lib4/items/_search
  2. {
  3. "post_filter": {
  4. "bool": {
  5. "should": [
  6. {"term": {"price":25}},
  7. {"term": {"itemID": "id100123"}}],
  8. "must_not": {
  9. "term":{"price": 30}
  10. }
  11. }
  12. }
  13. }

嵌套使用bool:

  1. GET /lib4/items/_search
  2. {
  3. "post_filter": {
  4. "bool": {
  5. "should": [
  6. {"term": {"itemID": "id100123"}},
  7. {
  8. "bool": {
  9. "must": [
  10. {"term": {"itemID": "id100124"}},
  11. {"term": {"price": 40}}
  12. ]
  13. }
  14. }
  15. ]
  16. }
  17. }
  18. }

范围过滤

gt: >

lt: <

gte: >=

lte: <=

  1. GET /lib4/items/_search
  2. {
  3. "post_filter": {
  4. "range": {
  5. "price": {
  6. "gt": 25,
  7. "lt": 50
  8. }
  9. }
  10. }
  11. }

过滤非空

  1. GET /lib4/items/_search
  2. {
  3. "query": {
  4. "bool": {
  5. "filter": {
  6. "exists":{
  7. "field":"price"
  8. }
  9. }
  10. }
  11. }
  12. }
  13. GET /lib4/items/_search
  14. {
  15. "query" : {
  16. "constant_score" : {
  17. "filter": {
  18. "exists" : { "field" : "price" }
  19. }
  20. }
  21. }
  22. }

过滤器缓存

ElasticSearch提供了一种特殊的缓存,即过滤器缓存(filter cache),用来存储过滤器的结果,被缓存的过滤器并不需要消耗过多的内存(因为它们只存储了哪些文档能与过滤器相匹配的相关信息),而且可供后续所有与之相关的查询重复使用,从而极大地提高了查询性能。

  1. 注意:ElasticSearch并不是默认缓存所有过滤器,
  2. 以下过滤器默认不缓存:
  3. numeric_range
  4. script
  5. geo_bbox
  6. geo_distance
  7. geo_distance_range
  8. geo_polygon
  9. geo_shape
  10. and
  11. or
  12. not
  13. exists,missing,range,term,terms默认是开启缓存的

开启方式:在filter查询语句后边加上"_catch":true

聚合查询

  1. (1)sum
  2. GET /lib4/items/_search
  3. {
  4. "size":0,
  5. "aggs": {
  6. "price_of_sum": {
  7. "sum": {
  8. "field": "price"
  9. }
  10. }
  11. }
  12. }
  1. (2)min
  2. GET /lib4/items/_search
  3. {
  4. "size": 0,
  5. "aggs": {
  6. "price_of_min": {
  7. "min": {
  8. "field": "price"
  9. }
  10. }
  11. }
  12. }
  1. (3)max
  2. GET /lib4/items/_search
  3. {
  4. "size": 0,
  5. "aggs": {
  6. "price_of_max": {
  7. "max": {
  8. "field": "price"
  9. }
  10. }
  11. }
  12. }
  1. (4)avg
  2. GET /lib4/items/_search
  3. {
  4. "size":0,
  5. "aggs": {
  6. "price_of_avg": {
  7. "avg": {
  8. "field": "price"
  9. }
  10. }
  11. }
  12. }
  1. (5)cardinality:求基数
  2. GET /lib4/items/_search
  3. {
  4. "size":0,
  5. "aggs": {
  6. "price_of_cardi": {
  7. "cardinality": {
  8. "field": "price"
  9. }
  10. }
  11. }
  12. }
  1. (6)terms:分组
  2. GET /lib4/items/_search
  3. {
  4. "size":0,
  5. "aggs": {
  6. "price_group_by": {
  7. "terms": {
  8. "field": "price"
  9. }
  10. }
  11. }
  12. }

对那些有唱歌兴趣的用户按年龄分组

  1. GET /lib3/user/_search
  2. {
  3. "query": {
  4. "match": {
  5. "interests": "changge"
  6. }
  7. },
  8. "size": 0,
  9. "aggs":{
  10. "age_group_by":{
  11. "terms": {
  12. "field": "age",
  13. "order": {
  14. "avg_of_age": "desc"
  15. }
  16. },
  17. "aggs": {
  18. "avg_of_age": {
  19. "avg": {
  20. "field": "age"
  21. }
  22. }
  23. }
  24. }
  25. }
  26. }

复合查询

将多个基本查询组合成单一查询的查询

使用bool查询

接收以下参数:

must:

文档 必须匹配这些条件才能被包含进来。

must_not:

文档 必须不匹配这些条件才能被包含进来。

should:

如果满足这些语句中的任意语句,将增加 _score,否则,无任何影响。它们主要用于修正每个文档的相关性得分。

filter:

必须 匹配,但它以不评分、过滤模式来进行。这些语句对评分没有贡献,只是根据过滤标准来排除或包含文档。

相关性得分是如何组合的。每一个子查询都独自地计算文档的相关性得分。一旦他们的得分被计算出来, bool 查询就将这些得分进行合并并且返回一个代表整个布尔操作的得分。

下面的查询用于查找 title 字段匹配 how to make millions 并且不被标识为 spam 的文档。那些被标识为 starred 或在2014之后的文档,将比另外那些文档拥有更高的排名。如果 两者 都满足,那么它排名将更高:

  1. {
  2. "bool": {
  3. "must": { "match": { "title": "how to make millions" }},
  4. "must_not": { "match": { "tag": "spam" }},
  5. "should": [
  6. { "match": { "tag": "starred" }},
  7. { "range": { "date": { "gte": "2014-01-01" }}}
  8. ]
  9. }
  10. }

如果没有 must 语句,那么至少需要能够匹配其中的一条 should 语句。但,如果存在至少一条 must 语句,则对 should 语句的匹配没有要求。

如果我们不想因为文档的时间而影响得分,可以用 filter 语句来重写前面的例子:

  1. {
  2. "bool": {
  3. "must": { "match": { "title": "how to make millions" }},
  4. "must_not": { "match": { "tag": "spam" }},
  5. "should": [
  6. { "match": { "tag": "starred" }}
  7. ],
  8. "filter": {
  9. "range": { "date": { "gte": "2014-01-01" }}
  10. }
  11. }
  12. }

通过将 range 查询移到 filter 语句中,我们将它转成不评分的查询,将不再影响文档的相关性排名。由于它现在是一个不评分的查询,可以使用各种对 filter 查询有效的优化手段来提升性能。

bool 查询本身也可以被用做不评分的查询。简单地将它放置到 filter 语句中并在内部构建布尔逻辑:

  1. {
  2. "bool": {
  3. "must": { "match": { "title": "how to make millions" }},
  4. "must_not": { "match": { "tag": "spam" }},
  5. "should": [
  6. { "match": { "tag": "starred" }}
  7. ],
  8. "filter": {
  9. "bool": {
  10. "must": [
  11. { "range": { "date": { "gte": "2014-01-01" }}},
  12. { "range": { "price": { "lte": 29.99 }}}
  13. ],
  14. "must_not": [
  15. { "term": { "category": "ebooks" }}
  16. ]
  17. }
  18. }
  19. }
  20. }

constant_score查询

它将一个不变的常量评分应用于所有匹配的文档。它被经常用于你只需要执行一个 filter 而没有其它查询(例如,评分查询)的情况下。

  1. {
  2. "constant_score": {
  3. "filter": {
  4. "term": { "category": "ebooks" }
  5. }
  6. }
  7. }

term 查询被放置在 constant_score 中,转成不评分的filter。这种方式可以用来取代只有 filter 语句的 bool 查询。

ELK查询命令详解总结的更多相关文章

  1. ELK查询命令详解

    目录 ELK查询命令详解 倒排索引 使用ElasticSearch API 实现CRUD 批量获取文档 使用Bulk API 实现批量操作 版本控制 什么是Mapping? 基本查询(Query查询) ...

  2. Mongodb查询命令详解

    前面我们简单的讲了下find方法,下面来深入的过一下它的用法以及常用的字方法. 下面是mongo中db.user.help()中对find方法的定义和解释: db.user.find([query], ...

  3. Linux系统诊断必备技能之二:日志查询常用命令详解

    一.概述 日常运维工作中,排查线上环境问题,少不了去线上查日志.而使用什么命令,能快速准确地查到我们需要查找地日志信息,也是我们需要掌握的一项技能.下面介绍一下日常工作常用到的查看日志命令:tail, ...

  4. windows/NBTSTAT,linux/nmblookup命令详解,查询NetBIOS名

    NBTSTAT命令详解 请问: Linux下有没有和nbtstat一样的命令,用 nmblookup -A ip 可以 nbstat命令主要用于查看当前基于netbios的tcp/ip连接状态,通过该 ...

  5. linux yum命令详解

    yum(全称为 Yellow dog Updater, Modified)是一个在Fedora和RedHat以及SUSE中的Shell前端软件包管理器.基於RPM包管理,能够从指定的服务器自动下载RP ...

  6. android adb 命令详解

    ADB (Android Debug Bridge)  是android SDK中的工具,需要先配置环境变量才能使用.起调试桥的作用,可以管理安卓设备.(也叫debug工具) ---------查看设 ...

  7. Top 命令详解

    Top 命令详解 先感受一下top命令的执行结果吧!哈哈-- top - 17:32:34 up 3 days, 8:04, 5 users, load average: 0.09, 0.12, 0. ...

  8. DOS命令详解

    DOS命令详解 命令 \? 可以进入命令帮助 1.md命令创建目录. MKDIR [drive:]pathMD [drive:]path 如果命令扩展被启用,MKDIR 会如下改变: 如果需要,MKD ...

  9. Linux/CentOS 服务安装/卸载,开机启动chkconfig命令详解|如何让MySQL、Apache开机启动?

    chkconfig chkconfig在命令行操作时会经常用到.它可以方便地设置和查询不同运行级上的系统服务.这个可要好好掌握,用熟练之后,就可以轻轻松松的管理好你的启动服务了. 注:谨记chkcon ...

随机推荐

  1. Web Share API

    Web Share API https://w3c.github.io/web-share/ Web Share API, W3C Editor's Draft 15 April 2020 https ...

  2. javascript IIFE in depth

    javascript IIFE in depth function type 函数表达式 x = function (){ console.log(x); } ƒ (){ console.log(x) ...

  3. nginx proxy

    listen 80; server_name localhost; # 访问"localhost"的全部请求会被转发到"localhost:81" # loca ...

  4. flutter 插件调用callback函数

    dart plugin class TestLib { static MethodChannel _channel = const MethodChannel('test_lib') ..setMet ...

  5. BGV暴涨千倍,未来或将超越YFI领跑DeFi全场!

    毫无疑问,YFI在2020年上半年以一己之力掀翻了DeFi市场的热潮.迄今为止,YFI的新鲜资讯从不缺席,最近也是频频登上各大知名媒体热搜.其币价远远超过比特币价格,也让资本市场注意到DeFi市场原来 ...

  6. mysql数据库表引入redis解决方案

    缓存方案 缓存方案在我的另外一篇博客里有详细说明,地址:https://www.cnblogs.com/wingfirefly/p/14419728.html 数据结构: 方案1: 1.存储结构采用h ...

  7. 2021-2-22:请你说下 CAP 理论并举例

    CAP CAP 理论是分布式系统中的一个老生常谈的理论了,最早由 Eric Brewer 在一个讲座中提出.在这个讲座中,在传统 ACID 理论以及当时比较流行但是比较抽象的的设计指导理论 BASE ...

  8. Spring Cloud基础

    1.网站架构演变过程 传统架构(单点应用SSM或SSH)→分布式架构(项目拆分)→SOA架构(面向服务架构)→微服务架构 2.微服务概述 2.1SOA架构 面向服务的架构(SOA)是一个组件模型,它将 ...

  9. E: Some index files failed to download. They have been**

    转: E: Some index files failed to download. They have been** 问题描述: 当使用Dockerfile从包含cuda的镜像建立新的image的时 ...

  10. 公钥基础设施PKI利用SRAM物理不可克隆函数PUF实现芯片标识唯一性

    下面给出PKI利用SRAM PUF实现芯片标识唯一性的方法思路: PKI利用SRAM PUF实现芯片标识唯一性的方式 (1)使用PUF原因 物理上不可克隆函数利用硅制造的自然变化来产生每个芯片统计上唯 ...