0
votes

I'm facing some challenges with Elastic Search. I want to query for by some text and then filter based on a category. I followed the Elastic Search 6.3 Documentation for Queries but my response for ES is always empty. I know for a fact that I have at least one entry that should match the request. Below I have posted my query to Elastic Search and the entry that I know is present in my Elastic Search index. Any help is very much appreciated.

Query

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

Record Present in 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
You are using term queries for instructions, introduction, and recipe name to all match Matt. Term queries match the term exactly see docs, and none of those terms match Matt exactly. introductions = "intro", instructions = "instructions", and recipe_name = "Matt Test". Try using a multi match query and an analyzer to break down the query and index into simple terms. There doesn't seem to be a problem with the filter you are using.cb64

1 Answers

2
votes

Your fields are probably indexed using a standard analyser, which means they are split into tokens and lowercased. The term query is an exact match and does not perform this analysis, so you are looking for 'Matt' and it only has 'matt'. You look for 'Soups' and it only has 'soups'. The easiest fix is to change your term queries into match queries. e.g:

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