ljzsdut
GitHubToggle Dark/Light/Auto modeToggle Dark/Light/Auto modeToggle Dark/Light/Auto modeBack to homepage

03 搜索api

准备数据

PUT ecommerce/product/1
{
  "name": "gaolujie yagao",
  "desc": "gaoxiao meibai",
  "price": 30,
  "producer": "gaolujie producer",
  "tags": [
    "meibai",
    "fangzhu"
  ]
}

PUT ecommerce/product/2
{
  "name": "jiajieshi yagao",
  "desc": "youxiao fangzhu",
  "price": 25,
  "producer": "jiajieshi producer",
  "tags": [
    "fangzhu"
  ]
}

PUT ecommerce/product/3
{
  "name": "zhonghua yagao",
  "desc": "caoben zhiwu",
  "price": 40,
  "producer": "zhonghua producer",
  "tags": [
    "qingxin"
  ]
}

示例1

搜索全部商品:GET /ecommerce/product/_search

查询:

GET /ecommerce/product/_search

结果:

{
  "took": 3,  // took:耗费了几毫秒
  "timed_out": false,  // timed_out:是否查询超时
  "_shards": {  // _shards:数据拆成了5个分片,所以对于搜索请求,会打到所有的primary shard(或者是它的某个replica shard也可以)
    "total": 5,
    "successful": 5,
    "failed": 0
  },
  "hits": {
    "total": 3, // hits.totalh:查询结果的数量,3个document
    "max_score": 1,  //hits.max_score:score的含义,就是document对于一个search的相关度的匹配分数,越相关,就越匹配,分数也高
    "hits": [  //hits.hits:包含了匹配搜索的document的详细数据
      {
        "_index": "ecommerce",
        "_type": "product",
        "_id": "2",
        "_score": 1,
        "_source": {
          "name": "jiajieshi yagao",
          "desc": "youxiao fangzhu",
          "price": 25,
          "producer": "jiajieshi producer",
          "tags": [
            "fangzhu"
          ]
        }
      },
      {
        "_index": "ecommerce",
        "_type": "product",
        "_id": "1",
        "_score": 1,
        "_source": {
          "name": "gaolujie yagao",
          "desc": "gaoxiao meibai",
          "price": 30,
          "producer": "gaolujie producer",
          "tags": [
            "meibai",
            "fangzhu"
          ]
        }
      },
      {
        "_index": "ecommerce",
        "_type": "product",
        "_id": "3",
        "_score": 1,
        "_source": {
          "name": "zhonghua yagao",
          "desc": "caoben zhiwu",
          "price": 40,
          "producer": "zhonghua producer",
          "tags": [
            "qingxin"
          ]
        }
      }
    ]
  }
}

示例2

搜索商品名称中包含yagao的商品,而且按照售价降序排序:

GET /ecommerce/product/_search?q=name:yagao&sort=price:desc
{
  "took": 42,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "failed": 0
  },
  "hits": {
    "total": 3,
    "max_score": null,
    "hits": [
      {
        "_index": "ecommerce",
        "_type": "product",
        "_id": "3",
        "_score": null,
        "_source": {
          "name": "zhonghua yagao",
          "desc": "caoben zhiwu",
          "price": 40,
          "producer": "zhonghua producer",
          "tags": [
            "qingxin"
          ]
        },
        "sort": [
          40
        ]
      },
      {
        "_index": "ecommerce",
        "_type": "product",
        "_id": "1",
        "_score": null,
        "_source": {
          "name": "gaolujie yagao",
          "desc": "gaoxiao meibai",
          "price": 30,
          "producer": "gaolujie producer",
          "tags": [
            "meibai",
            "fangzhu"
          ]
        },
        "sort": [
          30
        ]
      },
      {
        "_index": "ecommerce",
        "_type": "product",
        "_id": "2",
        "_score": null,
        "_source": {
          "name": "jiajieshi yagao",
          "desc": "youxiao fangzhu",
          "price": 25,
          "producer": "jiajieshi producer",
          "tags": [
            "fangzhu"
          ]
        },
        "sort": [
          25
        ]
      }
    ]
  }
}

query string search的由来,因为search参数都是以http请求的query string来附带的。适用于临时的在命令行使用一些工具,比如curl,快速的发出请求,来检索想要的信息;但是如果查询请求很复杂,是很难去构建的 在生产环境中,几乎很少使用query string search。

2、query DSL

DSL:Domain Specified Language,特定领域的语言

http request body:请求体,可以用json的格式来构建查询语法,比较方便,可以构建各种复杂的语法,比query string search肯定强大多了。

示例1:query

查询所有的商品

GET ecommerce/product/_search
{
  "query":{  // 定义查询条件
    "match_all": {}
  }
}

示例2:sort

查询名称包含yagao的商品,同时按照价格降序排序

格式:

