elasticsearch學習筆記(六)——快速入門案例實戰之電商網站商品管理:多種搜索方式

簡單介紹一下ES的多種搜索方式json

一、query string search

格式:curl

GET /{index}/_search
GET /product/_search
{
  "took" : 1,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 2,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "product",
        "_type" : "_doc",
        "_id" : "2",
        "_score" : 1.0,
        "_source" : {
          "name" : "jiajieshi yagao",
          "desc" : "youxiao fangzhu",
          "price" : 25,
          "producer" : "jiajieshi producer",
          "tags" : [
            "fangzhu"
          ]
        }
      },
      {
        "_index" : "product",
        "_type" : "_doc",
        "_id" : "3",
        "_score" : 1.0,
        "_source" : {
          "name" : "zhonghua yagao",
          "desc" : "caoben zhiwu",
          "price" : 40,
          "producer" : "zhonghua producer",
          "tags" : [
            "qingxin"
          ]
        }
      }
    ]
  }
}

簡單見一下查詢結果的各個值的含義:
took:耗費的時間 單位是毫秒
timed_out:是否超時
_shards: total是指打到的primary shard(或者replica shard)的個數,successful是指查詢成功的分片數,skipped是指跳過的分片個數,failed是指查詢失敗的分片的個數
hits.total:value表明查詢匹配的總數,relation表明The count is accurate (e.g. "eq" means equals).
hits.max_score:是指匹配的文檔中相關度分數最高的
hits.hits:包含匹配搜索的document的詳細數據工具

爲何叫作query string search ,主要是由於search參數都是以http請求的query string來附帶的
例如搜索商品名稱中包含yagao的商品,並且按照售價降序排列:url

GET /product/_search?q=name:yagao&sort=price:desc
{
  "took" : 36,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 2,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : [
      {
        "_index" : "product",
        "_type" : "_doc",
        "_id" : "3",
        "_score" : null,
        "_source" : {
          "name" : "zhonghua yagao",
          "desc" : "caoben zhiwu",
          "price" : 40,
          "producer" : "zhonghua producer",
          "tags" : [
            "qingxin"
          ]
        },
        "sort" : [
          40
        ]
      },
      {
        "_index" : "product",
        "_type" : "_doc",
        "_id" : "2",
        "_score" : null,
        "_source" : {
          "name" : "jiajieshi yagao",
          "desc" : "youxiao fangzhu",
          "price" : 25,
          "producer" : "jiajieshi producer",
          "tags" : [
            "fangzhu"
          ]
        },
        "sort" : [
          25
        ]
      }
    ]
  }
}

query string search適用於臨時的在命令行使用的一些工具,好比curl,快速發出請求,來檢索想要的信息。可是若是查詢請求很複雜,就很難去構建搜索條件,在生產環境中不多使用。命令行

二、query DSL

什麼叫作DSL?
DSL:Domain Specified Language 特定領域語言
使用query DSL 查詢時查詢的參數採用的是請求體(http request body),能夠用json的格式來構建查詢語法,比較方便,能夠構建各類複雜的語法。比query string search 確定是強大多了
格式:code

GET /{index}/{type}/_search
{
    "json數據"
}

下面都是實際的一些例子:
查詢全部的商品:排序

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

查詢名稱中包含yagao的商品,同時按照價格降序排序:索引

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

分頁查詢商品,總共3個商品,假設每一頁就顯示1條商品,如今顯示第2頁,因此就查出來第2個商品ip

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

指定要查詢出來的商品只返回名稱和價格,也就是定製返回字段ci

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

query DSL 更加適合生產環境使用,能夠構建複雜的查詢

三、query filter

搜索商品名稱包含yagao,並且售價大於25元的商品

GET /product/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "name": "yagao"
          }
        }
      ],
      "filter": {
        "range": {
          "price": {
            "gt": 25
          }
        }
      }
    }
  }
}

四、full-text search

GET /product/_search
{
  "query": {
    "match": {
      "producer": "jiajieshi producer"
    }
  }
}
{
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 2,
      "relation" : "eq"
    },
    "max_score" : 0.18232156,
    "hits" : [
      {
        "_index" : "product",
        "_type" : "_doc",
        "_id" : "2",
        "_score" : 0.18232156,
        "_source" : {
          "name" : "jiajieshi yagao",
          "desc" : "youxiao fangzhu",
          "price" : 25,
          "producer" : "jiajieshi producer",
          "tags" : [
            "fangzhu"
          ]
        }
      },
      {
        "_index" : "product",
        "_type" : "_doc",
        "_id" : "3",
        "_score" : 0.18232156,
        "_source" : {
          "name" : "zhonghua yagao",
          "desc" : "caoben zhiwu",
          "price" : 40,
          "producer" : "zhonghua producer",
          "tags" : [
            "qingxin"
          ]
        }
      }
    ]
  }
}

爲何連zhonghua producer這個文檔也被檢索出來了,緣由是producer這個字段一開始插入數據的時候,就會被拆解,創建倒排索引
jiajieshi 1
zhonghua 2
producer 1,2
搜索yagao producer的時候,會進行拆分變成yagao和producer

五、phrase search短語搜索

phrase search 跟全文檢索相反,全文檢索會將輸入的搜索串拆解開來,去倒排索引裏面一一去匹配,只要能匹配上任意一個拆解後的單詞,就能夠做爲結果返回。可是phrase search要求輸入的搜索串,必須在指定的字段文本中,徹底包含如出一轍的,才能夠算匹配上了,做爲結果返回。

GET /product/_search
{
  "query": {
    "match_phrase": {
      "producer": "jiajieshi producer"
    }
  }
}
{
  "took" : 4,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 0.87546873,
    "hits" : [
      {
        "_index" : "product",
        "_type" : "_doc",
        "_id" : "2",
        "_score" : 0.87546873,
        "_source" : {
          "name" : "jiajieshi yagao",
          "desc" : "youxiao fangzhu",
          "price" : 25,
          "producer" : "jiajieshi producer",
          "tags" : [
            "fangzhu"
          ]
        }
      }
    ]
  }
}

六、highlight search高亮搜索

GET /product/_search
{
  "query": {
    "match_phrase": {
      "producer": "jiajieshi producer"
    }
  },
  "highlight": {
    "fields": {
      "producer":{}
    }
  }
}
{
  "took" : 23,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 0.87546873,
    "hits" : [
      {
        "_index" : "product",
        "_type" : "_doc",
        "_id" : "2",
        "_score" : 0.87546873,
        "_source" : {
          "name" : "jiajieshi yagao",
          "desc" : "youxiao fangzhu",
          "price" : 25,
          "producer" : "jiajieshi producer",
          "tags" : [
            "fangzhu"
          ]
        },
        "highlight" : {
          "producer" : [
            "<em>jiajieshi</em> <em>producer</em>"
          ]
        }
      }
    ]
  }
}
相關文章
相關標籤/搜索