1. 程式人生 > >Kibana+ElasticSearch實現索引資料的幾種查詢方式

Kibana+ElasticSearch實現索引資料的幾種查詢方式

 1.match_all搜尋,直接返回所有文件

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

 返回結果大致如下:
{
  "took": 13,
  "timed_out": false,
  "_shards": {
    "total": 3,
    "successful": 3,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 23,
    "max_score": 1,
    "hits": [
      {
        "_index": "school",
        "_type": "student",
        "_id": "b3ffcWIB-npqvsX5SmVm",
        "_score": 1,
        "_source": {
          "aggs": {
            "group_by_word_count": {
              "terms": {
                "field": "word_count"
              }
            }
          }
        }
      },
      {
        "_index": "school",
        "_type": "student",
        "_id": "c3fjcWIB-npqvsX5G2Wh",
        "_score": 1,
        "_source": {
          "aggs": {
            "grades_word_count": {
              "stats": {
                "field": "word_count"
              }
            }
          }
        }
      },
      {
        "_index": "school",
        "_type": "student",
        "_id": "dHfjcWIB-npqvsX5uWWr",
        "_score": 1,
        "_source": {
          "aggs": {
            "grades_word_count": {
              "min": {
                "field": "word_count"
              }
            }
          }
        }
      },
      {
        "_index": "school",
        "_type": "student",
        "_id": "dXfkcWIB-npqvsX5hmWx",
        "_score": 1,
        "_source": {
          "query": {
            "match": {
              "name": "海哥"
            }
          }
        }
      },
      {
        "_index": "school",
        "_type": "student",
        "_id": "dnflcWIB-npqvsX5S2V0",
        "_score": 1,
        "_source": {
          "query": {
            "multi_match": {
              "query": "海哥",
              "fields": [
                "name",
                "address"
              ]
            }
          }
        }
      },
      {
        "_index": "school",
        "_type": "student",
        "_id": "fXfqcWIB-npqvsX5yGXf",
        "_score": 1,
        "_source": {
          "query": {
            "bool": {
              "filter": {
                "term": {
                  "word_count": 2000
                }
              }
            }
          }
        }
      },
      {
        "_index": "school",
        "_type": "student",
        "_id": "fnfrcWIB-npqvsX5mGXq",
        "_score": 1,
        "_source": {
          "query": {
            "constant_score": {
              "filter": {
                "match": {
                  "title": "ElasticSearch"
                }
              },
              "boost": 2
            }
          }
        }
      },
      {
        "_index": "school",
        "_type": "student",
        "_id": "gHfucWIB-npqvsX5HWUB",
        "_score": 1,
        "_source": {
          "query": {
            "bool": {
              "must_not": [
                {
                  "term": {
                    "word_count": "2000"
                  }
                }
              ]
            }
          }
        }
      },
      {
        "_index": "school",
        "_type": "student",
        "_id": "HYVJOGIBUtf8tEPshwDC",
        "_score": 1,
        "_source": {
          "name": "張小花",
          "address": "山東煙臺",
          "age": 24,
          "date": "1996-07-24"
        }
      },
      {
        "_index": "school",
        "_type": "student",
        "_id": "cHffcWIB-npqvsX59mXN",
        "_score": 1,
        "_source": {
          "aggs": {
            "group_by_word_count": {
              "terms": {
                "field": "word_count"
              }
            }
          }
        }
      }
    ]
  }
}

 引數大致解釋:
  • took: 執行搜尋耗時,毫秒為單位
  • time_out: 搜尋是否超時
  • _shards: 多少分片被搜尋,成功多少,失敗多少
  • hits: 搜尋結果展示
  • hits.total: 匹配條件的文件總數
  • hits.hits: 返回結果展示,預設返回十個
  • hits.max_score:最大匹配得分
  • hits._score: 返回文件的匹配得分(得分越高,匹配程度越高,越靠前)
  • _index _type _id 作為剝層定位到特定的文件
  • _source 文件源

 2.執行查詢

