四、ES文档操做命令(二)

参数查询

GET /student/_search
{
    "query": {
        "match": {
            "student-gender": "女"
        }
    }
}

返回结果如下

{
  "took": 1,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": {
      "value": 2,
      "relation": "eq"
    },
    "max_score": 0.6931471,
    "hits": [
      {
        "_index": "student",
        "_id": "3",
        "_score": 0.6931471,
        "_source": {
          "student-provice": "山东",
          "student-city": "济宁",
          "student-name": "冯程程",
          "student-gender": "女",
          "student-address": "高新区10689号",
          "student-age": 10,
          "student-total-score": 603.5,
          "student-birthday": "2003-08-09",
          "student-hobby": "唱歌、跳舞、下棋"
        }
      },
      {
        "_index": "student",
        "_id": "6",
        "_score": 0.6931471,
        "_source": {
          "student-provice": "山东",
          "student-city": "威海",
          "student-name": "宋丽君",
          "student-gender": "女",
          "student-address": "槐荫区10989号",
          "student-age": 12,
          "student-total-score": 596.5,
          "student-birthday": "2003-11-22",
          "student-hobby": "唱歌、跳舞、下棋"
        }
      }
    ]
  }
}

指定查询字段

GET /student/_search
{
  "query": {
    "match": {
      "student-gender": "女"
    }
  },
  "_source": [
    "student-name"
  ]
}

返回结果如下

{
  "took": 1,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": {
      "value": 2,
      "relation": "eq"
    },
    "max_score": 0.6931471,
    "hits": [
      {
        "_index": "student",
        "_id": "3",
        "_score": 0.6931471,
        "_source": {
          "student-name": "冯程程"
        }
      },
      {
        "_index": "student",
        "_id": "6",
        "_score": 0.6931471,
        "_source": {
          "student-name": "宋丽君"
        }
      }
    ]
  }
}

分页参数查询

GET /student/_search
{
  "query": {
    "match": {
      "student-gender": "女"
    }
  },
  "from": 0,
  "size": 2
}

返回结果如下

{
  "took": 1,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": {
      "value": 2,
      "relation": "eq"
    },
    "max_score": 0.6931471,
    "hits": [
      {
        "_index": "student",
        "_id": "3",
        "_score": 0.6931471,
        "_source": {
          "student-provice": "山东",
          "student-city": "济宁",
          "student-name": "冯程程",
          "student-gender": "女",
          "student-address": "高新区10689号",
          "student-age": 10,
          "student-total-score": 603.5,
          "student-birthday": "2003-08-09",
          "student-hobby": "唱歌、跳舞、下棋"
        }
      }
    ]
  }
}

排序查询

GET /student/_search
{
  "query": {
    "match_all": {}
  },
  "sort": [
    {
      "student-total-score": {
        "order": "asc"
      }
    }
  ]
}

返回结果如下