GET ecommerce/product/_search
{
  "query": {
    "match": {
      "FIELD": "TEXT"
    }
  },
  "sort": [
    {
      "FIELD": {
        "order": "desc"
      }
    }
  ]
}

示例:

GET ecommerce/product/_search
{
  "query": { // 定义查询条件
    "match": { // 条件类型
      "name": "yagao"
    }
  },
  "sort": [ // 定义排序条件
    {
      "price": { // 排序字段
        "order": "desc"
      }
    }
  ]
}

或:
GET ecommerce/product/_search
{
  "query": {
    "match": {
      "name": "yagao"
    }
  },
  "sort": [
    {
      "price":  "desc"
    }
  ]
}

示例3:from-size(分页)

分页查询商品,总共3条商品,假设每页就显示1条商品,现在显示第2页,所以就查出来第2个商品

GET ecommerce/product/_search
{
  "query": {
    "match": {
      "name": "yagao"
    }
  },
  "sort": [
    {
      "price": "desc"
    }
  ],
  "from": 1,  //目标数据的偏移值,默认from为0
  "size": 1		// 当前返回的数据数目,默认size为10
}

示例4:_source(投影查询)

指定要查询出来商品的名称和价格就可以(投影操作),更加适合生产环境的使用,可以构建复杂的查询。

GET ecommerce/product/_search
{
  "query": {
    "match": {
      "name": "yagao"
    }
  },
  "_source": [  // 指定_source字段的要显示的字段
    "name",
    "price"
  ]
}

结果:

{
  "took": 36,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "failed": 0
  },
  "hits": {
    "total": 3,
    "max_score": 0.25811607,
    "hits": [
      {
        "_index": "ecommerce",
        "_type": "product",
        "_id": "2",
        "_score": 0.25811607,
        "_source": {
          "price": 25,
          "name": "jiajieshi yagao"
        }
      },
      {
        "_index": "ecommerce",
        "_type": "product",
        "_id": "1",
        "_score": 0.25811607,
        "_source": {
          "price": 30,
          "name": "gaolujie yagao"
        }
      },
      {
        "_index": "ecommerce",
        "_type": "product",
        "_id": "3",
        "_score": 0.25811607,
        "_source": {
          "price": 40,
          "name": "zhonghua yagao"
        }
      }
    ]
  }
}

bool查询

bool:布尔值查询

  • must:且操作。一个must下可以有多个match,必须同时满足
  • must_not:非操作。多个条件,都不满足
  • should:或操作。至少一个满足
  • filter:过滤操作。子句查询。

以上3种操作的操作对象是match匹配

GET /ecommerce/product/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "name": "yagao"
          }
        },
        {
          "match": {
            "price": "40"
          }
        }
      ]
    }
  }
}

3、query filter

一个查询语句究竟具有什么样的行为和得到什么结果,主要取决于它到底是处Query还是Filter。两者有很大区别:

Query context 查询上下文 这种语句在执行时既要计算文档是否匹配,还要计算文档相对于其他文档的匹配度有多高,匹配度越高,_score 分数就越高

Filter context 过滤上下文 过滤上下文中的语句在执行时只关心文档是否和查询匹配,不会计算匹配度,也就是得分

示例1:filter

搜索商品名称包含yagao,而且售价大于25元的商品

GET ecommerce/product/_search
{
  "query": {
    "bool": {
      "must": {
        "match": {
          "name": "yagao"
        }
      },
      "filter": {
        "range": {
          "price": {
            "gte": 25
          }
        }
      }
    }
  }
}

4、full-text search(全文检索)

示例1:分析全文解锁的过程

GET /ecommerce/product/_search
{
  "query": {
    "match": {
      "producer": "yagao producer"
    }
  }
}

producer这个字段,会先被拆解,建立倒排索引:

分词doc_id
special4
yagao4
producer1,2,3,4
gaolujie1
zhognhua3
jiajieshi2

搜索字符串也会被分词:yagao producer —> yagao和producer

所以producer字段包含yagao或producer都会被检索到。

结果:

{
  "took": 4,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "failed": 0
  },
  "hits": {
    "total": 3,
    "max_score": 0.25811607,
    "hits": [
      {
        "_index": "ecommerce",
        "_type": "product",
        "_id": "2",
        "_score": 0.25811607,
        "_source": {
          "name": "jiajieshi yagao",
          "desc": "youxiao fangzhu",
          "price": 25,
          "producer": "jiajieshi producer",
          "tags": [
            "fangzhu"
          ]
        }
      },
      {
        "_index": "ecommerce",
        "_type": "product",
        "_id": "1",
        "_score": 0.25811607,
        "_source": {
          "name": "gaolujie yagao",
          "desc": "gaoxiao meibai",
          "price": 30,
          "producer": "gaolujie producer",
          "tags": [
            "meibai",
            "fangzhu"
          ]
        }
      },
      {
        "_index": "ecommerce",
        "_type": "product",
        "_id": "3",
        "_score": 0.25811607,
        "_source": {
          "name": "zhonghua yagao",
          "desc": "caoben zhiwu",
          "price": 40,
          "producer": "zhonghua producer",
          "tags": [
            "qingxin"
          ]
        }
      }
    ]
  }
}

