我正在使用Elasticsearch查询数据库中的作业。下面是我正在使用的查询。
查询应询问以下内容:
-查询匹配文本、名称或描述
-作业必须包含所有给定的类别和分段
然而,我遇到的问题是,当我给出一个查询,并添加分段和类别时。该查询被明显忽略,并且该请求返回具有给定分段和类别的所有作业。
{
"index": "jobs",
"type": "job",
"body": {
"query": {
"filtered": {
"query": {
"bool": {
"must": [
{
"match": {
"categories": "23"
}
},
{
"match": {
"segments": "10"
}
}
],
"should": [
{
"match": {
"name": "php"
}
},
{
"match": {
"text": "php"
}
},
{
"match": {
"description": "php"
}
}
]
}
},
"filter": {
"nested": {
"path": "networks",
"filter": {
"bool": {
"must": [
{
"term": {
"networks.id": 1
}
},
{
"term": {
"networks.status.raw": "PRODUCTION"
}
},
{
"range": {
"networks.start": {
"lte": "now"
}
}
},
{
"range": {
"networks.end": {
"gte": "now"
}
}
}
]
}
}
}
}
}
},
"aggs": {
"categories": {
"terms": {
"field": "categories"
}
},
"segments": {
"terms": {
"field": "segments"
}
}
}
}
}
附带说明一下,我使用的是laravel和elasticsearch的php实现,上面是查询数组的json_encoded表示。(类型可能被篡改)
好吧,我自己想好了:
将bool查询中的should参数替换为must子句中的multi_match将解决我的问题。
{
"index": "jobs",
"type": "job",
"body": {
"query": {
"filtered": {
"query": {
"bool": {
"must": [
{
"multi_match": {
"query": "php",
"fields": [
"name",
"text",
"description"
]
}
},
{
"match": {
"segments": "10"
}
}
]
}
},
它变得非常明显,因为它在文档中被注意到:
子句(查询)应出现在匹配的文档中在不包含must子句的布尔查询,必须有一个或多个should子句匹配文档。要匹配的should子句的最小数目可以是使用minimum_should_match参数设置。
http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/query-dsl-bool-query.html#query-dsl-bool查询