2.1 只顯示name和address

POST /school/_search
{
  "query": { "match_all": {} },
  "_source": ["name", "address"]
}

查詢結果:

{
  "took": 313,
  "timed_out": false,
  "_shards": {
    "total": 3,
    "successful": 3,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 23,
    "max_score": 1,
    "hits": [
      {
        "_index": "school",
        "_type": "student",
        "_id": "b3ffcWIB-npqvsX5SmVm",
        "_score": 1,
        "_source": {}
      },
      {
        "_index": "school",
        "_type": "student",
        "_id": "c3fjcWIB-npqvsX5G2Wh",
        "_score": 1,
        "_source": {}
      },
      {
        "_index": "school",
        "_type": "student",
        "_id": "dHfjcWIB-npqvsX5uWWr",
        "_score": 1,
        "_source": {}
      },
      {
        "_index": "school",
        "_type": "student",
        "_id": "dXfkcWIB-npqvsX5hmWx",
        "_score": 1,
        "_source": {}
      },
      {
        "_index": "school",
        "_type": "student",
        "_id": "dnflcWIB-npqvsX5S2V0",
        "_score": 1,
        "_source": {}
      },
      {
        "_index": "school",
        "_type": "student",
        "_id": "fXfqcWIB-npqvsX5yGXf",
        "_score": 1,
        "_source": {}
      },
      {
        "_index": "school",
        "_type": "student",
        "_id": "fnfrcWIB-npqvsX5mGXq",
        "_score": 1,
        "_source": {}
      },
      {
        "_index": "school",
        "_type": "student",
        "_id": "gHfucWIB-npqvsX5HWUB",
        "_score": 1,
        "_source": {}
      },
      {
        "_index": "school",
        "_type": "student",
        "_id": "HYVJOGIBUtf8tEPshwDC",
        "_score": 1,
        "_source": {
          "address": "山東煙臺",
          "name": "張小花"
        }
      },
      {
        "_index": "school",
        "_type": "student",
        "_id": "cHffcWIB-npqvsX59mXN",
        "_score": 1,
        "_source": {}
      }
    ]
  }
}

 2.2 返回name為haige的document
POST /school/_search
{
  "query": { "match": { "name": "張小花" } }
}
  查詢結果:
{
  "took": 439,
  "timed_out": false,
  "_shards": {
    "total": 3,
    "successful": 3,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 2,
    "max_score": 2.634553,
    "hits": [
      {
        "_index": "school",
        "_type": "student",
        "_id": "bXfXcWIB-npqvsX5w2Vc",
        "_score": 2.634553,
        "_source": {
          "name": "張小花",
          "address": "山東煙臺",
          "age": 24,
          "date": "1996-07-24"
        }
      },
      {
        "_index": "school",
        "_type": "student",
        "_id": "HYVJOGIBUtf8tEPshwDC",
        "_score": 0.8630463,
        "_source": {
          "name": "張小花",
          "address": "山東煙臺",
          "age": 24,
          "date": "1996-07-24"
        }
      }
    ]
  }
}

 2.3 返回name包含"海"的所有document
POST /school/_search
{
  "query": { "match": { "name": "海" } }
}

  查詢結果:
{
  "took": 68,
  "timed_out": false,
  "_shards": {
    "total": 3,
    "successful": 3,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 1,
    "max_score": 1.0417081,
    "hits": [
      {
        "_index": "school",
        "_type": "student",
        "_id": "3",
        "_score": 1.0417081,
        "_source": {
          "name": "海哥",
          "address": "山東濟寧",
          "age": 27,
          "date": "1998-03-16"
        }
      }
    ]
  }
}

 2.4 返回name中包含term "海" 或 "花" 的所有document
POST /school/_search
{
  "query": { "match": { "name": "海 花" } }
}

