Elasticsearch 忽略查询字符串

Elasticsearch ignores query string

我正在使用 Elasticsearch 查询数据库中的作业。下面是我正在使用的查询。

查询应询问以下内容:
-查询匹配文本、名称或描述
- 工作必须包含所有给定的类别和细分

但是我遇到的问题是,当我提出查询并添加细分和类别时。查询在外观上被忽略,并且请求 returns 具有给定细分和类别的所有作业。

{
  "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 表示。 (类型可能已经杂耍)

好的,我自己想出来了:

用 must 子句中的 multi_match 替换 bool 查询中的 should 参数将解决我的问题。

{
  "index": "jobs",
  "type": "job",
  "body": {
    "query": {
      "filtered": {
        "query": {
          "bool": {
            "must": [
              {
                "multi_match": {
                  "query": "php",
                  "fields": [
                    "name",
                    "text",
                    "description"
                  ]
                }
              },
              {
                "match": {
                  "segments": "10"
                }
              }
            ]
          }
        },

结果很明显,因为它在文档中有注明:

The clause (query) should appear in the matching document. In a boolean query with no must clauses, one or more should clauses must match a document. The minimum number of should clauses to match can be set using the minimum_should_match parameter.

http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/query-dsl-bool-query.html#query-dsl-bool-query