Elasticsearch 7.x 【02】

主要内容:7种搜索方式的简单操作

1、query string search
2、query DSL
3、query filter
4、full-text search
5、phrase search
6、highlight search
7、term query

1、query string search

名称由来:search参数都是以http请求的query string来附带的

参数列表

搜索示例:
搜索全部商品

GET /ecommerce/_search
{
  "took" : 11,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 5,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : [
      {
        "_index" : "ecommerce",
        "_type" : "_doc",
        "_id" : "SH1w83ABZ60GR8nZv1rN",
        "_score" : null,
        "_source" : {
          "name" : "zhonghua yagao",
          "desc" : "caoben zhiwu",
          "price" : 40,
          "producer" : "zhonghua producer",
          "tags" : [
            "qingxin"
          ]
        },
        "sort" : [
          40
        ]
      }      
    ]
  }
}

字段说明:

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

搜索名为yagao的商品,并按价格进行降序排列

GET /ecommerce/_search?q=name:yagao&sort=price:desc

小结

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

2、query DSL

DSL:Domain Specified Language,特定领域的语言
http request body:请求体,可以用json的格式来构建查询语法,比较方便,可以构建各种复杂的语法.

查询示例:

GET /ecommerce/_search
{
  "query": {
    "match_all": {}
  }
}

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

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

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

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

指定查询出来商品的名称和价格 类似sql语句的select

GET /ecommerce/_search
{
  "query": {
    "match_all": {}
  },
  "_source": [
    "name",
    "price"
  ]
}
{
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 5,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "ecommerce",
        "_type" : "_doc",
        "_id" : "2",
        "_score" : 1.0,
        "_source" : {
          "price" : 25,
          "name" : "jiajieshi yagao"
        }
      },
      {
        "_index" : "ecommerce",
        "_type" : "_doc",
        "_id" : "SH1w83ABZ60GR8nZv1rN",
        "_score" : 1.0,
        "_source" : {
          "price" : 40,
          "name" : "zhonghua yagao"
        }
      }
    ]
  }
}

3、query filter

对数据进行过滤

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

(gt:大于,lt:小于,gte:大于等于,lte:小于等于)

GET /ecommerce/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "name": "yagao"
          }
        }
      ],
      "filter": {
        "range": {
          "price": {
            "gte": 40
          }
        }
      }
    }
  },
  "sort": [
    {
      "price": {
        "order": "desc"
      }
    }
  ]
}

返回的结果

{
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 0.03390155,
    "hits" : [
      {
        "_index" : "ecommerce",
        "_type" : "_doc",
        "_id" : "SH1w83ABZ60GR8nZv1rN",
        "_score" : 0.03390155,
        "_source" : {
          "name" : "zhonghua yagao",
          "desc" : "caoben zhiwu",
          "price" : 40,
          "producer" : "zhonghua producer",
          "tags" : [
            "qingxin"
          ]
        }
      }
    ]
  }
}

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

全文检索会将输入的搜索串拆解开来,去倒排索引里面去逐一匹配,只要能匹配上任意一个拆解后的单词,就可以作为结果返回。

返回的结果会按照匹配度进行排序

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

返回的结果

{
  "took" : 36,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 1.395052,
    "hits" : [
      {
        "_index" : "ecommerce",
        "_type" : "_doc",
        "_id" : "sBkH-3ABDLwU_RgbxhX0",
        "_score" : 1.395052,
        "_source" : {
          "name" : "special yagao",
          "desc" : "special meibai",
          "price" : 50,
          "producer" : "special yagao producer",
          "tags" : [
            "meibai"
          ]
        }
      }
    ]
  }
}

5、phrase search(短语搜索)

跟全文检索相对应,要求输入的搜索串,必须在指定的字段文本中,完全包含一模一样的,才可以算匹配,才能作为结果返回

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

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

对匹配的producer字段进行高亮显示

GET /ecommerce/_search
{
  "query": {
    "match": {
      "producer": "yagao"
    }
  },
  "highlight": {
    "fields": {
      "producer": {}
    }
  }
}

返回结果

{
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 1.3310189,
    "hits" : [
      {
        "_index" : "ecommerce",
        "_type" : "_doc",
        "_id" : "sBkH-3ABDLwU_RgbxhX0",
        "_score" : 1.3310189,
        "_source" : {
          "name" : "special yagao",
          "desc" : "special meibai",
          "price" : 50,
          "producer" : "special yagao producer",
          "tags" : [
            "meibai"
          ]
        },
        "highlight" : {
          "producer" : [
            "special <em>yagao</em> producer"
          ]
        }
      }
    ]
  }
}

7、term query( 完全匹配 )

term是代表完全匹配,即不进行分词器分析,文档中必须包含整个搜索的词汇

GET /ecommerce/_search
{
  "query": {
    "term": {
      "producer": "special"
    }
  }
}

(terms多值)字段有一多个值时候,用terms关键词查询,后跟数组

GET /ecommerce/_search
{
  "query": {
    "terms": {
      "producer": [
        "special",
        "producer"
      ]
    }
  }
}

 

 

 

 

 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章