{
  "took": 11,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": {
      "value": 6,
      "relation": "eq"
    },
    "max_score": null,
    "hits": [
      {
        "_index": "student",
        "_id": "5",
        "_score": null,
        "_source": {
          "student-provice": "山东",
          "student-city": "聊城",
          "student-name": "刘英杰",
          "student-gender": "男",
          "student-address": "平阴县10001号",
          "student-age": 14,
          "student-total-score": 483.5,
          "student-birthday": "2003-02-13",
          "student-hobby": "唱歌、游泳、跑步、爬山、篮球、乒乓球"
        },
        "sort": [
          483.5
        ]
      },
      {
        "_index": "student",
        "_id": "1",
        "_score": null,
        "_source": {
          "student-provice": "山东",
          "student-city": "菏泽",
          "student-name": "张海剑",
          "student-gender": "男",
          "student-address": "历下区111号",
          "student-age": 11,
          "student-total-score": 540.5,
          "student-birthday": "2003-08-25",
          "student-hobby": "唱歌、游泳、跑步、骑行、爬山、篮球、乒乓球、足球、排球、跳舞、下棋、军事"
        },
        "sort": [
          540.5
        ]
      },
      {
        "_index": "student",
        "_id": "4",
        "_score": null,
        "_source": {
          "student-provice": "山东",
          "student-city": "济南",
          "student-name": "程诚",
          "student-gender": "男",
          "student-address": "历下区2869号",
          "student-age": 11,
          "student-total-score": 559.5,
          "student-birthday": "2004-06-09",
          "student-hobby": "唱歌、游泳、跑步、骑行、爬山、篮球、乒乓球、足球、排球、跳舞、下棋、军事"
        },
        "sort": [
          559.5
        ]
      },
      {
        "_index": "student",
        "_id": "2",
        "_score": null,
        "_source": {
          "student-provice": "山东",
          "student-city": "威海",
          "student-name": "张小明",
          "student-gender": "男",
          "student-address": "历城区10001号",
          "student-age": 12,
          "student-total-score": 583.5,
          "student-birthday": "2003-08-09",
          "student-hobby": "唱歌、游泳、跑步"
        },
        "sort": [
          583.5
        ]
      },
      {
        "_index": "student",
        "_id": "6",
        "_score": null,
        "_source": {
          "student-provice": "山东",
          "student-city": "威海",
          "student-name": "宋丽君",
          "student-gender": "女",
          "student-address": "槐荫区10989号",
          "student-age": 12,
          "student-total-score": 596.5,
          "student-birthday": "2003-11-22",
          "student-hobby": "唱歌、跳舞、下棋"
        },
        "sort": [
          596.5
        ]
      },
      {
        "_index": "student",
        "_id": "3",
        "_score": null,
        "_source": {
          "student-provice": "山东",
          "student-city": "济宁",
          "student-name": "冯程程",
          "student-gender": "女",
          "student-address": "高新区10689号",
          "student-age": 10,
          "student-total-score": 603.5,
          "student-birthday": "2003-08-09",
          "student-hobby": "唱歌、跳舞、下棋"
        },
        "sort": [
          603.5
        ]
      }
    ]
  }
}

多条件查询(bool)

GET /student/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "student-provice": "山东"
          }
        }
      ],
      "must_not": [
        {
          "match": {
            "student-city": "菏泽"
          }
        }
      ],
      "should": [
        {
          "match": {
            "student-gender": "女"
          }
        }
      ]
    }
  }
}

返回结果如下

{
  "took": 1,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": {
      "value": 5,
      "relation": "eq"
    },
    "max_score": 0.8754687,
    "hits": [
      {
        "_index": "student",
        "_id": "3",
        "_score": 0.8754687,
        "_source": {
          "student-provice": "山东",
          "student-city": "济宁",
          "student-name": "冯程程",
          "student-gender": "女",
          "student-address": "高新区10689号",
          "student-age": 10,
          "student-total-score": 603.5,
          "student-birthday": "2003-08-09",
          "student-hobby": "唱歌、跳舞、下棋"
        }
      },
      {
        "_index": "student",
        "_id": "6",
        "_score": 0.8754687,
        "_source": {
          "student-provice": "山东",
          "student-city": "威海",
          "student-name": "宋丽君",
          "student-gender": "女",
          "student-address": "槐荫区10989号",
          "student-age": 12,
          "student-total-score": 596.5,
          "student-birthday": "2003-11-22",
          "student-hobby": "唱歌、跳舞、下棋"
        }
      },
      {
        "_index": "student",
        "_id": "4",
        "_score": 0.2876821,
        "_source": {
          "student-provice": "山东",
          "student-city": "济南",
          "student-name": "程诚",
          "student-gender": "男",
          "student-address": "历下区2869号",
          "student-age": 11,
          "student-total-score": 559.5,
          "student-birthday": "2004-06-09",
          "student-hobby": "唱歌、游泳、跑步、骑行、爬山、篮球、乒乓球、足球、排球、跳舞、下棋、军事"
        }
      },
      {
        "_index": "student",
        "_id": "5",
        "_score": 0.18232156,
        "_source": {
          "student-provice": "山东",
          "student-city": "聊城",
          "student-name": "刘英杰",
          "student-gender": "男",
          "student-address": "平阴县10001号",
          "student-age": 14,
          "student-total-score": 483.5,
          "student-birthday": "2003-02-13",
          "student-hobby": "唱歌、游泳、跑步、爬山、篮球、乒乓球"
        }
      },
      {
        "_index": "student",
        "_id": "2",
        "_score": 0.18232156,
        "_source": {
          "student-provice": "山东",
          "student-city": "威海",
          "student-name": "张小明",
          "student-gender": "男",
          "student-address": "历城区10001号",
          "student-age": 12,
          "student-total-score": 583.5,
          "student-birthday": "2003-08-09",
          "student-hobby": "唱歌、游泳、跑步"
        }
      }
    ]
  }
}

