简体   繁体   English

Elasticsearch忽略查询字符串

[英]Elasticsearch ignores query string

I am using Elasticsearch to query jobs in my database. 我正在使用Elasticsearch查询数据库中的作业。 Below is the query that I am using. 下面是我正在使用的查询。

The query should ask for the following : 该查询应要求以下内容:
-Query matches text, name or description -查询匹配文本,名称或描述
-Job must contain all of the given categories and segments -工作必须包含所有给定的类别和细分

However the problem that i'm having, is that when i give a query, and add segments and categories. 但是,我遇到的问题是当我进行查询并添加细分和类别时。 The query is ignored appearantly, and the request returns ALL jobs with the given segments and categories. 该查询被表面上忽略,并且该请求返回具有给定细分和类别的所有作业。

{
  "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"
        }
      }
    }
  }
}

As a sidenote, i am using laravel and elasticsearch's php implementation, and the above is the json_encoded representation of the query array. 附带说明一下,我正在使用laravel和elasticsearch的php实现,而上面是查询数组的json_encoded表示形式。 (types could have been juggled) (类型本来可以杂耍)

Ok, i figured it out myself: 好吧,我自己弄清楚了:

Replacing the should parameters in the bool query with a multi_match in the must clause will solve my problem. 用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"
                }
              }
            ]
          }
        },

It turned out pretty obvious because it was noted in the documentation: 事实证明它很明显,因为它在文档中有所说明:

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 . 在没有must子句的布尔查询中,一个或多个should子句必须与document匹配 The minimum number of should clauses to match can be set using the minimum_should_match parameter. 可以使用minimum_should_match参数设置应匹配的should子句的最小数量。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM