Elastic Search v6.3:带过滤器的查询从不返回任何匹配项

时间:2019-05-08 17:13:12

标签: elasticsearch aws-elasticsearch

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

1 个答案:

答案 0 :(得分:2)

您的字段可能使用标准分析器进行索引,这意味着它们被拆分为标记并以小写字母表示。 term query是精确匹配项,不会执行此分析,因此您正在寻找“ Matt”,并且仅包含“ matt”。您在寻找“汤”,而只有“汤”。最简单的解决方法是将词条查询更改为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
        }
    }
}