【问题标题】:Elasticsearch ignores query stringElasticsearch 忽略查询字符串
【发布时间】:2015-03-04 20:24:35
【问题描述】:

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

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

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

{
  "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 表示。 (类型可以被玩弄)

【问题讨论】:

  • 作为建议,发送到弹性不是字符串“23”,而是 int 23,因为它有比较问题(例如:“23”!=23)
  • 这是由于我使用 elasticsearchs php 实现,上面是查询数组的 json 编码表示(可能不完全是发送到 ES0 的内容。

标签: php laravel elasticsearch


【解决方案1】:

好的,我自己想通了:

将 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 子句必须 匹配文档。要匹配的应子句的最小数量可以是 使用 minimum_should_match 参数设置。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-10-22
    • 1970-01-01
    • 2011-03-04
    • 1970-01-01
    • 1970-01-01
    • 2015-04-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多