[西柚仔]ElasticSearch高级检索总结

1.1 检索方式 _search

ES官方提供了两中检索方式:一种是通过 URL 参数进行搜索,另一种是通过 DSL(Domain Specified Language) 进行搜索官方更推荐使用第二种方式第二种方式是基于传递JSON作为请求体(request body)格式与ES进行交互,这种方式更强大,更简洁

image-20200701225025975

  • 使用语法

    URL查询: GET /索引/类型/_search?参数

    DSL查询: GET /索引/类型/_search {}

1.2 测试数据

1.删除索引
DELETE /ems

2.创建索引并指定类型
PUT /ems
{
  "mappings":{
    "emp":{
      "properties":{
        "name":{
          "type":"text"
        },
        "age":{
          "type":"integer"
        },
        "bir":{
          "type":"date"
        },
        "content":{
          "type":"text"
        },
        "address":{
          "type":"keyword"
        }
      }
    }
  }
}

3.插入测试数据
PUT /ems/emp/_bulk
  {"index":{}}
  {"name":"小黑","age":23,"bir":"2012-12-12","content":"为开发团队选择一款优秀的MVC框架是件难事儿,在众多可行的方案中决择需要很高的经验和水平","address":"北京"}
  {"index":{}}
  {"name":"王小黑","age":24,"bir":"2012-12-12","content":"Spring 框架是一个分层架构,由 7 个定义良好的模块组成。Spring 模块构建在核心容器之上,核心容器定义了创建、配置和管理 bean 的方式","address":"上海"}
  {"index":{}}
  {"name":"张小五","age":8,"bir":"2012-12-12","content":"Spring Cloud 作为Java 语言的微服务框架,它依赖于Spring Boot,有快速开发、持续交付和容易部署等特点。Spring Cloud 的组件非常多,涉及微服务的方方面面,井在开源社区Spring 和Netflix 、Pivotal 两大公司的推动下越来越完善","address":"无锡"}
  {"index":{}}
  {"name":"win7","age":9,"bir":"2012-12-12","content":"Spring的目标是致力于全方位的简化Java开发。 这势必引出更多的解释, Spring是如何简化Java开发的?","address":"南京"}
  {"index":{}}
  {"name":"梅超风","age":43,"bir":"2012-12-12","content":"Redis是一个开源的使用ANSI C语言编写、支持网络、可基于内存亦可持久化的日志型、Key-Value数据库,并提供多种语言的API","address":"杭州"}
  {"index":{}}
  {"name":"张无忌","age":59,"bir":"2012-12-12","content":"ElasticSearch是一个基于Lucene的搜索服务器。它提供了一个分布式多用户能力的全文搜索引擎,基于RESTful web接口","address":"北京"}

1.2 URL检索

GET /ems/emp/_search?q=*&sort=age:asc

​ _search 搜索的API
​ q=* 匹配所有文档
​ sort 以结果中的指定字段排序

GET /ems/emp/_search?q=*&sort=age:desc&size=5&from=0&_source=name,age,bir

1.3 DSL检索

NOTE: 以下重点讲解DSL语法

GET /ems/emp/_search
{
    "query": {"match_all": {}},
    "sort": [
        {
            "age": {
                "order": "desc"
            }
        }
    ]
}

1.4 DSL高级检索(Query)

0. 查询所有(match_all)

match_all关键字: 返回索引中的全部文档

GET /ems/emp/_search
{
 	"query": { "match_all": {} }
}	
1. 查询结果中返回指定条数(size)

size 关键字: 指定查询结果中返回指定条数。 默认返回值10条

GET /ems/emp/_search
{
 	"query": { "match_all": {} },
	"size": 1
}	
2. 分页查询(from)

from 关键字: 用来指定起始返回位置,和size关键字连用可实现分页效果

GET /ems/emp/_search
{
      "query": {"match_all": {}},
      "sort": [
        {
          "age": {
            "order": "desc"
          }
        }
      ],
      "size": 2, 
      "from": 1
}
3. 查询结果中返回指定字段(_source)

_source 关键字: 是一个数组,在数组中用来指定展示那些字段

GET /ems/emp/_search
{
      "query": { "match_all": {} },
      "_source": ["name", "age"]
}
4. 关键词查询(term)

term 关键字: 用来使用关键词查询

GET /ems/emp/_search
{
  "query": {
    "term": {
      "address": {
        "value": "北京"
      }
    }
  }
}

NOTE1: 通过使用term查询得知ES中默认使用分词器为标准分词器(StandardAnalyzer),标准分词器对于英文单词分词,对于中文单字分词

NOTE2: 通过使用term查询得知,在ES的Mapping Type 中 keyword , date ,integer, long , double , boolean or ip 这些类型不分词只有text类型分词

5. 范围查询(range)

range 关键字: 用来指定查询指定范围内的文档

GET /ems/emp/_search
{
  "query": {
    "range": {
      "age": {
        "gte": 8,
        "lte": 30
      }
    }
  }
}
6. 前缀查询(prefix)

prefix 关键字: 用来检索含有指定前缀的关键词的相关文档

GET /ems/emp/_search
{
  "query": {
    "prefix": {
      "content": {
        "value": "redis"
      }
    }
  }
}
7. 通配符查询(wildcard)

