Elasticsearch(六)多种搜索方式

query string search

语法格式:GET /Index/Type/_search

搜索全部商品:
GET /store/product/_search
返回结果:

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

搜索商品名称中包含 "yagao" 的商品并按照售价降序排序:
GET /store/product/_search?q=name:yagao&sort=price:desc
返回结果:

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

相关解释:

took:耗费了几毫秒
timed_out:是否超时
_shards:数据拆成了几个分片,这里是 5 个,所以对于搜索请求,会打到所有的 primary shard(或者是它的某个 replica shard 也可以)
hits.total:查询结果的数量
hits.max_score:score 就是 document 对于一个搜素的相关度匹配分数
hits.hits:包含了搜索匹配的 document 的详细数据

适用情况:
适用于临时在命令行中使用,比如使用 curl 工具快速的发出请求来检索想要的信息;但是如果查询请求变得复杂,是很难去构建 query string 的。在生产环境中,几乎很少使用这种方式。

query DSL

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

请求时会将请求数据放在请求体中(http request body),可以用 JSON 格式来构建查询的语法,比较方便,可以构建各种复杂的查询,比 query string search 强大。

搜索全部商品:

GET /store/product/_search
{
  "query": {
    "match_all": {}
  }
}

搜索商品名称中包含 "yagao" 的商品并按照售价降序排序:

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

或者

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

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

GET /store/product/_search
{
  "query": {
    "match_all": {}
  },
  "from": 1,
  "size": 1
}

指定查询字段,比如只查询 "name" 和 "price"

GET /store/product/_search
{
  "query": {
    "match_all": {}
  },
  "_source": ["name", "price"]
}
query filter

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

GET /store/product/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "name": "yagao"
          }
        }
      ], 
      "filter": {
        "range": {
          "price": {
            "gt": 25
          }
        }
      }
    }
  }
}
full-text search(全文检索)

首先新增一条商品:

PUT /store/product/4
{
  "name": "special yagao",
  "desc": "special meibai",
  "price": 50,
  "producer": "special yagao producer",
  "tags": [
    "meibai"
  ]
}

查询所有的商品:

GET /store/product/_search
{
  "query": {
    "match_all": {}
  }
}

返回结果:

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

搜索商品生产厂家包含 "yagao producer" 的商品:

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

返回结果:

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

说明:
语法看上去和 query DSL 没什么区别,重点在于 producer 这个字段。

现在有四个商品,producer 这个字段对应的值分别是:
"gaolujie producer"
"jiajieshi producer"
"zhonghua producer"
"special yagao producer"

对 4 个商品中的 producer 这个字段对应的值进行拆解,建立倒排索引,拆解结果如下:
special 4
yagao 4
producer 1,2,3,4
gaolujie 1
jiajieshi 2
zhonghua 3

搜索的是 "yagao producer",注意中间的空格,这时会拆分成两个词 "yagao" 和 "producer"。"yagao" 匹配 id 为 4 的商品,"producer" 匹配 id 为 1,2,3,4的商品,两个词都被匹配到了,所以 id 为 4 的商品 _score 最大。

Elasticsearch(六)多种搜索方式_第1张图片
phrase search(短语搜索)

跟全文检索相对应,相反,全文检索会将搜索字符串拆解开来,去倒排索引中进行一一匹配,只要能匹配上任意一个拆解后的单词,就可以作为结果返回。phrase search,要求搜索字符串必须在指定的字段文本中,完全包含一模一样的,才算匹配,才会作为结果返回。

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

返回结果:

{
  "took": 15,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "failed": 0
  },
  "hits": {
    "total": 1,
    "max_score": 0.70293105,
    "hits": [
      {
        "_index": "store",
        "_type": "product",
        "_id": "4",
        "_score": 0.70293105,
        "_source": {
          "name": "special yagao",
          "desc": "special meibai",
          "price": 50,
          "producer": "special yagao producer",
          "tags": [
            "meibai"
          ]
        }
      }
    ]
  }
}
highlight search(高亮搜索结果)

对查询结果中的 producer field 进行高亮

GET /store/product/_search
{
  "query": {
    "match": {
      "producer": "producer"
    }
  },
  "highlight": {
    "fields": {
      "producer": {}
    }
  }
}

返回结果:

{
  "took": 39,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "failed": 0
  },
  "hits": {
    "total": 4,
    "max_score": 0.25811607,
    "hits": [
      {
        "_index": "store",
        "_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 producer"
          ]
        }
      },
      {
        "_index": "store",
        "_type": "product",
        "_id": "3",
        "_score": 0.25811607,
        "_source": {
          "name": "zhonghua yagao",
          "desc": "caoben zhiwu",
          "price": 40,
          "producer": "zhonghua producer",
          "tags": [
            "qingxin"
          ]
        },
        "highlight": {
          "producer": [
            "zhonghua producer"
          ]
        }
      },
      {
        "_index": "store",
        "_type": "product",
        "_id": "2",
        "_score": 0.1805489,
        "_source": {
          "name": "jiajieshi yagao",
          "desc": "youxiao fangzhu",
          "price": 25,
          "producer": "jiajieshi producer",
          "tags": [
            "fangzhu"
          ]
        },
        "highlight": {
          "producer": [
            "jiajieshi producer"
          ]
        }
      },
      {
        "_index": "store",
        "_type": "product",
        "_id": "4",
        "_score": 0.14638957,
        "_source": {
          "name": "special yagao",
          "desc": "special meibai",
          "price": 50,
          "producer": "special yagao producer",
          "tags": [
            "meibai"
          ]
        },
        "highlight": {
          "producer": [
            "special yagao producer"
          ]
        }
      }
    ]
  }
}

你可能感兴趣的:(Elasticsearch(六)多种搜索方式)