Elasticsearch 深入搜索

  • 因为带请求体的 GET 请求并不被广泛支持,
    • 所以  任何需要带请求体的 GET API 同时支持 POST 请求


  • 查询语句(query clauses)的结构
    • QUERY_NAME:{FILED_NAME:{ARGUMENT:VALUE,...}}
      • 好像只有match_all,不必指定FIELD_NAME
  • 查询语句(Query clauses) 就像一些简单的组合块
    • 叶子语句(Leaf clauses)
    • 复合语句(Compound) 



  • range 查询,可以对精确值进行范围查询
  • The term query 
    • finds documents that contain the exact term specified in the inverted index
  • terms 查询 term 查询一样,
    • 但它允许你指定多值进行匹配。如果这个字段包含了指定值中的任何一个值,那么这个文档满足条件
  • Exists Query:
    • null, [] (空数组)些都是等价的,它们无法存于倒排索引中
      • 但是为了查询value missing的情况,所以才有Exists Query
    • Returns documents that have at least one non-null value in the original field
      • 空字符串不是null value
    • missing query (Exists Query的反义)
      • There isn’t a missing query. Instead use the exists query inside a  must_not clause 
    • 区分一个字段是没有值,还是被显式的设置成了 null 
      • 使用 Mapping parameters中的 null_value来实现
      • 注意:The null_value needs to be the same datatype as the field.
        • For instance, a long field cannot have a string null_value


  • match 查询
    • 底层会拆分为term 查询
    • match 查询支持 Minimum Should Match 最小匹配参数
      • 指定必须匹配的term数
    • operator 参数
      • 默认拆分成的多个term查询之间是or关系。可以指定and关系。
  • multi_match 查询可以在多个字段上执行相同的 match 查询


  • bool 查询结构:
    • bool:{must/ should、must_not /filter : {query clauses}}
      • bool 语句,可以嵌套到filter语句中。
    • must_not和filter,属于Filter Context。不影响score
    • 如果存在must语句,should 语句只影响score,不过滤文档。


  • 使用 boost 为Query Clause增加评分权重



  • The behaviour of a query clause depends on whether it is used in query context or in filter context
    • Filter Context(过滤情况):关心的是是否匹配(过滤掉不匹配的文档
      • Filter context is in effect whenever a query clause is passed to a filter parameter, 
        • such as the filter or must_not parameters in the bool query, 
        • the filter parameter in the constant_scorequery, 
        • or the filter aggregation.
      • filter 性能更好:
        • fillter 不计算score。。
          • filter 语句返回的score是0
          • 可以使用  Constant Score Query 让filter语句返回一个非0常数
        • Frequently used filters will be cached automatically by
    • ElasticsearchQuery Context(查询情况):关心的是匹配相关度
      • 除Filter Context之外,都是处于Query Context
      • 处于Query Context 的query clause,会计算score
        • 对于精确值子语句,score要么是0,要么是boost value(默认值是1)








  • 所有field,只要想searchable,就必须index(即加入倒排索引,可搜索)
  • index方式不同:
    • 结构化datatype,不经过Analysis,整体作为token ,加入倒排索引
    • 全文datatype,要经过Analysis,解析成tokens,加入倒排索引
      • 貌似只有 Text datatype是全文字段。有相关的Analysis。
  • 如何确定使用哪个分析器
    • Index Time Analysis:
      • 分析器可以从三个层面进行定义,Elasticsearch 会按照顺序依次查找
        • 字段映射里定义的 analyzer 索引设置中默认分析器、全局默认(global default)。
    • Search Time Analysis(针对Query string
      • 优先级最高位置,依次添加了两个选项


  • string类型的数据,在Elasticsearch 6以后,已经分为keyword、text
    • keyword是结构化数据
      • keyword有 normalizer(标准化),类似于 analyzer
      • 区别是normalizer produces a single token
      • normalizer默认为null
    • text是全文数据
    • index参数,只用于限定当前field是否index



  • 当进行精确值查找时, 一般会使用过滤器(filters)
  • 非评分计算(过滤)是首先执行的,
    • 这将有助于写出高效又快速的搜索请求。





  • Term level queries  bitsets缓存
    • bitset 是 一个包含 0 和 1 的数组,
      • 它描述了哪个文档会包含该 term 。匹配文档的标志位是 1 。
    • bitsets 以增量方式更新。
      • 当我们索引新文档时,只需将那些新文档加入已有 bitset,
    • Elasticsearch 会基于使用频次自动缓存查询
      • 如果一个非评分查询在最近的 256 词查询中被使用过(次数取决于查询类型),那么这个查询就会作为缓存的候选。
      • 但是,并不是所有的片段都能保证缓存 bitset 
        • 只有那些文档数量超过 10,000 (或超过总文档数量的 3% )才会缓存 bitset 。
        • 因为小的片段可以很快的进行搜索和合并,这里缓存的意义不大。
      • 缓存剔除规则是基于 LRU 的:
        • 一旦缓存满了,最近最少使用的过滤器会被剔除。




  • 由于数据太少,可能引起的基于分片的本地IDF 和全局的 IDF 的差异
    • 因为评分计算是基于分片,然后汇总。
      • 所以这种差异会造成评分错误
    • 在真实世界的数据量下,局部的 IDF 会被迅速均化
    • 可以使用分布式频率搜索(dfs_query_then_fetch )来解决 
      • 指定search_type=dfs_query_then_fetch参数,
      • 不要在生产环境上使用。完全没有必要




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值