【问题标题】:Elastic Search v6.3: Query with filter never returns any matchesElastic Search v6.3:使用过滤器的查询永远不会返回任何匹配项
【发布时间】:2019-05-08 17:13:11
【问题描述】:

我在使用 Elastic Search 时遇到了一些挑战。我想通过一些文本进行查询,然后根据类别进行过滤。我关注了Elastic Search 6.3 Documentation for Queries,但我对ES 的回复总是空的。我知道我至少有一个条目应该匹配请求。下面我已将我的查询发布到 Elastic Search,并且我知道的条目存在于我的 Elastic Search 索引中。很感谢任何形式的帮助。

查询

{
    "from": 0,
    "size": 300,
    "query": {
      "bool": {
        "filter": {
          "term": {"category": "Soups"}
        },
        "should": [
          {"term": {"instructions": "Matt"}},
          {"term": {"introduction": "Matt"}},
          {"term": {"recipe_name": "Matt"}},
        ],
        "minimum_should_match": 1,
        "boost": 1.0
      }
    }
  }

记录存在于 Elastic Search 中

        {
            "_index": "recipes",
            "_type": "_doc",
            "_id": "QMCScWoBkkkjW61rD81v",
            "_score": 0.2876821,
            "_source": {
                "calories": 124,
                "category": "Soups",
                "cook_time": {
                    "hour": "2",
                    "min": "4"
                },
                "cooking_temp": "375",
                "cooking_temp_units": "°F",
                "creator_username": "virtualprodigy",
                "ingredients": [
                    {
                        "majorQuantity": "1 ",
                        "measuring_units": "teaspoon",
                        "minorQuantity": " ",
                        "name": "mett"
                    }
                ],
                "instructions": "instructions",
                "introduction": "intro",
                "prep_time": {
                    "hour": "1",
                    "min": "2"
                },
                "recipe_name": "Matt Test",
                "servings": 1
            }
        }

【问题讨论】:

  • 您正在使用术语查询来获取说明、介绍和食谱名称以全部匹配 Matt。词条查询与词条完全匹配 see docs,并且这些词条都没有完全匹配Matt。介绍=“介绍”,说明=“说明”和配方名称=“马特测试”。尝试使用多重匹配查询和分析器将查询和索引分解为简单的术语。您使用的过滤器似乎没有问题。

标签: elasticsearch aws-elasticsearch


【解决方案1】:

您的字段可能使用标准分析器编制索引,这意味着它们被拆分为标记并小写。 term query 是完全匹配的,不执行此分析,因此您正在寻找“马特”,它只有“马特”。你寻找“汤”,它只有“汤”。最简单的解决方法是将您的术语查询更改为match queries。例如:

{
    "from": 0,
    "size": 300,
    "query": {
        "bool": {
            "filter": {
                "match": {
                    "category": "Soups"
                }
            },
            "should": [
                {"match": {"instructions": "Matt"}},
                {"match": {"introduction": "Matt"}},
                {"match": {"recipe_name": "Matt"}}
            ],
            "minimum_should_match": 1,
            "boost": 1.0
        }
    }
}

【讨论】:

  • 我认为你可能是对的。我今晚会试试这个。希望它有效。我还将研究不同的分析器,看看我是否可以升级我的映射。
  • 终于试了一下,效果很好。我将不得不稍后返回并更新我的索引分析器以便更好地搜索。目前,这将解决我对 POC 的需求。非常感谢
猜你喜欢
  • 1970-01-01
  • 2021-02-20
  • 1970-01-01
  • 1970-01-01
  • 2021-05-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多