1.准备数据
export LANG=en_US.UTF8
vi sspuuser.json
{"index":{"_index":"sspuuser","_id":"1"}}
{"id": 1, "name": "sspu001", "city": "广州", "course":"oracle", "teacher": "王麻子", "birthdate": "20200829"}
{"index":{"_index":"sspuuser","_id":"2"}}
{"id": 2, "name": "sspu002", "city": "深圳", "course":"mysql", "teacher": "王麻子 2 号", "birthdate": "20200826"}
{"index":{"_index":"sspuuser","_id":"3"}}
{"id": 3, "name": "sspu003", "city": "北京", "course":"sqlserver", "teacher": "王麻子 1号", "birthdate": "20200825"}
{"index":{"_index":"sspuuser","_id":"4"}}
{"id": 4, "name": "sspu004", "city": "上海", "course":"postgrestsql", "teacher": "王麻子 3 号", "birthdate": "20200828"}
{"index":{"_index":"sspuuser","_id":"5"}}
{"id": 5, "name": "sspu005", "city": "杭州", "course":"开源数据库", "teacher": "王麻子", "birthdate": "20200827"}
{"index":{"_index":"sspuuser","_id":"6"}}
{"id": 6, "name": "sspu006", "city": "成都", "course":"greenplum", "teacher": "王麻子", "birthdate": "20200729"}
{"index":{"_index":"sspuuser","_id":"7"}}
{"id": 7, "name": "sspu007", "city": "武汉", "course":"redis", "teacher": "王麻子", "birthdate": "20200210"}
{"index":{"_index":"sspuuser","_id":"8"}}
{"id": 8, "name": "sspu008", "city": "北京", "course":"mongodb", "teacher": "王麻子", "birthdate": "20190529"}
{"index":{"_index":"sspuuser","_id":"9"}}
{"id": 9, "name": "sspu009", "city": "上海", "course":"db2", "teacher": "王麻子", "birthdate": "20180229"}
{"index":{"_index":"sspuuser","_id":"10"}}
{"id": 10, "name": "sspu010", "city": "广州", "course":"国产数据库", "teacher": "王麻子 1 号", "birthdate": "20200302"}
{"index":{"_index":"sspuuser","_id":"11"}}
{"id": 11, "name": "sspu011", "city": "上海", "course":"国产数据库", "teacher": "王麻子 2 号", "birthdate": "20200402"}
2.批量插入数据。
curl -XPUT '192.168.1.7:9201/_bulk?pretty' -H "Content-Type: application/json" --data-binary @sspuuser.json
--查询所有数据。
GET sspuuser/_search
{
"query": {
"match_all": {}
}
}
{
"took" : 9,
"timed_out" : false,
"_shards" : {
"total" : 3,
"successful" : 3,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 11,
"relation" : "eq"
},
"max_score" : 1.0,
"hits" : [
{
"_index" : "sspuuser",
"_type" : "_doc",
"_id" : "5",
"_score" : 1.0,
"_source" : {
"id" : 5,
"name" : "sspu005",
"city" : "杭州",
"course" : "开源数据库",
"teacher" : "王麻子",
"birthdate" : "20200827"
}
},
{
"_index" : "sspuuser",
"_type" : "_doc",
"_id" : "7",
"_score" : 1.0,
"_source" : {
"id" : 7,
"name" : "sspu007",
"city" : "武汉",
"course" : "redis",
"teacher" : "王麻子",
"birthdate" : "20200210"
}
},
{
"_index" : "sspuuser",
"_type" : "_doc",
"_id" : "2",
"_score" : 1.0,
"_source" : {
"id" : 2,
"name" : "sspu002",
"city" : "深圳",
"course" : "mysql",
"teacher" : "王麻子 2 号",
"birthdate" : "20200826"
}
},
{
"_index" : "sspuuser",
"_type" : "_doc",
"_id" : "3",
"_score" : 1.0,
"_source" : {
"id" : 3,
"name" : "sspu003",
"city" : "北京",
"course" : "sqlserver",
"teacher" : "王麻子 1号",
"birthdate" : "20200825"
}
},
{
"_index" : "sspuuser",
"_type" : "_doc",
"_id" : "4",
"_score" : 1.0,
"_source" : {
"id" : 4,
"name" : "sspu004",
"city" : "上海",
"course" : "postgrestsql",
"teacher" : "王麻子 3 号",
"birthdate" : "20200828"
}
},
{
"_index" : "sspuuser",
"_type" : "_doc",
"_id" : "10",
"_score" : 1.0,
"_source" : {
"id" : 10,
"name" : "sspu010",
"city" : "广州",
"course" : "国产数据库",
"teacher" : "王麻子 1 号",
"birthdate" : "20200302"
}
},
{
"_index" : "sspuuser",
"_type" : "_doc",
"_id" : "1",
"_score" : 1.0,
"_source" : {
"id" : 1,
"name" : "sspu001",
"city" : "广州",
"course" : "oracle",
"teacher" : "王麻子",
"birthdate" : "20200829"
}
},
{
"_index" : "sspuuser",
"_type" : "_doc",
"_id" : "6",
"_score" : 1.0,
"_source" : {
"id" : 6,
"name" : "sspu006",
"city" : "成都",
"course" : "greenplum",
"teacher" : "王麻子",
"birthdate" : "20200729"
}
},
{
"_index" : "sspuuser",
"_type" : "_doc",
"_id" : "8",
"_score" : 1.0,
"_source" : {
"id" : 8,
"name" : "sspu008",
"city" : "北京",
"course" : "mongodb",
"teacher" : "王麻子",
"birthdate" : "20190529"
}
},
{
"_index" : "sspuuser",
"_type" : "_doc",
"_id" : "9",
"_score" : 1.0,
"_source" : {
"id" : 9,
"name" : "sspu009",
"city" : "上海",
"course" : "db2",
"teacher" : "王麻子",
"birthdate" : "20180229"
}
}
]
}
}
3.按照一个字段顺序排序
GET sspuuser/_search
{
"query": {
"match_all": {}
},
"_source": ["birthdate"],
"sort": [
{
"birthdate": {
"order": "asc"
}
}
]
}
--无法直接排序,需要设置:在字段:birthdate上设置:
--fielddata=true
"failed_shards" : [
{
"shard" : 0,
"index" : "sspuuser",
"node" : "rVIh8-d2SmKscxM5zlgqfA",
"reason" : {
"type" : "illegal_argument_exception",
"reason" : "Text fields are not optimised for operations that require per-document field data like aggregations and sorting, so these operations are disabled by default. Please use a keyword field instead. Alternatively, set fielddata=true on [birthdate] in order to load field data by uninverting the inverted index. Note that this can use significant memory."
}
}
]
--设置:字段的:"fielddata": true属性。
PUT sspuuser/_mapping
{
"properties": {
"birthdate": {
"type": "text",
"fielddata": true
}
}
}
--按生日升序。
GET sspuuser/_search
{
"query": {
"match_all": {}
},
"_source": ["birthdate"],
"sort": [
{
"birthdate": {
"order": "asc"
}
}
]
}
{
"took" : 71,
"timed_out" : false,
"_shards" : {
"total" : 3,
"successful" : 3,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 11,
"relation" : "eq"
},
"max_score" : null,
"hits" : [
{
"_index" : "sspuuser",
"_type" : "_doc",
"_id" : "9",
"_score" : null,
"_source" : {
"birthdate" : "20180229"
},
"sort" : [
"20180229"
]
},
{
"_index" : "sspuuser",
"_type" : "_doc",
"_id" : "8",
"_score" : null,
"_source" : {
"birthdate" : "20190529"
},
"sort" : [
"20190529"
]
},
{
"_index" : "sspuuser",
"_type" : "_doc",
"_id" : "7",
"_score" : null,
"_source" : {
"birthdate" : "20200210"
},
"sort" : [
"20200210"
]
},
{
"_index" : "sspuuser",
"_type" : "_doc",
"_id" : "10",
"_score" : null,
"_source" : {
"birthdate" : "20200302"
},
"sort" : [
"20200302"
]
},
{
"_index" : "sspuuser",
"_type" : "_doc",
"_id" : "11",
"_score" : null,
"_source" : {
"birthdate" : "20200402"
},
"sort" : [
"20200402"
]
},
{
"_index" : "sspuuser",
"_type" : "_doc",
"_id" : "6",
"_score" : null,
"_source" : {
"birthdate" : "20200729"
},
"sort" : [
"20200729"
]
},
{
"_index" : "sspuuser",
"_type" : "_doc",
"_id" : "3",
"_score" : null,
"_source" : {
"birthdate" : "20200825"
},
"sort" : [
"20200825"
]
},
{
"_index" : "sspuuser",
"_type" : "_doc",
"_id" : "2",
"_score" : null,
"_source" : {
"birthdate" : "20200826"
},
"sort" : [
"20200826"
]
},
{
"_index" : "sspuuser",
"_type" : "_doc",
"_id" : "5",
"_score" : null,
"_source" : {
"birthdate" : "20200827"
},
"sort" : [
"20200827"
]
},
{
"_index" : "sspuuser",
"_type" : "_doc",
"_id" : "4",
"_score" : null,
"_source" : {
"birthdate" : "20200828"
},
"sort" : [
"20200828"
]
}
]
}
}
4.按生日降序
GET sspuuser/_search
{
"query": {
"match_all": {}
},
"_source": ["birthdate"],
"sort": [
{
"birthdate": {
"order": "desc"
}
}
]
}
{
"took" : 9,
"timed_out" : false,
"_shards" : {
"total" : 3,
"successful" : 3,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 11,
"relation" : "eq"
},
"max_score" : null,
"hits" : [
{
"_index" : "sspuuser",
"_type" : "_doc",
"_id" : "1",
"_score" : null,
"_source" : {
"birthdate" : "20200829"
},
"sort" : [
"20200829"
]
},
{
"_index" : "sspuuser",
"_type" : "_doc",
"_id" : "4",
"_score" : null,
"_source" : {
"birthdate" : "20200828"
},
"sort" : [
"20200828"
]
},
{
"_index" : "sspuuser",
"_type" : "_doc",
"_id" : "5",
"_score" : null,
"_source" : {
"birthdate" : "20200827"
},
"sort" : [
"20200827"
]
},
{
"_index" : "sspuuser",
"_type" : "_doc",
"_id" : "2",
"_score" : null,
"_source" : {
"birthdate" : "20200826"
},
"sort" : [
"20200826"
]
},
{
"_index" : "sspuuser",
"_type" : "_doc",
"_id" : "3",
"_score" : null,
"_source" : {
"birthdate" : "20200825"
},
"sort" : [
"20200825"
]
},
{
"_index" : "sspuuser",
"_type" : "_doc",
"_id" : "6",
"_score" : null,
"_source" : {
"birthdate" : "20200729"
},
"sort" : [
"20200729"
]
},
{
"_index" : "sspuuser",
"_type" : "_doc",
"_id" : "11",
"_score" : null,
"_source" : {
"birthdate" : "20200402"
},
"sort" : [
"20200402"
]
},
{
"_index" : "sspuuser",
"_type" : "_doc",
"_id" : "10",
"_score" : null,
"_source" : {
"birthdate" : "20200302"
},
"sort" : [
"20200302"
]
},
{
"_index" : "sspuuser",
"_type" : "_doc",
"_id" : "7",
"_score" : null,
"_source" : {
"birthdate" : "20200210"
},
"sort" : [
"20200210"
]
},
{
"_index" : "sspuuser",
"_type" : "_doc",
"_id" : "8",
"_score" : null,
"_source" : {
"birthdate" : "20190529"
},
"sort" : [
"20190529"
]
}
]
}
}
--筛选出多个字段。
GET sspuuser/_search
{
"query": {
"match_all": {}
},
"_source": ["birthdate","city","course"],
"sort": [
{
"birthdate": {
"order": "desc"
}
}
]
}
{
"took" : 8,
"timed_out" : false,
"_shards" : {
"total" : 3,
"successful" : 3,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 11,
"relation" : "eq"
},
"max_score" : null,
"hits" : [
{
"_index" : "sspuuser",
"_type" : "_doc",
"_id" : "1",
"_score" : null,
"_source" : {
"birthdate" : "20200829",
"city" : "广州",
"course" : "oracle"
},
"sort" : [
"20200829"
]
},
{
"_index" : "sspuuser",
"_type" : "_doc",
"_id" : "4",
"_score" : null,
"_source" : {
"birthdate" : "20200828",
"city" : "上海",
"course" : "postgrestsql"
},
"sort" : [
"20200828"
]
},
{
"_index" : "sspuuser",
"_type" : "_doc",
"_id" : "5",
"_score" : null,
"_source" : {
"birthdate" : "20200827",
"city" : "杭州",
"course" : "开源数据库"
},
"sort" : [
"20200827"
]
},
{
"_index" : "sspuuser",
"_type" : "_doc",
"_id" : "2",
"_score" : null,
"_source" : {
"birthdate" : "20200826",
"city" : "深圳",
"course" : "mysql"
},
"sort" : [
"20200826"
]
},
{
"_index" : "sspuuser",
"_type" : "_doc",
"_id" : "3",
"_score" : null,
"_source" : {
"birthdate" : "20200825",
"city" : "北京",
"course" : "sqlserver"
},
"sort" : [
"20200825"
]
},
{
"_index" : "sspuuser",
"_type" : "_doc",
"_id" : "6",
"_score" : null,
"_source" : {
"birthdate" : "20200729",
"city" : "成都",
"course" : "greenplum"
},
"sort" : [
"20200729"
]
},
{
"_index" : "sspuuser",
"_type" : "_doc",
"_id" : "11",
"_score" : null,
"_source" : {
"birthdate" : "20200402",
"city" : "上海",
"course" : "国产数据库"
},
"sort" : [
"20200402"
]
},
{
"_index" : "sspuuser",
"_type" : "_doc",
"_id" : "10",
"_score" : null,
"_source" : {
"birthdate" : "20200302",
"city" : "广州",
"course" : "国产数据库"
},
"sort" : [
"20200302"
]
},
{
"_index" : "sspuuser",
"_type" : "_doc",
"_id" : "7",
"_score" : null,
"_source" : {
"birthdate" : "20200210",
"city" : "武汉",
"course" : "redis"
},
"sort" : [
"20200210"
]
},
{
"_index" : "sspuuser",
"_type" : "_doc",
"_id" : "8",
"_score" : null,
"_source" : {
"birthdate" : "20190529",
"city" : "北京",
"course" : "mongodb"
},
"sort" : [
"20190529"
]
}
]
}
}
5.筛选指定字段排序
GET sspuuser/_search?q=teacher:"王麻子 2 号"&sort=birthdate:desc
{
"took" : 6,
"timed_out" : false,
"_shards" : {
"total" : 3,
"successful" : 3,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 2,
"relation" : "eq"
},
"max_score" : null,
"hits" : [
{
"_index" : "sspuuser",
"_type" : "_doc",
"_id" : "2",
"_score" : null,
"_source" : {
"id" : 2,
"name" : "sspu002",
"city" : "深圳",
"course" : "mysql",
"teacher" : "王麻子 2 号",
"birthdate" : "20200826"
},
"sort" : [
"20200826"
]
},
{
"_index" : "sspuuser",
"_type" : "_doc",
"_id" : "11",
"_score" : null,
"_source" : {
"id" : 11,
"name" : "sspu011",
"city" : "上海",
"course" : "国产数据库",
"teacher" : "王麻子 2 号",
"birthdate" : "20200402"
},
"sort" : [
"20200402"
]
}
]
}
}
6.全文排序。
GET sspuuser/_search?sort=birthdate:asc --按照生日升序。
GET sspuuser/_search?sort=birthdate:desc --按照生日降序
7.分页查询
--按照生日升序,从第3条开始取两条。
GET sspuuser/_search
{
"query": {
"match_all": {}
},
"_source": ["birthdate","city","name"],
"sort": [
{
"birthdate": {
"order": "asc"
}
}
],
"from": 3,
"size": 2
}
{
"took" : 9,
"timed_out" : false,
"_shards" : {
"total" : 3,
"successful" : 3,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 11,
"relation" : "eq"
},
"max_score" : null,
"hits" : [
{
"_index" : "sspuuser",
"_type" : "_doc",
"_id" : "10",
"_score" : null,
"_source" : {
"birthdate" : "20200302",
"city" : "广州",
"name" : "sspu010"
},
"sort" : [
"20200302"
]
},
{
"_index" : "sspuuser",
"_type" : "_doc",
"_id" : "11",
"_score" : null,
"_source" : {
"birthdate" : "20200402",
"city" : "上海",
"name" : "sspu011"
},
"sort" : [
"20200402"
]
}
]
}
}
8.全文搜索取前两行
GET sspuuser/_search
{
"query": {
"match_all": {}
},
"from": 0,
"size": 2
}
{
"took" : 4,
"timed_out" : false,
"_shards" : {
"total" : 3,
"successful" : 3,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 11,
"relation" : "eq"
},
"max_score" : 1.0,
"hits" : [
{
"_index" : "sspuuser",
"_type" : "_doc",
"_id" : "5",
"_score" : 1.0,
"_source" : {
"id" : 5,
"name" : "sspu005",
"city" : "杭州",
"course" : "开源数据库",
"teacher" : "王麻子",
"birthdate" : "20200827"
}
},
{
"_index" : "sspuuser",
"_type" : "_doc",
"_id" : "7",
"_score" : 1.0,
"_source" : {
"id" : 7,
"name" : "sspu007",
"city" : "武汉",
"course" : "redis",
"teacher" : "王麻子",
"birthdate" : "20200210"
}
}
]
}
}