Elasticsearch 基于词项和基于全文的搜索详解

1. term基于词项查询

term 的 重要性
Term 是表达语意的最小单位。搜索和利用统计语言模型进行自然语言处理都需要处理 Term

1.1 特点

  • Term Level Query:Term Query / Range Query / Exists Query / Prefix
    Query / Wildcard Query
  • 在 ES 中,Term查询,对输入不做分词。会将输入作为一个整体,在倒排索引中查找准确的词项,并且使用相关度算分公式为每个包含该词项的文档进行相关度算分 -例如 “Apple Store”
  • 可以通过 Constant Score 将查询转换换成一个 Filtering,避免算分,并利用缓存,提交性能

1.2 demo

插入数据

POST /products/_bulk
{ "index": { "_id": 1 }}
{ "productID" : "XHDK-A-1293-#fJ3","desc":"iPhone" }
{ "index": { "_id": 2 }}
{ "productID" : "KDKE-B-9947-#kL5","desc":"iPad" }
{ "index": { "_id": 3 }}
{ "productID" : "JODL-X-1937-#pV7","desc":"MBP" }

查询

POST /products/_search
{
"query": {
  "term": {
    "desc.keyword": {
      "value": "iPhone" //查不多数据,term查询
      //"value":"iphone" // 查到数据 term查询会做分词
    }
  }
}
}
POST /products/_search
{
"query": {
  "term": {
    "productID": {
      "value": "XHDK-A-1293-#fJ3"  // 无结果
      "value": "xhdk" //有一条数据
      "value": "xhdk-a-1293-#fj3"
    }
  }
}
}
//如果对值进行查询
POST /products/_search
{
//"explain": true,
"query": {
  "term": {
    "productID.keyword": {
      "value": "XHDK-A-1293-#fJ3"
    }
  }
}
}

1.3 复合查询 - Constant Score 转为 Filter

  • 将 Query 转成 Filter,忽略 TF-IDF 计算,避免相关性算分的开销
  • Filter 可以有效利用缓存
POST /products/_search
{
  "explain": true,
  "query": {
    "constant_score": {
      "filter": {
        "term": {
          "productID.keyword": "XHDK-A-1293-#fJ3"
        }
      }
    }
  }
}

2. 基于全文本的查找

  • Match Query / Match Phrase Query / Query String Query

2.1 特点

  • 索引和搜索时会进行分词,查询字符串先传递到一个合适的分词器,然后生成一个供查询的词项列表
  • 查询时候,先会对输入的查询进行分词。然后每个词项逐个进行底层的查询,最终将结果进行合并。并未每个文档生成一个算分。 例如查 “Martix reloaded”, 会查到包括 Matrix 或者 reload 的所有结果

2.2. Match Query Result

POST movies/_search
{
  "query":{
    "match": {
      "title":{
        "query": "Matrix reloaded"
      }
    }
  }
}

2.3 Operator

POST movies/_search
{
  "query":{
    "match": {
      "title":{
        "query": "Matrix reloaded",
        "operator":"AND"
      }
    }
  }
}

2.4 Minimun_should_match

POST movies/_search
{
  "query":{
    "match": {
      "title":{
        "query": "Matrix reloaded",
        "minimum_should_match":2"
      }
    }
  }
}

2.5 Match Phrase Query

POST movies/_search
{
  "profile":true,   //启用Profile API
  "query":{
    "match_phrase": {
      "title":{
        "query": "Matrix reloaded",
        "slop":1"
      }
    }
  }
}

2.6 Match Query 查询过程

基于全文本的查找

  • Match Query / Match Phrase Query / Query String Query

基于全文本的查询的特点

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ghostwritten

口渴,请赏一杯下午茶吧

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值