多条件查询(范围)

GET /student/_search
GET /student/_search
{
  "query": {
    "bool": {
      "filter": [
        {
          "range": {
            "student-age": {
              "gte": 12,
              "lte": 13
            }
          }
        }
      ]
    }
  }
}

返回结果如下

{
  "took": 1,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": {
      "value": 2,
      "relation": "eq"
    },
    "max_score": 0,
    "hits": [
      {
        "_index": "student",
        "_id": "2",
        "_score": 0,
        "_source": {
          "student-provice": "山东",
          "student-city": "威海",
          "student-name": "张小明",
          "student-gender": "男",
          "student-address": "历城区10001号",
          "student-age": 12,
          "student-total-score": 583.5,
          "student-birthday": "2003-08-09",
          "student-hobby": "唱歌、游泳、跑步"
        }
      },
      {
        "_index": "student",
        "_id": "6",
        "_score": 0,
        "_source": {
          "student-provice": "山东",
          "student-city": "威海",
          "student-name": "宋丽君",
          "student-gender": "女",
          "student-address": "槐荫区10989号",
          "student-age": 12,
          "student-total-score": 596.5,
          "student-birthday": "2003-11-22",
          "student-hobby": "唱歌、跳舞、下棋"
        }
      }
    ]
  }
}

全文检索查询

GET /student/_search
{
  "query": {
    "match": {
      "student-hobby": "军事"
    }
  }
}

返回结果如下

{
  "took": 2,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": {
      "value": 2,
      "relation": "eq"
    },
    "max_score": 0.2876821,
    "hits": [
      {
        "_index": "student",
        "_id": "4",
        "_score": 0.2876821,
        "_source": {
          "student-provice": "山东",
          "student-city": "济南",
          "student-name": "程诚",
          "student-gender": "男",
          "student-address": "历下区2869号",
          "student-age": 11,
          "student-total-score": 559.5,
          "student-birthday": "2004-06-09",
          "student-hobby": "唱歌、游泳、跑步、骑行、爬山、篮球、乒乓球、足球、排球、跳舞、下棋、军事"
        }
      },
      {
        "_index": "student",
        "_id": "1",
        "_score": 0.2876821,
        "_source": {
          "student-provice": "山东",
          "student-city": "菏泽",
          "student-name": "张海剑",
          "student-gender": "男",
          "student-address": "历下区111号",
          "student-age": 11,
          "student-total-score": 540.5,
          "student-birthday": "2003-08-25",
          "student-hobby": "唱歌、游泳、跑步、骑行、爬山、篮球、乒乓球、足球、排球、跳舞、下棋、军事"
        }
      }
    ]
  }
}

短语匹配

slop:两个词语之间的长度不能超过4 如家酒店,如家惬意酒店

GET /student/_search
{
  "query": {
    "match_phrase": {
      "student-hobby": {
        "query": "唱歌 骑行",
        "slop": 9
      }
    }
  }
}

返回结果如下

{
  "took": 8,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": {
      "value": 2,
      "relation": "eq"
    },
    "max_score": 0.2751742,
    "hits": [
      {
        "_index": "student",
        "_id": "4",
        "_score": 0.2751742,
        "_source": {
          "student-provice": "山东",
          "student-city": "济南",
          "student-name": "程诚",
          "student-gender": "男",
          "student-address": "历下区2869号",
          "student-age": 11,
          "student-total-score": 559.5,
          "student-birthday": "2004-06-09",
          "student-hobby": "唱歌、游泳、跑步、骑行、爬山、篮球、乒乓球、足球、排球、跳舞、下棋、军事"
        }
      },
      {
        "_index": "student",
        "_id": "1",
        "_score": 0.2751742,
        "_source": {
          "student-provice": "山东",
          "student-city": "菏泽",
          "student-name": "张海剑",
          "student-gender": "男",
          "student-address": "历下区111号",
          "student-age": 11,
          "student-total-score": 540.5,
          "student-birthday": "2003-08-25",
          "student-hobby": "唱歌、游泳、跑步、骑行、爬山、篮球、乒乓球、足球、排球、跳舞、下棋、军事"
        }
      }
    ]
  }
}

高亮查询

GET /student/_search
{
  "query": {
    "match_phrase": {
      "student-hobby": {
        "query": "唱歌 下棋",
        "slop": 4
      }
    }
  },
  "highlight": {
    "fields": {
      "student-hobby": {}
    }
  }
}

