Home > front end >  Why is filter not working for elastic search?
Why is filter not working for elastic search?

Time:11-20

I have this query:

"query": {
        "bool": {
          "must": [
            {
              "query_string": {
                "query": "query",
                "fields": [
                  "some.field"
                ],
                "lenient": true,
                "default_operator": "OR"
              }
            }
          ],
          "filter": [
     
                                 {"term": {
                "test.name.enum": {
                  "value":"filterTerm"
                }
              }}
          ]

and the mapping for "test.name" is the following:

"test" : {
          "type" : "nested",
          "properties" : {
            "name" : {
              "type" : "text",
              "fields" : {
                "delimiter" : {
                  "type" : "text",
                  "index_options" : "freqs",
                  "analyzer" : "iq_text_delimiter"
                },
                "enum" : {
                  "type" : "keyword",
                  "ignore_above" : 2048
                },
                "joined" : {
                  "type" : "text",
                  "index_options" : "freqs",
                  "analyzer" : "i_text_bigram",
                  "search_analyzer" : "q_text_bigram"
                },
                "prefix" : {
                  "type" : "text",
                  "index_options" : "docs",
                  "analyzer" : "i_prefix",
                  "search_analyzer" : "q_prefix"
                },
                "sear_as_type" : {
                  "type" : "search_as_you_type",
                  "doc_values" : false,
                  "max_shingle_size" : 3
                },
                "stem" : {
                  "type" : "text",
                  "analyzer" : "iq_text_stem"
                },
                "synonym" : {
                  "type" : "text",
                  "analyzer" : "synonym_analyzer"
                }
              },
              "index_options" : "freqs",
              "analyzer" : "iq_text_base"
            }
            
          }
        

however when I try to filter it doesnt return any results despite making sure that a result does exist for that filter and returns 0 hits, enum here has type keyword which I thought is how you should filter? I am new to elasticsearch so any help appreciated

CodePudding user response:

Since test is declared nested, your filter needs to include a nested query as well:

  ...
  "filter": [
    {
      "nested": {
        "path": "test",
        "query": {
          "term": {
            "test.name.enum": {
              "value": "filterTerm"
            }
          }
        }
      }
    }
  ]
  • Related