Query DSL(Domain Specific Language)

1 查询上下文

使用query关键字进行检索,倾向于相关度搜索,故需要计算评分。搜索是Elasticsearch最关键和重要的部分。

2 相关度评分:_score

概念:相关度评分用于对搜索结果排序,评分越高则认为其结果和搜索的预期值相关度越高,即越符合搜索预期值。在7.x之前相关度评分默认使用TF/IDF算法计算而来,7.x之后默认为BM25。在核心知识篇不必关心相关评分的具体原理,只需知晓其概念即可。

排序:相关度评分为搜索结果的排序依据,默认情况下评分越高,则结果越靠前。

3 元数据:_source

  1. 禁用_source:
    1. 好处:节省存储开销
    2. 坏处:
      • 不支持update、update_by_query和reindex API。
      • 不支持高亮。
      • 不支持reindex、更改mapping分析器和版本升级。
      • 通过查看索引时使用的原始文档来调试查询或聚合的功能。
      • 将来有可能自动修复索引损坏。

总结:如果只是为了节省磁盘,可以压缩索引比禁用_source更好。

  1. 数据源过滤器:
    Including:结果中返回哪些field
    Excluding:结果中不要返回哪些field,不返回的field不代表不能通过该字段进行检索,因为元数据不存在不代表索引不存在
    1. 在mapping中定义过滤:支持通配符,但是这种方式不推荐,因为mapping不可变
PUT product
{
  "mappings": {
    "_source": {
      "includes": [
        "name",
        "price"
      ],
      "excludes": [
        "desc",
        "tags"
      ]
    }
  }
}

    1. 常用过滤规则
      • "_source": "false",
      • "_source": "obj.*",
      • "_source": [ "obj1.*", "obj2.*" ],
      • "_source": {
        "includes": [ "obj1.*", "obj2.*" ],
        "excludes": [ "*.description" ]
        }

4 Query String


  • GET /product/_search
查询所有:

  • GET /product/_search?q=name:xiaomi
带参数:

  • GET /product/_search?from=0&size=2&sort=price:asc
分页:

  • GET /product/_search?q=date:2021-06-01
精准匹配 exact value

  • GET /product/_search?q=2021-06-01
_all搜索 相当于在所有有索引的字段中检索
DELETE product
# 验证_all搜索
PUT product
{
  "mappings": {
    "properties": {
      "desc": {
        "type": "text", 
        "index": false
      }
    }
  }
}
# 先初始化数据
POST /product/_update/5
{
  "doc": {
    "desc": "erji zhong de kendeji 2021-06-01"
  }
}

5 全文检索-Fulltext query

GET index/_search
{
  "query": {
    ***
  }
}

match:匹配包含某个term的子句
GET product/_search
{
  "query": {
    "match": {
      "name": "xiaomi nfc phone"
    }
  }
}  

multi_match:多字段条件
GET product/_search
{
  "query": {
    "multi_match": {
      "query": "huangmenji",
      "fields": ["name","desc"]
    }
  }
  
}  

match_all:匹配所有结果的子句
match_phrase:短语查询,
GET product/_search
{
  "query": {
    "match_phrase": {
      "name": "xiaomi nfc"
    }
  }
}

6 精准查询-Term query

GET product/_search
{
  "query": {
    "term": {
      "name": {
        "value": "xiaomi phone"
      }
    }
  }
}

term:匹配和搜索词项完全相等的结果
    • term和match_phrase区别:
      match_phrase 会将检索关键词分词, match_phrase的分词结果必须在被检索字段的分词中都包含,而且顺序必须相同,而且默认必须都是连续的
      term搜索不会将搜索词分词
    • term和keyword区别
      term是对于搜索词不分词,
      keyword是字段类型,是对于source data中的字段值不分词
terms:匹配和搜索词项列表中任意项匹配的结果
GET product/_search
{
  "query": {
    "terms": {
      "tags": [
        "xingjiabi",
        "yinzhicha"
      ]
    }
  }
}  

range:范围查找
GET product/_search
{
  "query": {
    "range": {
      "price": {
        "gte": 2999,
        "lte": 4999
      }
    }
  }
}

7 过滤器-Filter

GET product/_search
{
  "query": {
    "constant_score": {
      "filter": {
        "term": {
          "name": "phone"
        }
      }
    }
  }
}

GET product/_search
{
  "query": {
    "bool": {
      "filter": {
        "term": {
          "name": "phone"
        }
      }
    }
  }
}

filter可以嵌套在constant_score和bool里

  • filter:query和filter的主要区别在: filter是结果导向的而query是过程导向。query倾向于“当前文档和查询的语句的相关度”而filter倾向于“当前文档和查询的条件是不是相符”。即在查询过程中,query是要对查询的每个结果计算相关性得分的,而filter不会。另外filter有相应的缓存机制,可以提高查询效率。

8 组合查询-Bool query

bool:可以组合多个查询条件,bool查询也是采用more_matches_is_better的机制,因此满足must和should子句的文档将会合并起来计算分值

  • must:必须满足子句(查询)必须出现在匹配的文档中,并将有助于得分。就是满足所有查询条件
GET product/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "name": "phone"
          }
        },{
          "match": {
            "price": "3999"
          }
        }
      ]
    }
  }
}
  • filter:过滤器 不计算相关度分数,cache☆子句(查询)必须出现在匹配的文档中。但是不像 must,查询的分数将被忽略。Filter子句在filter上下文中执行,这意味着计分被忽略,并且子句被考虑用于缓存。
  • should:可能满足 or子句(查询)应出现在匹配的文档中。
GET product/_search
{
  "query": {
    "bool": {
      "should": [
        {
          "match_phrase": {
            "name": "xiaomi nfc"
          }
        },{
          "range": {
            "price": {
              "gte": 3999
            }
          }
        } 
      ]
    }
  }
}
  • must_not:必须不满足 不计算相关度分数  not子句(查询)不得出现在匹配的文档中。子句在过滤器上下文中执行,这意味着计分被忽略,并且子句被视为用于缓存。由于忽略计分,0因此将返回所有文档的分数。
组合查询

因为filter不计算评分,所以性能更高,可以先根据filter筛出部分数据,再进行其他条件的筛选

GET product/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match_phrase": {
            "name": "xiaomi nfc"
          }
        },{
          "range": {
            "price": {
              "gte": 3999
            }
          }
        } 
      ]
      , "filter": [
        {
          "range": {
            "price": {
              "gte": 4999
            }
          }
        }
      ]
    }
  }
}

minimum_should_match:参数指定should返回的文档必须匹配的子句的数量或百分比。如果bool查询包含至少一个should子句,而没有must或 filter子句,则默认值为1。否则,默认值为0

如果must或者filter配合should使用时,那么默认should里的条件是无效的,可以指定minimum_should_match:1来设置should里的条件至少有一个满足

GET product/_search
{
  "query": {
    "bool": {
      "filter": [
        {
          "range": {
            "price": {
              "gte": 1
            }
          }
        }
      ],
      "should": [
        {"match": {
          "name": "phone"
        }}
      ]
    }
  }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值