返回结果如下

{
  "took": 3,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": {
      "value": 2,
      "relation": "eq"
    },
    "max_score": 0.745998,
    "hits": [
      {
        "_index": "student",
        "_id": "3",
        "_score": 0.745998,
        "_source": {
          "student-provice": "山东",
          "student-city": "济宁",
          "student-name": "冯程程",
          "student-gender": "女",
          "student-address": "高新区10689号",
          "student-age": 10,
          "student-total-score": 603.5,
          "student-birthday": "2003-08-09",
          "student-hobby": "唱歌、跳舞、下棋"
        },
        "highlight": {
          "student-hobby": [
            "<em>唱歌、跳舞、下棋</em>"
          ]
        }
      },
      {
        "_index": "student",
        "_id": "6",
        "_score": 0.5664797,
        "_source": {
          "student-provice": "山东",
          "student-city": "威海",
          "student-name": "宋丽君",
          "student-gender": "女",
          "student-address": "槐荫区10989号",
          "student-age": 12,
          "student-total-score": 596.5,
          "student-birthday": "2003-11-22",
          "student-hobby": "唱歌、跳舞、下棋"
        },
        "highlight": {
          "student-hobby": [
            "<em>唱歌、跳舞、下棋</em>"
          ]
        }
      }
    ]
  }
}

分组查询(聚合)

GET /student/_search
{
  "aggs": {
    "city_group": {
      "terms": {
        "field": "student-city"
      }
    }
  }
}

返回结果如下

{
  "took": 1,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": {
      "value": 6,
      "relation": "eq"
    },
    "max_score": 1,
    "hits": [
      {
        "_index": "student",
        "_id": "3",
        "_score": 1,
        "_source": {
          "student-provice": "山东",
          "student-city": "济宁",
          "student-name": "冯程程",
          "student-gender": "女",
          "student-address": "高新区10689号",
          "student-age": 10,
          "student-total-score": 603.5,
          "student-birthday": "2003-08-09",
          "student-hobby": "唱歌、跳舞、下棋"
        }
      },
      {
        "_index": "student",
        "_id": "5",
        "_score": 1,
        "_source": {
          "student-provice": "山东",
          "student-city": "聊城",
          "student-name": "刘英杰",
          "student-gender": "男",
          "student-address": "平阴县10001号",
          "student-age": 14,
          "student-total-score": 483.5,
          "student-birthday": "2003-02-13",
          "student-hobby": "唱歌、游泳、跑步、爬山、篮球、乒乓球"
        }
      },
      {
        "_index": "student",
        "_id": "4",
        "_score": 1,
        "_source": {
          "student-provice": "山东",
          "student-city": "济南",
          "student-name": "程诚",
          "student-gender": "男",
          "student-address": "历下区2869号",
          "student-age": 11,
          "student-total-score": 559.5,
          "student-birthday": "2004-06-09",
          "student-hobby": "唱歌、游泳、跑步、骑行、爬山、篮球、乒乓球、足球、排球、跳舞、下棋、军事"
        }
      },
      {
        "_index": "student",
        "_id": "2",
        "_score": 1,
        "_source": {
          "student-provice": "山东",
          "student-city": "威海",
          "student-name": "张小明",
          "student-gender": "男",
          "student-address": "历城区10001号",
          "student-age": 12,
          "student-total-score": 583.5,
          "student-birthday": "2003-08-09",
          "student-hobby": "唱歌、游泳、跑步"
        }
      },
      {
        "_index": "student",
        "_id": "6",
        "_score": 1,
        "_source": {
          "student-provice": "山东",
          "student-city": "威海",
          "student-name": "宋丽君",
          "student-gender": "女",
          "student-address": "槐荫区10989号",
          "student-age": 12,
          "student-total-score": 596.5,
          "student-birthday": "2003-11-22",
          "student-hobby": "唱歌、跳舞、下棋"
        }
      },
      {
        "_index": "student",
        "_id": "1",
        "_score": 1,
        "_source": {
          "student-provice": "山东",
          "student-city": "菏泽",
          "student-name": "张海剑",
          "student-gender": "男",
          "student-address": "历下区111号",
          "student-age": 11,
          "student-total-score": 540.5,
          "student-birthday": "2003-08-25",
          "student-hobby": "唱歌、游泳、跑步、骑行、爬山、篮球、乒乓球、足球、排球、跳舞、下棋、军事"
        }
      }
    ]
  },
  "aggregations": {
    "city_group": {
      "doc_count_error_upper_bound": 0,
      "sum_other_doc_count": 0,
      "buckets": [
        {
          "key": "威海",
          "doc_count": 2
        },
        {
          "key": "济南",
          "doc_count": 1
        },
        {
          "key": "济宁",
          "doc_count": 1
        },
        {
          "key": "聊城",
          "doc_count": 1
        },
        {
          "key": "菏泽",
          "doc_count": 1
        }
      ]
    }
  }
}

