【问题标题】:Elasticsearch Search query with multiple filters具有多个过滤器的 Elasticsearch 搜索查询
【发布时间】:2017-06-17 22:03:55
【问题描述】:

大家好,我是弹性搜索的新手,但我已经阅读了基本的 ElasticSearch 5.1 文档。

一行的问题:

搜索成功,但过滤器无法正常工作。

映射数据类型

{
    "properties": {
        "title": {"type": "string"},
        "description": {"type": "string"},
        "slug": {"type": "string"},
        "course_type": {"type": "string", "index" : "not_analyzed"},
        "price": {"type": "string"},
        "categories": {"type": "keyword", "index" : "not_analyzed"},
        "tags": {"type" : "keyword"},
        // "tags": {"type" : "keyword", "index" : "not_analyzed"},
        "status": {"type" : "string","index" : "not_analyzed"},
    }
}

正如@Darth_Vader 所述,我也尝试了映射。以下是我的映射

索引中的文档 (Req-1)

....
{
    "_index": "learnings",
    "_type": "materials",
    "_id": "582d9xxxxxxxx9b27fab2c",
    "_score": 1,
    "_source": {
      "title": "Mobile Marketing",
      "slug": "mobile-marketing",
      "description": "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla eleifend hendrerit vehicula.",
      "categories": [
        "Digital Marketing"
      ],
      "tags": [
        "digital-marketing",
        "mobile-marketing"
      ],
      "status": "published"
    }
},
...

和上面一样,我在索引中有一百个文档

我正在使用的搜索查询已满

"query": {
    "bool": {
        "must": {
             "multi_match" : {
              "query" : "mobile",
              "fields" : [ "title^5", "tags^4", "categories^3" ],
              "operator": "and"
            }
        },
        "filter": {
            "bool" : {
                "must" : [
                    {"term" : {"status": "published"} }
                ]
             }
         }
    }
}
  • 在上述查询中,最重要的搜索条件/过滤器是 {"term" : {"status": "published"} }。每个搜索结果都必须满足这个 要求。

  • 现在从结果列表中,我想过滤更多。因此,假设我只想获得具有 mobile-marketing 作为 tagdocuments。我的文档 (Req-1) 有这个 tag (mobile-marketing)

现在的问题是:

如果我修改我的 Search Query 并添加我所需的 filter,如下所示:我得到 NO 搜索结果 (hits = 0)即使我的文档 (Req-1) 具有 mobile-marketing 作为 tag

"filter": {
    "bool" : {
        "must" : [
            {"term" : {"status": "published"} },
            {"term" : {"tags": "mobile-marketing"} }
        ]
     }
 }

但是如果我更改过滤器 {"tags": "mobile-marketing"} TO {"tags": "mobile"},我会得到所需的文档 (Req-1) 作为结果.

我想使用此过滤器获取相同的文档:{"tags": "mobile-marketing"}。那么我在哪里做错了?

我的搜索查询需要进行哪些修改?

谢谢

【问题讨论】:

  • [对于有类似问题的人]。上述过程和相关代码有效,请确保在映射和其他相关操作时使用正确的索引名称和索引类型

标签: node.js elasticsearch


【解决方案1】:

您的映射如何查找tags

您似乎已将tags 字段的映射分析。 *analyzed` 所做的是,来自books:

首先分析字符串,然后索引它。换句话说,索引这个 字段为全文。

因此它首先对其进行分析,其价值类似于 mobile-marketing。因此,由于中间的连字符,它将分开存储移动和营销,并将被标记为令牌。即:它将 mobilema​​rketing 存储到两个不同的令牌中。

如果它是 not_analyzed

索引这个字段,所以它是可搜索的,但索引值与 指定的。不要分析它。

因此,这将基本上按原样存储值而不对其进行分析,这在您的情况下应该可以解决问题。也许你也应该看看这个point

希望对你有帮助!

【讨论】:

  • 哦,谢谢@Darth_vader,我没有做任何映射,我只是创建了一个索引并开始在没有任何映射的情况下填充文档。我想我需要进行映射并将其设为 "type" : "string", "index" : "not_analyzed" 。让我检查它是否有效。谢谢
  • 是的,如果它有效,请告诉我@SiddharthaChowdhury。
  • 嘿@Darth_Vader,我尝试了映射并在问题中添加了我的映射详细信息。但它没有奏效。它的行为方式相同。
  • 如果你有 tags type 作为text 怎么办? “标签”:{“类型”:“文本”,“索引”:“未分析”}
  • 如果它的类型是 keyword 你不必显式设置 not_analyzed 因为 keyword 应该默认有它。可能是helpful。让我知道它是否有效。
猜你喜欢
  • 2021-08-27
  • 1970-01-01
  • 2019-03-26
  • 2020-08-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多