Elasticsearch查询2

1 上一篇介绍match_all 查询来匹配所有文档,现在介绍match查询,可以将其视为基本的字段搜索查询(即针对特定字段或一组字段进行搜索)。
2 数据集合如下:
 Elasticsearch查询2
 //查询age=100

GET /mydb/_search
{
"query": { "match": { "age": 100 } }
}
"hits": {
"total": 1,
"max_score": 1,
"hits": [
{
"_index": "mydb",
"_type": "external",
"_id": "XJ7uwmIBJNOIwaI44rwX",
"_score": 1,
"_source": {
"name": "王鹏",
"age": 100,
"date": "2018-04-11 13:01:01"
}
}
]
}

/查询 name包含'鹏'或'cheng'
GET /mydb/_search
{
"query": { "match": { "name": "鹏 cheng" } },
"_source": ["name","date"],
"sort": [
{"name.keyword": { "order": "desc" }},
{"date.keyword": { "order": "desc" }}
]
}

//查询结果
"hits": {
"total": 3,
"max_score": null,
"hits": [
{
"_index": "mydb",
"_type": "external",
"_id": "XJ7uwmIBJNOIwaI44rwX",
"_score": null,
"_source": {
"date": "2018-04-11 13:01:01",
"name": "王鹏"
},
"sort": [
"王鹏",
"2018-04-11 13:01:01"
]
},
{
"_index": "mydb",
"_type": "external",
"_id": "Yp47w2IBJNOIwaI407zN",
"_score": null,
"_source": {
"date": "2018-04-11 13:01:01",
"name": "cheng hong yan"
},
"sort": [
"cheng hong yan",
"2018-04-11 13:01:01"
]
},
{
"_index": "mydb",
"_type": "external",
"_id": "Y547w2IBJNOIwaI44Lze",
"_score": null,
"_source": {
"date": "2018-04-11 13:01:01",
"name": "cheng hong fei"
},
"sort": [
"cheng hong fei",
"2018-04-11 13:01:01"
]
}
]
}

//查询 name包含短语'cheng hong'
GET /mydb/_search
{
"query": { "match_phrase": { "name": "cheng hong" } },
"_source": ["name","date"],
"sort": [
{"name.keyword": { "order": "desc" }},
{"date.keyword": { "order": "desc" }}
]
}

//查询结果
"hits": {
"total": 2,
"max_score": null,
"hits": [
{
"_index": "mydb",
"_type": "external",
"_id": "Yp47w2IBJNOIwaI407zN",
"_score": null,
"_source": {
"date": "2018-04-11 13:01:01",
"name": "cheng hong yan"
},
"sort": [
"cheng hong yan",
"2018-04-11 13:01:01"
]
},
{
"_index": "mydb",
"_type": "external",
"_id": "Y547w2IBJNOIwaI44Lze",
"_score": null,
"_source": {
"date": "2018-04-11 13:01:01",
"name": "cheng hong fei"
},
"sort": [
"cheng hong fei",
"2018-04-11 13:01:01"
]
}
]
}

//查询 name同时包含'cheng'和'yan'
//bool must 里面的所有查询条件必须都为真时才会被匹配
GET /mydb/_search
{
"query": {
"bool": {
"must": [
{ "match": { "name": "cheng" } },
{ "match": { "name": "yan" } }
]
}
}
}

//结果
"hits": {
"total": 1,
"max_score": 0.5753642,
"hits": [
{
"_index": "mydb",
"_type": "external",
"_id": "Yp47w2IBJNOIwaI407zN",
"_score": 0.5753642,
"_source": {
"name": "cheng hong yan",
"age": 28,
"date": "2018-04-11 13:01:01"
}
}
]
}

//查询 name包含'cheng' or 'yan'
GET /mydb/_search
{
"query": {
"bool": {
"should": [
{ "match": { "name": "cheng" } },
{ "match": { "name": "fei" } }
]
}
}
}

//查询结果
"hits": {
"total": 2,
"max_score": 1.6285466,
"hits": [
{
"_index": "mydb",
"_type": "external",
"_id": "Y547w2IBJNOIwaI44Lze",
"_score": 1.6285466,
"_source": {
"name": "cheng hong fei",
"age": 28,
"date": "2018-04-11 13:01:01"
}
},
{
"_index": "mydb",
"_type": "external",
"_id": "Yp47w2IBJNOIwaI407zN",
"_score": 0.2876821,
"_source": {
"name": "cheng hong yan",
"age": 28,
"date": "2018-04-11 13:01:01"
}
}
]
}

//查询 name 不包含'新' 和 'chy' 和 'cheng' 和 'jack' 和 '王'
GET /mydb/_search
{
"query": {
"bool": {
"must_not": [
{ "match": { "name": "新" } },
{ "match": { "name": "chy" } },
{ "match": { "name": "cheng" } },
{ "match": { "name": "jack" } },
{ "match": { "name": "王" } }
]
}
}
}

//查询结果
"hits": {
"total": 2,
"max_score": 1,
"hits": [
{
"_index": "mydb",
"_type": "external",
"_id": "YJ43w2IBJNOIwaI4ibx3",
"_score": 1,
"_source": {
"name": "chenghongyan",
"age": 28,
"date": "2018-04-11 13:01:01"
}
},
{
"_index": "mydb",
"_type": "external",
"_id": "YZ43w2IBJNOIwaI4mry8",
"_score": 1,
"_source": {
"name": "chenghongfei",
"age": 28,
"date": "2018-04-11 13:01:01"
}
}
]
}

//查询 age=100 并且 name 不包含
GET /mydb/_search
{
"query": {
"bool": {
"must": [
{ "match": { "age": "100" } }
],
"must_not": [
{ "match": { "name": "jack" } }
]
}
}
}

//查询结果
"hits": {
"total": 1,
"max_score": 1,
"hits": [
{
"_index": "mydb",
"_type": "external",
"_id": "XJ7uwmIBJNOIwaI44rwX",
"_score": 1,
"_source": {
"name": "王鹏",
"age": 100,
"date": "2018-04-11 13:01:01"
}
}
]
}