ElasticSearch之查询

 

1, 简单查询, 查询books索引下的全部信息

POST /books/_search
{
    "took": 6,
    "timed_out": false,
    "_shards": {
        "total": 5,
        "successful": 5,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": 3,
        "max_score": 1,
        "hits": [
            {
                "_index": "books",
                "_type": "it",
                "_id": "2dtQGGUBNmgQz3Wn9pp4",
                "_score": 1,
                "_source": {
                    "bookId": 3,
                    "bookName": "演员的自我修养",
                    "author": "周星驰",
                    "publishDate": "2018-08-08"
                }
            },
            {
                "_index": "books",
                "_type": "it",
                "_id": "2",
                "_score": 1,
                "_source": {
                    "bookId": 2,
                    "bookName": "Mysql",
                    "author": "Liudehua",
                    "publishDate": "2018-01-12"
                }
            },
            {
                "_index": "books",
                "_type": "it",
                "_id": "1",
                "_score": 1,
                "_source": {
                    "bookId": 1,
                    "bookName": "PHP",
                    "publishDate": "2018-01-12"
                }
            }
        ]
    }
}

2, 简单条件查询

PUT books/it/_search

查询条件: bookName是php的

{
	"query":{
		"term":{
			"bookName":"php"
		}
	}
}

查询结果:

{
    "took": 8,
    "timed_out": false,
    "_shards": {
        "total": 5,
        "successful": 5,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": 3,
        "max_score": 0.5193239,
        "hits": [
            {
                "_index": "books",
                "_type": "it",
                "_id": "29tWGGUBNmgQz3WnOJo7",
                "_score": 0.5193239,
                "_source": {
                    "bookId": 5,
                    "bookName": "PHP是世界上最好的语言",
                    "publishDate": "2018-01-01"
                }
            },
            {
                "_index": "books",
                "_type": "it",
                "_id": "2ttVGGUBNmgQz3Wn35oo",
                "_score": 0.2876821,
                "_source": {
                    "bookId": 4,
                    "bookName": "PHP从入门到放弃",
                    "publishDate": "2018-01-01"
                }
            },
            {
                "_index": "books",
                "_type": "it",
                "_id": "1",
                "_score": 0.2876821,
                "_source": {
                    "bookId": 1,
                    "bookName": "PHP",
                    "publishDate": "2018-01-12"
                }
            }
        ]
    }
}

3, 分页查询:

POST /books/it/_search

请求参数:

{
	"from":0,"size":2,
	"query":{
		"term":{
			"bookName":"php"
		}
	}
}

查询结果:

{
    "took": 8,
    "timed_out": false,
    "_shards": {
        "total": 5,
        "successful": 5,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": 3,
        "max_score": 0.5193239,
        "hits": [
            {
                "_index": "books",
                "_type": "it",
                "_id": "29tWGGUBNmgQz3WnOJo7",
                "_score": 0.5193239,
                "_source": {
                    "bookId": 5,
                    "bookName": "PHP是世界上最好的语言",
                    "publishDate": "2018-01-01"
                }
            },
            {
                "_index": "books",
                "_type": "it",
                "_id": "2ttVGGUBNmgQz3Wn35oo",
                "_score": 0.2876821,
                "_source": {
                    "bookId": 4,
                    "bookName": "PHP从入门到放弃",
                    "publishDate": "2018-01-01"
                }
            }
        ]
    }
}

4, 排序

POST books/it/_search
{
	"sort":{
			"bookId":{"order":"asc"},
			"author":{"order":"desc"}
			}
}

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值