分组和平均值查询

GET /student/_search
{
  "aggs": {
    "age_group": {
      "terms": {
        "field": "student-age"
      }
    },
    "score_avg": {
      "avg": {
        "field": "student-total-score"
      }
    }
  }
}

返回结果如下

{
  "took": 2,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": {
      "value": 6,
      "relation": "eq"
    },
    "max_score": 1,
    "hits": [
      {
        "_index": "student",
        "_id": "3",
        "_score": 1,
        "_source": {
          "student-provice": "山东",
          "student-city": "济宁",
          "student-name": "冯程程",
          "student-gender": "女",
          "student-address": "高新区10689号",
          "student-age": 10,
          "student-total-score": 603.5,
          "student-birthday": "2003-08-09",
          "student-hobby": "唱歌、跳舞、下棋"
        }
      },
      {
        "_index": "student",
        "_id": "5",
        "_score": 1,
        "_source": {
          "student-provice": "山东",
          "student-city": "聊城",
          "student-name": "刘英杰",
          "student-gender": "男",
          "student-address": "平阴县10001号",
          "student-age": 14,
          "student-total-score": 483.5,
          "student-birthday": "2003-02-13",
          "student-hobby": "唱歌、游泳、跑步、爬山、篮球、乒乓球"
        }
      },
      {
        "_index": "student",
        "_id": "4",
        "_score": 1,
        "_source": {
          "student-provice": "山东",
          "student-city": "济南",
          "student-name": "程诚",
          "student-gender": "男",
          "student-address": "历下区2869号",
          "student-age": 11,
          "student-total-score": 559.5,
          "student-birthday": "2004-06-09",
          "student-hobby": "唱歌、游泳、跑步、骑行、爬山、篮球、乒乓球、足球、排球、跳舞、下棋、军事"
        }
      },
      {
        "_index": "student",
        "_id": "2",
        "_score": 1,
        "_source": {
          "student-provice": "山东",
          "student-city": "威海",
          "student-name": "张小明",
          "student-gender": "男",
          "student-address": "历城区10001号",
          "student-age": 12,
          "student-total-score": 583.5,
          "student-birthday": "2003-08-09",
          "student-hobby": "唱歌、游泳、跑步"
        }
      },
      {
        "_index": "student",
        "_id": "6",
        "_score": 1,
        "_source": {
          "student-provice": "山东",
          "student-city": "威海",
          "student-name": "宋丽君",
          "student-gender": "女",
          "student-address": "槐荫区10989号",
          "student-age": 12,
          "student-total-score": 596.5,
          "student-birthday": "2003-11-22",
          "student-hobby": "唱歌、跳舞、下棋"
        }
      },
      {
        "_index": "student",
        "_id": "1",
        "_score": 1,
        "_source": {
          "student-provice": "山东",
          "student-city": "菏泽",
          "student-name": "张海剑",
          "student-gender": "男",
          "student-address": "历下区111号",
          "student-age": 11,
          "student-total-score": 540.5,
          "student-birthday": "2003-08-25",
          "student-hobby": "唱歌、游泳、跑步、骑行、爬山、篮球、乒乓球、足球、排球、跳舞、下棋、军事"
        }
      }
    ]
  },
  "aggregations": {
    "age_group": {
      "doc_count_error_upper_bound": 0,
      "sum_other_doc_count": 0,
      "buckets": [
        {
          "key": 11,
          "doc_count": 2
        },
        {
          "key": 12,
          "doc_count": 2
        },
        {
          "key": 10,
          "doc_count": 1
        },
        {
          "key": 14,
          "doc_count": 1
        }
      ]
    },
    "score_avg": {
      "value": 561.1666666666666
    }
  }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Grain322

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

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

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

打赏作者

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

抵扣说明:

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

余额充值