1. 程式人生 > >Elasticsearch Essentials——Elasticsearch查詢操作

Elasticsearch Essentials——Elasticsearch查詢操作

Query string

查詢可以將需要查詢的條件組裝在一起形成字串,來進行復雜的資料查詢。例如GET /oa/employee/_search?q=gender:male&sort=age:desc,這串查詢的結果就是以age為降序排序的所有的male員工。查詢結果為

{
  "took": 7,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "failed": 0
  },
  "hits": {
    "total": 4,
    "max_score": null,
    "hits": [
      {
        "_index": "oa",
        "_type": "employee",
        "_id": "4",
        "_score": null,
        "_source": {
          "name": "li zong rui",
          "age": 37,
          "gender": "male",
          "hobby": [
            "basketball"
          ],
          "address": {
            "province": "beijing",
            "city": "chaoyangqu",
            "county": "chaoyangqu",
            "details": "chaoyanglu"
          }
        },
        "sort": [
          37
        ]
      },
      {
        "_index": "oa",
        "_type": "employee",
        "_id": "1",
        "_score": null,
        "_source": {
          "name": "wang kai",
          "age": 34,
          "gender": "male",
          "hobby": [
            "basketball",
            "football"
          ],
          "address": {
            "province": "jiangsu",
            "city": "yancheng",
            "county": "xiangshui",
            "details": "hepingjie"
          }
        },
        "sort": [
          34
        ]
      },
      {
        "_index": "oa",
        "_type": "employee",
        "_id": "3",
        "_score": null,
        "_source": {
          "name": "li si",
          "age": 30,
          "gender": "male",
          "hobby": [
            "basketball"
          ],
          "address": {
            "province": "shandong",
            "city": "jinan",
            "county": "lixiaqu",
            "details": "yulanguanchang"
          }
        },
        "sort": [
          30
        ]
      },
      {
        "_index": "oa",
        "_type": "employee",
        "_id": "5",
        "_score": null,
        "_source": {
          "name": "jiang kang jian",
          "age": 25,
          "gender": "male",
          "hobby": [
            "basketball"
          ],
          "address": {
            "province": "jiangsu",
            "city": "suzhou",
            "county": "huqiuqu",
            "details": "disanlu"
          }
        },
        "sort": [
          25
        ]
      }
    ]
  }
}

Query DSL

查詢所有的員工

GET /oa/employee/_search
{
  "query": {
    "match_all": {}
  }
}

改寫query string

GET /oa/employee/_search
{
  "query": {
    "match": {
      "gender": "male"
    }
  },
  "sort": [
    {
      "age": "desc"
    }
  ]
}

分頁查詢

GET /oa/employee/_search
{
  "query": {
    "match_all": {}
  },
  "from": 0,
  "size": 2
}

查詢結果

{
  "took": 1,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "failed": 0
  },
  "hits": {
    "total": 5,
    "max_score": 1,
    "hits": [
      {
        "_index": "oa",
        "_type": "employee",
        "_id": "5",
        "_score": 1,
        "_source": {
          "name": "jiang kang jian",
          "age": 25,
          "gender": "male",
          "hobby": [
            "basketball"
          ],
          "address": {
            "province": "jiangsu",
            "city": "suzhou",
            "county": "huqiuqu",
            "details": "disanlu"
          }
        }
      },
      {
        "_index": "oa",
        "_type": "employee",
        "_id": "4",
        "_score": 1,
        "_source": {
          "name": "li zong rui",
          "age": 37,
          "gender": "male",
          "hobby": [
            "basketball"
          ],
          "address": {
            "province": "beijing",
            "city": "chaoyangqu",
            "county": "chaoyangqu",
            "details": "chaoyanglu"
          }
        }
      }
    ]
  }
}

全文檢索

GET /oa/employee/_search
{
  "query": {
    "match": {
      "name": "wang yuan yuan"
    }
  }
}

查詢結果

{
  "took": 10,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "failed": 0
  },
  "hits": {
    "total": 2,
    "max_score": 2.3527396,
    "hits": [
      {
        "_index": "oa",
        "_type": "employee",
        "_id": "2",
        "_score": 2.3527396,
        "_source": {
          "name": "wang yuan yuan",
          "age": 29,
          "gender": "female",
          "hobby": [
            "games",
            "dancing"
          ],
          "address": {
            "province": "shandong",
            "city": "jinan",
            "county": "zhangqiu",
            "details": "moalingshanlu"
          }
        }
      },
      {
        "_index": "oa",
        "_type": "employee",
        "_id": "1",
        "_score": 0.25811607,
        "_source": {
          "name": "wang kai",
          "age": 34,
          "gender": "male",
          "hobby": [
            "basketball",
            "football"
          ],
          "address": {
            "province": "jiangsu",
            "city": "yancheng",
            "county": "xiangshui",
            "details": "hepingjie"
          }
        }
      }
    ]
  }
}

Query phrase

GET /oa/employee/_search
{
  "query": {
    "match_phrase": {
      "name": "wang yuan yuan"
    }
  }
}

查詢結果

{
  "took": 38,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "failed": 0
  },
  "hits": {
    "total": 1,
    "max_score": 1.8299086,
    "hits": [
      {
        "_index": "oa",
        "_type": "employee",
        "_id": "2",
        "_score": 1.8299086,
        "_source": {
          "name": "wang yuan yuan",
          "age": 29,
          "gender": "female",
          "hobby": [
            "games",
            "dancing"
          ],
          "address": {
            "province": "shandong",
            "city": "jinan",
            "county": "zhangqiu",
            "details": "moalingshanlu"
          }
        }
      }
    ]
  }
}

總結

full_text query和phrase query的區別是:full_text query會把需要查詢的資料拆分匹配,例如這裡面搜尋的是wang yuan yuan,es會把wang,yuan,yuan,wang yuan等一系列排列組合放進去搜索,匹配到wang yuan yuan的是分數最高的,一些其他的會根據匹配的依次降分,所以全文檢索出來的資料會比phrase query出來的資料多。而phrase query就是對單個term的完全匹配。