{
  "took": 26,
  "timed_out": false,
  "_shards": {
    "total": 3,
    "successful": 3,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 3,
    "max_score": 1.0417081,
    "hits": [
      {
        "_index": "school",
        "_type": "student",
        "_id": "3",
        "_score": 1.0417081,
        "_source": {
          "name": "海哥",
          "address": "山東濟寧",
          "age": 27,
          "date": "1998-03-16"
        }
      },
      {
        "_index": "school",
        "_type": "student",
        "_id": "bXfXcWIB-npqvsX5w2Vc",
        "_score": 0.8781843,
        "_source": {
          "name": "張小花",
          "address": "山東煙臺",
          "age": 24,
          "date": "1996-07-24"
        }
      },
      {
        "_index": "school",
        "_type": "student",
        "_id": "HYVJOGIBUtf8tEPshwDC",
        "_score": 0.2876821,
        "_source": {
          "name": "張小花",
          "address": "山東煙臺",
          "age": 24,
          "date": "1996-07-24"
        }
      }
    ]
  }
}
 2.5 匹配phrase "海 花"
POST /school/_search
{
 "query": { "match_phrase": { "name": "海 花" }}
}

  查詢結果:
{
  "took": 5,
  "timed_out": false,
  "_shards": {
    "total": 3,
    "successful": 3,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 0,
    "max_score": null,
    "hits": []
  }
}

 2.6 返回name中包含"海"和"哥"的所有賬戶(AND)
POST /school/_search
{
  "query": {
    "bool": {
      "must": [
        { "match": { "name": "海" } },
        { "match": { "name": "哥" } }
      ]
    }
  }
}

  查詢結果:
{
  "took": 208,
  "timed_out": false,
  "_shards": {
    "total": 3,
    "successful": 3,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 1,
    "max_score": 2.0834162,
    "hits": [
      {
        "_index": "school",
        "_type": "student",
        "_id": "3",
        "_score": 2.0834162,
        "_source": {
          "name": "海哥",
          "address": "山東濟寧",
          "age": 27,
          "date": "1998-03-16"
        }
      }
    ]
  }
}

 2.7 返回name中包含"海"或"花"的所有document
POST /school/_search
{
  "query": {
    "bool": {
      "should": [
        { "match": { "name": "海" } },
        { "match": { "name": "花" } }
      ]
    }
  }
}

 查詢結果:
{
  "took": 19,
  "timed_out": false,
  "_shards": {
    "total": 3,
    "successful": 3,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 3,
    "max_score": 1.0417081,
    "hits": [
      {
        "_index": "school",
        "_type": "student",
        "_id": "3",
        "_score": 1.0417081,
        "_source": {
          "name": "海哥",
          "address": "山東濟寧",
          "age": 27,
          "date": "1998-03-16"
        }
      },
      {
        "_index": "school",
        "_type": "student",
        "_id": "bXfXcWIB-npqvsX5w2Vc",
        "_score": 0.8781843,
        "_source": {
          "name": "張小花",
          "address": "山東煙臺",
          "age": 24,
          "date": "1996-07-24"
        }
      },
      {
        "_index": "school",
        "_type": "student",
        "_id": "HYVJOGIBUtf8tEPshwDC",
        "_score": 0.2876821,
        "_source": {
          "name": "張小花",
          "address": "山東煙臺",
          "age": 24,
          "date": "1996-07-24"
        }
      }
    ]
  }
}

  2.8 查詢name中既不包含"海",也不包含"哥"的所有document
