1、match all
查询所有
GET /company/employee/_search
{
"query": {
"match_all": {}
}
}
响应结果
{
"took": 1,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 3,
"max_score": 1,
"hits": [
{
"_index": "company",
"_type": "employee",
"_id": "2",
"_score": 1,
"_source": {
"address": {
"country": "china",
"province": "jiangsu",
"city": "nanjing"
},
"name": "tom",
"age": 30,
"join_date": "2016-01-01"
}
},
{
"_index": "company",
"_type": "employee",
"_id": "1",
"_score": 1,
"_source": {
"address": {
"country": "china",
"province": "guangdong",
"city": "guangzhou"
},
"name": "zhangsan",
"age": 18,
"join_date": "2017-01-01"
}
},
{
"_index": "company",
"_type": "employee",
"_id": "3",
"_score": 1,
"_source": {
"address": {
"country": "china",
"province": "shanxi",
"city": "xian"
},
"name": "marry",
"age": 35,
"join_date": "2015-01-01"
}
}
]
}
}
2、match
查询指定filed是否包含指定文本
match query 知道分词器的存在,会对field进行分词操作,然后再查询
GET /_search
{
"query": { "match": { "title": "my elasticsearch article" }}
}
响应结果
{
"took": 7,
"timed_out": false,
"_shards": {
"total": 31,
"successful": 31,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 3,
"max_score": 0.7594807,
"hits": [
{
"_index": "website",
"_type": "article",
"_id": "2",
"_score": 0.7594807,
"_source": {
"title": "my elasticsearch article",
"content": "es is very good",
"author_id": 112
}
},
{
"_index": "website",
"_type": "article",
"_id": "1",
"_score": 0.7594807,
"_source": {
"title": "my elasticsearch article",
"content": "elasticsearch is very good",
"author_id": 111
}
},
{
"_index": "website",
"_type": "article",
"_id": "3",
"_score": 0.7594807,
"_source": {
"title": "my elasticsearch article",
"content": "elasticsearch is very bad",
"author_id": 113
}
}
]
}
}
3、multi match
指定多个filed是否包含指定文本
GET /test_index/test_type/_search
{
"query": {
"multi_match": {
"query": "test",
"fields": ["test_field", "test_field1"]
}
}
}
响应结果
{
"took": 30,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 5,
"max_score": 0.8835016,
"hits": [
{
"_index": "test_index",
"_type": "test_type",
"_id": "6",
"_score": 0.8835016,
"_source": {
"test_field": "test test"
}
},
{
"_index": "test_index",
"_type": "test_type",
"_id": "8",
"_score": 0.49191087,
"_source": {
"test_field": "test client 2"
}
},
{
"_index": "test_index",
"_type": "test_type",
"_id": "4",
"_score": 0.25811607,
"_source": {
"test_field1": "test field111111"
}
},
{
"_index": "test_index",
"_type": "test_type",
"_id": "1",
"_score": 0.25811607,
"_source": {
"test_field1": "test field1",
"test_field2": "bulk test1"
}
},
{
"_index": "test_index",
"_type": "test_type",
"_id": "7",
"_score": 0.25316024,
"_source": {
"test_field": "test client 2"
}
}
]
}
}
4、range query
区间查询
GET /company/employee/_search
{
"query": {
"range": {
"age": {
"gte": 30
}
}
}
}
响应结果
{
"took": 2,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 2,
"max_score": 1,
"hits": [
{
"_index": "company",
"_type": "employee",
"_id": "2",
"_score": 1,
"_source": {
"address": {
"country": "china",
"province": "jiangsu",
"city": "nanjing"
},
"name": "tom",
"age": 30,
"join_date": "2016-01-01"
}
},
{
"_index": "company",
"_type": "employee",
"_id": "3",
"_score": 1,
"_source": {
"address": {
"country": "china",
"province": "shanxi",
"city": "xian"
},
"name": "marry",
"age": 35,
"join_date": "2015-01-01"
}
}
]
}
}
5、term query
查询某个字段里含有某个关键词的文档
term query会去倒排索引中寻找确切的term,它并不知道分词器的存在,这种查询适合keyword、numeric、date等明确值的,不会对文本分词
因为 term query不会对文本分词,如果想使用term query的话,要在创建index时手动指定field不分词no_analyze.
GET /test_index/test_type/_search
{
"query": {
"term": {
"test_field": "test"
}
}
}
响应结果
{
"took": 0,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 3,
"max_score": 0.8835016,
"hits": [
{
"_index": "test_index",
"_type": "test_type",
"_id": "6",
"_score": 0.8835016,
"_source": {
"test_field": "test test"
}
},
{
"_index": "test_index",
"_type": "test_type",
"_id": "8",
"_score": 0.49191087,
"_source": {
"test_field": "test client 2"
}
},
{
"_index": "test_index",
"_type": "test_type",
"_id": "7",
"_score": 0.25316024,
"_source": {
"test_field": "test client 2"
}
}
]
}
}
GET /test_index/test_type/_search
{
"query": {
"term": {
"test_field": "test hello"
}
}
}
响应结果
{
"took": 0,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 0,
"max_score": null,
"hits": []
}
}
6、terms query
查询某个字段里含有多个关键词的文档,只要符合一个关键词就可以,等价于mysql 的 in()
GET /test_index/test_type/_search
{
"query": {
"terms": {
"test_field": [ "test","elasticsearch"]
}
}
}
响应结果
{
"took": 3,
"timed_out": false,
"_shards": {
"total": 31,
"successful": 31,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 3,
"max_score": 0.98382175,
"hits": [
{
"_index": "test_index",
"_type": "test_type",
"_id": "8",
"_score": 0.98382175,
"_source": {
"test_field": "test client 2"
}
},
{
"_index": "test_index",
"_type": "test_type",
"_id": "6",
"_score": 0.8835016,
"_source": {
"test_field": "test test"
}
},
{
"_index": "test_index",
"_type": "test_type",
"_id": "7",
"_score": 0.5063205,
"_source": {
"test_field": "test client 2"
}
}
]
}
}
|