繁体   English   中英

Elasticsearch忽略查询字符串

[英]Elasticsearch ignores query string

我正在使用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表示形式。 (类型本来可以杂耍)

好吧,我自己弄清楚了:

用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"
                }
              }
            ]
          }
        },

事实证明它很明显,因为它在文档中有所说明:

子句(查询)应出现在匹配的文档中。 在没有must子句的布尔查询中,一个或多个should子句必须与document匹配 可以使用minimum_should_match参数设置应匹配的should子句的最小数量。

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