Joseph Bou Saada
Joseph Bou Saada

Reputation: 13

ElasticSearch not filtering

I am trying to filter based on the categoryCode and I am not getting any result back,

I have tried using a filed that isn't analyzed and it worked, how can I make it work for this specific field?

  "categoryCode": {
  "type": "text",
  "analyzer": "autocomplete",
  "search_analyzer": "standard",
  "norms": false,
  "copy_to": "all_fields",
  "doc_values": false,
  "fields": {
    "raw": {
      "type": "keyword",
      "index": false,
      "normalizer": "lowercase_normalizer",
      "norms": false
    },
    "fulltext": {
      "type": "text",
      "analyzer": "standard",
      "doc_values": false
    }
  }
}

query for filtering:

   "bool":{
      "filter":[
         {
            "bool":{
               "must":[
                  {
                     "term":{
                        "categoryCode":{
                           "value":"PAYROLL"
                        }
                     }
                  }
               ]
            }
         }
      ]
   }
}

Upvotes: 1

Views: 77

Answers (1)

Amit
Amit

Reputation: 32376

You are searching on categoryCode which is analyzed, hence goes through the analysis phase and creates tokens according to your standard analyzer(search analyzer) which lowercases the tokens as well.

Instead of categoryCode , you need to search on categoryCode.raw which contains the keyword form of your document. Also you might have to remove lowercase_normalizer as in your query, you are using term query which wouldn't go through your lowercase normalizer on raw field, and could cause PAYROLL to not match with payroll.

I've created sample index definition, indexed document and modified your search query to get the expected results.(Your entire mapping wasn't available, hence created a bare minimum example to explain)

Index def (minimum)

{
    "mappings": {
        "properties": {
            "categoryCode": {
                "type": "text",
                "fields": {
                    "raw": {
                        "type": "keyword",
                        "norms": false
                    },
                    "fulltext": {
                        "type": "text",
                        "analyzer": "standard",
                        "doc_values": false
                    }
                }
            }
        }
    }
}

Index doc

{
   "categoryCode" : "PAYROLL"
}

Search query

{
    "query": {
        "bool": {
            "filter": [
                {
                    "bool": {
                        "must": [
                            {
                                "term": {
                                    "categoryCode.raw": { -->notice `.raw`
                                        "value": "PAYROLL"
                                    }
                                }
                            }
                        ]
                    }
                }
            ]
        }
    }
}

Search result

 "hits": [
         {
            "_index": "so-60531341",
            "_type": "_doc",
            "_id": "1",
            "_score": 0.0,
            "_source": {
               "categoryCode": "PAYROLL"
            }
         }
      ]

Upvotes: 1

Related Questions