执行搜索
既然我们已经看到了一些基本的搜索参数,那么让我们深入研究一下Query DSL。让我们先看看返回的文档字段。默认情况下,作为所有搜索的一部分返回完整的JSON文档。这被称为“源”(搜索命中中的_source字段)。如果我们不希望返回整个源文档,我们可以只请求从源中返回几个字段。
此示例显示如何从搜索中返回两个字段,即帐号和余额(在_source内):
GET /bank/_search
{
"query": {"match_all": {}},
"_source": ["account_number","balance"]
}
-------------------------------------------------
curl -XGET "http://localhost:9200/bank/_search" -H 'Content-Type: application/json' -d'{ "query": {"match_all": {}}, "_source": ["account_number","balance"]}'
请注意,上面的示例只是减少了_source字段。它仍然只返回一个名为_source
的字段,但在其中,只包括“account_number
”和“balance
”字段。
如果您来自SQL基础,那么上面的内容在概念上与从字段列表中选择SQL有些相似。
现在让我们转到查询部分。以前,我们已经看到了match_all查询如何用于匹配所有文档。现在让我们介绍一个名为匹配查询的match query,它可以被视为基本的字段化搜索查询(即针对特定字段或一组字段进行的搜索)。
此示例返回编号为20的帐户:
GET /bank/_search
{
"query": {"match": {
"account_number": "20"
}}
}
--------------------------------------------------------
curl -XGET "http://localhost:9200/bank/_search" -H 'Content-Type: application/json' -d'{ "query": {"match": { "account_number": "20" }}}'
此示例返回地址中包含术语“mill”的所有帐户:
GET /bank/_search
{
"query": {"match": {
"address": "mill"
}}
}
--------------------------
curl -XGET "http://localhost:9200/bank/_search" -H 'Content-Type: application/json' -d'{ "query": {"match": { "address": "mill" }}}'
此示例返回地址中包含术语“mill”或“lane”的所有帐户:
GET /bank/_search
{
"query": {"match": {
"address": "mill lane"
}}
}
------------------------------
curl -XGET "http://localhost:9200/bank/_search" -H 'Content-Type: application/json' -d'{ "query": {"match": { "address": "mill lane" }}}'
此示例是match(match_phrase)的变体,返回地址中包含短语“mill lane”的所有帐户:
GET /bank/_search
{
"query": {"match_phrase": {
"address": "mill lane"
}}
}
-------------------------------
curl -XGET "http://localhost:9200/bank/_search" -H 'Content-Type: application/json' -d'{ "query": {"match_phrase": { "address": "mill lane" }}}'
现在我们来介绍bool query。bool
query允许我们使用布尔逻辑将较小的查询组合成较大的查询。
此示例包含两个匹配查询,并返回地址中包含“mill”和“lane”的所有帐户:
GET /bank/_search
{
"query": {
"bool": {
"must": [
{"match": {
"address": "mill"
}},
{"match": {
"address": "lane"
}}
]
}
}
}
--------------------------
curl -XGET "http://localhost:9200/bank/_search" -H 'Content-Type: application/json' -d'{ "query": { "bool": { "must": [ {"match": { "address": "mill" }}, {"match": { "address": "lane" }} ] } }}'
在上面的示例中,bool must子句指定文档被视为匹配项时必须为true的所有查询。
相反,此示例包含两个匹配查询,并返回地址中包含“mill”或“lane”的所有帐户:
GET /bank/_search
{
"query": {
"bool": {
"should": [
{"match": {
"address": "mill"
}},
{"match": {
"address": "lane"
}}
]
}
}
}
-----------------------------
curl -XGET "http://localhost:9200/bank/_search" -H 'Content-Type: application/json' -d'{ "query": { "bool": { "should": [ {"match": { "address": "mill" }}, {"match": { "address": "lane" }} ] } }}'
在上面的示例中,bool should子句指定一个查询列表,其中任何一个查询为true,文档被视为匹配。
此示例包含两个match 查询,并返回地址中既不包含“mill”也不包含“lane”的所有帐户:
GET /bank/_search
{
"query": {
"bool": {
"must_not": [
{"match": {
"address": "mill"
}},
{"match": {
"address": "lane"
}}
]
}
}
}
---------------------------
curl -XGET "http://localhost:9200/bank/_search" -H 'Content-Type: application/json' -d'{ "query": { "bool": { "must_not": [ {"match": { "address": "mill" }}, {"match": { "address": "lane" }} ] } }}'
在上面的示例中,bool must_not子句指定一个查询列表,其中任何一个查询都不能为真,文档才能被视为匹配。
我们可以在bool查询中同时组合must、should和must-not子句。此外,我们可以在这些bool子句中组合bool查询,以模拟任何复杂的多级布尔逻辑。
此示例返回40岁但不在ID(AHO)中居住的任何人的所有帐户:
GET /bank/_search
{
"query": {
"bool": {
"must": [
{"match": {
"age": "40"
}}
],
"must_not": [
{"match": {
"state": "ID"
}}
]
}
}
}
---------------------------------
curl -XGET "http://localhost:9200/bank/_search" -H 'Content-Type: application/json' -d'{ "query": { "bool": { "must": [ {"match": { "age": "40" }} ], "must_not": [ {"match": { "state": "ID" }} ] } }}'