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的结果可以缓存到内存中,方便再用


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

潇潇雨歇_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值