elasticsearch 极客时间学习笔记(四)

223 阅读1分钟

基于Term和基于全文的搜索

基于Term的查询

image.png

示例

image.png

POST /products/_search
{
  "query": {
    "term": {
      "desc": {
        "value": "iPhone" //不能查询到结果,因为term查询的时候不会做分词处理,但index数据的时候做了分词
        "value":"iphone" //能查询到结果,
      }
    }
  }
}

要做完全匹配用keyword image.png

复合查询 - ConstantScore 转为 Filter

image.png

基于全文的查询

特点

image.png

示例

image.png image.png

image.png

Match Query查询过程

image.png

结构化搜索

结构化数据

image.png

ES中的结构化搜索

image.png

#结构化搜索,精确匹配
DELETE products
POST /products/_bulk
{ "index": { "_id": 1 }}
{ "price" : 10,"avaliable":true,"date":"2018-01-01", "productID" : "XHDK-A-1293-#fJ3" }
{ "index": { "_id": 2 }}
{ "price" : 20,"avaliable":true,"date":"2019-01-01", "productID" : "KDKE-B-9947-#kL5" }
{ "index": { "_id": 3 }}
{ "price" : 30,"avaliable":true, "productID" : "JODL-X-1937-#pV7" }
{ "index": { "_id": 4 }}
{ "price" : 30,"avaliable":false, "productID" : "QQPX-R-3956-#aD8" }

GET products/_mapping



#对布尔值 match 查询,有算分
POST products/_search
{
  "profile": "true",
  "explain": true,
  "query": {
    "term": {
      "avaliable": true
    }
  }
}



#对布尔值,通过constant score 转成 filtering,没有算分
POST products/_search
{
  "profile": "true",
  "explain": true,
  "query": {
    "constant_score": {
      "filter": {
        "term": {
          "avaliable": true
        }
      }
    }
  }
}


#数字类型 Term
POST products/_search
{
  "profile": "true",
  "explain": true,
  "query": {
    "term": {
      "price": 30
    }
  }
}

#数字类型 terms
POST products/_search
{
  "query": {
    "constant_score": {
      "filter": {
        "terms": {
          "price": [
            "20",
            "30"
          ]
        }
      }
    }
  }
}

#数字 Range 查询
GET products/_search
{
    "query" : {
        "constant_score" : {
            "filter" : {
                "range" : {
                    "price" : {
                        "gte" : 20,
                        "lte"  : 30
                    }
                }
            }
        }
    }
}


# 日期 range
POST products/_search
{
    "query" : {
        "constant_score" : {
            "filter" : {
                "range" : {
                    "date" : {
                      "gte" : "now-1y"
                    }
                }
            }
        }
    }
}

搜索的相关性算分

相关性和相关性算分

image.png

词频 TF

image.png

逆文档频率 IDF

image.png

TF-IDF的概念

image.png

BM 25

image.png

Query&Filtering与多字符串多字段查询

Query Context & Filter Context

image.png

条件组合

image.png

bool查询

image.png

bool查询语法

image.png

Filter Context - 不影响算分

image.png

Query Context - 影响算分

image.png

image.png

控制字段的Boosting

image.png

单字符串多字段查询:Dis-Max-Query
单字符串多字段查询:Multi-Match