POST /school/_search
{
  "query": {
    "bool": {
      "must_not": [
        { "match": { "name": "海" } },
        { "match": { "name": "哥" } }
      ]
    }
  }
}

 查詢結果:
{
  "took": 264,
  "timed_out": false,
  "_shards": {
    "total": 3,
    "successful": 3,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 22,
    "max_score": 1,
    "hits": [
      {
        "_index": "school",
        "_type": "student",
        "_id": "b3ffcWIB-npqvsX5SmVm",
        "_score": 1,
        "_source": {
          "aggs": {
            "group_by_word_count": {
              "terms": {
                "field": "word_count"
              }
            }
          }
        }
      },
      {
        "_index": "school",
        "_type": "student",
        "_id": "c3fjcWIB-npqvsX5G2Wh",
        "_score": 1,
        "_source": {
          "aggs": {
            "grades_word_count": {
              "stats": {
                "field": "word_count"
              }
            }
          }
        }
      },
      {
        "_index": "school",
        "_type": "student",
        "_id": "dHfjcWIB-npqvsX5uWWr",
        "_score": 1,
        "_source": {
          "aggs": {
            "grades_word_count": {
              "min": {
                "field": "word_count"
              }
            }
          }
        }
      },
      {
        "_index": "school",
        "_type": "student",
        "_id": "dXfkcWIB-npqvsX5hmWx",
        "_score": 1,
        "_source": {
          "query": {
            "match": {
              "name": "海哥"
            }
          }
        }
      },
      {
        "_index": "school",
        "_type": "student",
        "_id": "dnflcWIB-npqvsX5S2V0",
        "_score": 1,
        "_source": {
          "query": {
            "multi_match": {
              "query": "海哥",
              "fields": [
                "name",
                "address"
              ]
            }
          }
        }
      },
      {
        "_index": "school",
        "_type": "student",
        "_id": "fXfqcWIB-npqvsX5yGXf",
        "_score": 1,
        "_source": {
          "query": {
            "bool": {
              "filter": {
                "term": {
                  "word_count": 2000
                }
              }
            }
          }
        }
      },
      {
        "_index": "school",
        "_type": "student",
        "_id": "fnfrcWIB-npqvsX5mGXq",
        "_score": 1,
        "_source": {
          "query": {
            "constant_score": {
              "filter": {
                "match": {
                  "title": "ElasticSearch"
                }
              },
              "boost": 2
            }
          }
        }
      },
      {
        "_index": "school",
        "_type": "student",
        "_id": "gHfucWIB-npqvsX5HWUB",
        "_score": 1,
        "_source": {
          "query": {
            "bool": {
              "must_not": [
                {
                  "term": {
                    "word_count": "2000"
                  }
                }
              ]
            }
          }
        }
      },
      {
        "_index": "school",
        "_type": "student",
        "_id": "HYVJOGIBUtf8tEPshwDC",
        "_score": 1,
        "_source": {
          "name": "張小花",
          "address": "山東煙臺",
          "age": 24,
          "date": "1996-07-24"
        }
      },
      {
        "_index": "school",
        "_type": "student",
        "_id": "cHffcWIB-npqvsX59mXN",
        "_score": 1,
        "_source": {
          "aggs": {
            "group_by_word_count": {
              "terms": {
                "field": "word_count"
              }
            }
          }
        }
      }
    ]
  }
}

 2.9 返回name中包含"海",且地址不是"山東煙臺"的所有document
POST /school/_search
{
  "query": {
    "bool": {
      "must": [
        { "match": { "name": "海" } }
      ],
      "must_not": [
        { "match": { "address": "山東煙臺" } }
      ]
    }
  }
}

 查詢結果:
{
  "took": 73,
  "timed_out": false,
  "_shards": {
    "total": 3,
    "successful": 3,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 1,
    "max_score": 1.0417081,
    "hits": [
      {
        "_index": "school",
        "_type": "student",
        "_id": "3",
        "_score": 1.0417081,
        "_source": {
          "name": "海哥",
          "address": "山東濟寧",
          "age": 27,
          "date": "1998-03-16"
        }
      }
    ]
  }
}

 3. 過濾查詢

     3.1 在所有document中尋找age在0-25歲之間(閉區間)的學生

POST /school/_search
{
  "query": {
    "filtered": {
      "query": { "match_all": {} },
      "filter": {
        "range": {
          "age": {
            "gte": 0,
            "lte": 25
          }
        }
      }
    }
  }
}

4.談論query和filter的效率

   一般認為filter的速度快於query的速度 
   - filter不會計算相關度得分,效率高 
   - filter的結果可以快取到記憶體中,方便再用