5、phrase search(短语搜索)

全文检索会将输入的搜索串拆解开来,去倒排索引里面去一一匹配,只要能匹配上任意一个拆解后的单词,就可以作为结果返回; 跟全文检索相反,phrase search,要求输入的搜索串是一个不可分割的整体,必须在指定的字段文本中,完全包含一模一样的,才可以算匹配,才能作为结果返回。

// 插入一条临时数据
PUT /ecommerce/product/4
{
  "name": "special yagao",
  "desc": "special meibai",
  "price": 50,
  "producer": "special yagao producer",
  "tags": [
    "meibai"
  ]
}

示例:

GET /ecommerce/product/_search
{
  "query": {
    "match_phrase": {
      "producer": "yagao producer"
    }
  }
}

结果:

{
  "took": 22,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "failed": 0
  },
  "hits": {
    "total": 1,
    "max_score": 0.70293105,
    "hits": [
      {
        "_index": "ecommerce",
        "_type": "product",
        "_id": "4",
        "_score": 0.70293105,
        "_source": {
          "name": "special yagao",
          "desc": "special meibai",
          "price": 50,
          "producer": "special yagao producer",
          "tags": [
            "meibai"
          ]
        }
      }
    ]
  }
}

term精确查询

term查询是直接通过倒排索引指定的词条进程精确查找的!

示例1

GET /ecommerce/product/_search
{
  "query": {
    "term": {
      "name": "yagao"
    }
  }
}

结果:

{
  "took": 3,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "failed": 0
  },
  "hits": {
    "total": 4,
    "max_score": 0.25811607,
    "hits": [
      {
        "_index": "ecommerce",
        "_type": "product",
        "_id": "1",
        "_score": 0.25811607,
        "_source": {
          "name": "gaolujie yagao",
          "desc": "gaoxiao meibai",
          "price": 30,
          "producer": "gaolujie producer",
          "tags": [
            "meibai",
            "fangzhu"
          ]
        }
      },
      {
        "_index": "ecommerce",
        "_type": "product",
        "_id": "3",
        "_score": 0.25811607,
        "_source": {
          "name": "zhonghua yagao",
          "desc": "caoben zhiwu",
          "price": 40,
          "producer": "zhonghua producer",
          "tags": [
            "qingxin"
          ]
        }
      },
      {
        "_index": "ecommerce",
        "_type": "product",
        "_id": "2",
        "_score": 0.16358379,
        "_source": {
          "name": "jiajieshi yagao",
          "desc": "youxiao fangzhu",
          "price": 25,
          "producer": "jiajieshi producer",
          "tags": [
            "fangzhu"
          ]
        }
      },
      {
        "_index": "ecommerce",
        "_type": "product",
        "_id": "4",
        "_score": 0.16358379,
        "_source": {
          "name": "special yagao",
          "desc": "special meibai",
          "price": 50,
          "producer": "special yagao producer",
          "tags": [
            "meibai"
          ]
        }
      }
    ]
  }
}

示例2

GET /ecommerce/product/_search
{
  "query": {
    "term": {
      "name": "gaolujie yagao"
    }
  }
}

结果:

{
  "took": 1,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "failed": 0
  },
  "hits": {
    "total": 0,
    "max_score": null,
    "hits": []
  }
}

发现没有结果,因为“gaolujie yagao”不是一个分词,不会存在倒排索引中。这种情况可以使用match_phrase查询。

6、highlight search(高亮搜索结果)

示例

GET /ecommerce/product/_search
{
  "query": {
    "match": {
      "producer": "producer"
    }
  },
  "highlight": {
    "fields": {
      "producer":{} //此处值任意
    }
  },
  "size": 1  //只显示1条记录
}
{
  "took": 7,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "failed": 0
  },
  "hits": {
    "total": 4,
    "max_score": 0.25811607,
    "hits": [
      {
        "_index": "ecommerce",
        "_type": "product",
        "_id": "1",
        "_score": 0.25811607,
        "_source": {
          "name": "gaolujie yagao",
          "desc": "gaoxiao meibai",
          "price": 30,
          "producer": "gaolujie producer",
          "tags": [
            "meibai",
            "fangzhu"
          ]
        },
        "highlight": {
          "producer": [
            "gaolujie <em>producer</em>" //被添加了html标签
          ]
        }
      }
    ]
  }
}

总结

query

  • match 匹配查询
  • bool:布尔值查询
    • must:且操作。一个must下可以有多个match,必须同时满足
    • must_not:非操作。
    • should:或操作。
    • filter