wildcard 关键字: 通配符查询 ? 用来匹配一个任意字符 * 用来匹配多个任意字符

GET /ems/emp/_search
{
  "query": {
    "wildcard": {
      "content": {
        "value": "re*"
      }
    }
  }
}
8. 多id查询(ids)

ids 关键字 : 值为数组类型,用来根据一组id获取多个对应的文档

GET  /ems/emp/_search
{
  "query": {
    "ids": {
      "values": ["lg5HwWkBxH7z6xax7W3_","lQ5HwWkBxH7z6xax7W3_"]
    }
  }
}
9. 模糊查询(fuzzy)

fuzzy 关键字: 用来模糊查询含有指定关键字的文档

GET /ems/emp/_search
{
  "query": {
    "fuzzy": {
      "content":"spring"
    }
  }
}

fuzzy 模糊查询  最大模糊错误 必须在0-2之间
# 搜索关键词长度为 2 不允许存在模糊 0
# 搜索关键词长度为3-5 允许一次模糊 0 1 
# 搜索关键词长度大于5 允许最大2模糊
10. 布尔查询(bool)

bool 关键字: 用来组合多个条件实现复杂查询

must: 相当于&& 同时成立

should: 相当于|| 成立一个就行

must_not: 相当于! 不能满足任何一个

GET /ems/emp/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "range": {
            "age": {
              "gte": 0,
              "lte": 30
            }
          }
        }
      ],
      "must_not": [
        {"wildcard": {
          "content": {
            "value": "redi?"
          }
        }}
      ]
    }
  },
  "sort": [
    {
      "age": {
        "order": "desc"
      }
    }
  ]
}
11. 高亮查询(highlight)

highlight 关键字: 可以让符合条件的文档中的关键词高亮

GET /ems/emp/_search
{
  "query": {
    "term": {
      "content": {
        "value": "redis"
      }
    }
  },
  "highlight": {
    "fields": {
      "*": {}
    }
  }
}

自定义高亮html标签: 可以在highlight中使用pre_tagspost_tags

GET /ems/emp/_search
{
  "query":{
    "term":{
      "content":"框架"
    }
  },
  "highlight": {
    "pre_tags": ["<span style='color:red'>"],
    "post_tags": ["</span>"],
    "fields": {
      "*":{}
    }
  }
}

多字段高亮 使用require_field_match开启多个字段高亮

 GET /ems/emp/_search
{
  "query":{
    "term":{
      "content":"框架"
    }
  },
  "highlight": {
    "pre_tags": ["<span style='color:red'>"],
    "post_tags": ["</span>"],
    "require_field_match":false,
    "fields": {
      "*":{}
    }
  }
}
12. 多字段查询(multi_match)
GET /ems/emp/_search
{
  "query": {
    "multi_match": {
      "query": "中国",
      "fields": ["name","content"] #这里写要检索的指定字段
    }
  } 
}

2. Filter Query(过滤查询)

2.1 过滤查询

其实准确来说,ES中的查询操作分为2种: 查询(query)过滤(filter)查询即是之前提到的query查询,它 (查询)默认会计算每个返回文档的得分,然后根据得分排序而过滤(filter)只会筛选出符合的文档,并不计算 得分,且它可以缓存文档 。所以,单从性能考虑,过滤比查询更快

换句话说,过滤适合在大范围筛选数据,而查询则适合精确匹配数据。一般应用时, 应先使用过滤操作过滤数据, 然后使用查询匹配数据。

image-20200703141421055

2.2 过滤语法

GET /ems/emp/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "term": {
            "content": {
              "value": "框架"
            }
          }
          
        }
      ],
      "filter": {
        "range": {
          "age": {
            "gte": 8,
            "lte": 23
          }
        }
      }
    }
  }
}

NOTE: 在执行filter和query时,先执行filter在执行query

NOTE:Elasticsearch会自动缓存经常使用的过滤器,以加快性能。

2.3 常见的过滤器类型

term 、 terms Filter
GET /ems/emp/_search   # 使用term过滤
{
  "query": {
    "bool": {
      "must": [
        {"term": {
          "name": {
            "value": "小黑"
          }
        }}
      ],
      "filter": {
        "term": {
          "content":"框架"
        }
      }
    }
  }
}
ranage filter
GET /ems/emp/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "term": {
            "content": {
              "value": "框架"
            }
          }
          
        }
      ],
      "filter": {
        "range": {
          "age": {
            "gte": 8,
            "lte": 23
          }
        }
      }
    }
  }
}
exists filter

过滤存在指定字段,获取字段不为空的索引记录使用

GET /ems/emp/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "term": {
            "content": {
              "value": "框架"
            }
          }
          
        }
      ],
      "filter": {
        "exists": {
          "field": "address"
        }
      }
    }
  }
}
ids filter

过滤含有指定字段的索引记录

GET /ems/emp/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "term": {
            "content": {
              "value": "框架"
            }
          }
          
        }
      ],
      "filter": {
        "ids": {
          "values": [
            "M3CK3HMB3-QMRFjecIJu",
            "NXCK3HMB3-QMRFjecIJu"
          ]
        }
      }
    }
  }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值