5

I'm doing an Elasticsearch Query DSL query on ELK such as:

{
  "query": {
    "wildcard": {
      "url.path": {
        "value": "*download*",
        "boost": 1,
        "rewrite": "constant_score"
      }
    }
  }
}

but it seems is case sensitive (so show only info with "download", not "Download" or "DOWNLOAD"). i.e. is case sensitive.

can I disable this? and search case insensitive?

Version used: 7.9.1

markzzz
  • 47,390
  • 120
  • 299
  • 507

3 Answers3

1

The below query will help you perform case-insensitive search as it will fetch results for *download, *Download and *DOWNLOAD. You may replace with your index and with the field you would like to perform this search.

Search Query

   GET /<my-index>/_search
    {
     "query" : {
         "bool" : {
            "must" : {
               "query_string" : {
                   "query" : "*download",
                   "fields": ["<field1>"]
               }
            }
         }
     }
}

If you wish to perform the same search on multiple fields, you can add the same in list.

Search on multiple fields

GET /<my-index>/_search
    {
     "query" : {
         "bool" : {
            "must" : {
               "query_string" : {
                   "query" : "*download",
                   "fields": ["<field1>","<field2>","field3>"]
               }
            }
         }
     }
}
Asmita Khaneja
  • 355
  • 1
  • 8
  • Nope. If I search "*download*" or "*Download*" it gives to me two different result sets. I've used this `{ "query" : { "bool" : { "must" : { "query_string" : { "query" : "*Download*", "fields": ["url.path"] } } } } }` – markzzz Oct 11 '21 at 09:37
1

There is a case_insensitive parameter available for wildcard query, but it was introduced in Elasticsearch 7.10.0, so you need to upgrade if you are still on 7.9.1.

If you can upgrade to 7.10.0 or higher:

Ideally, in index mapping field should use wildcard type:

{
  "mappings": {
    "properties": {
      "url.path": {
        "type": "wildcard"
      }
    }
  }
}

Then a wildcard query with case insensitivity enabled will find all the variants ("download", "DOWNLOAD", "download", etc)

{
  "query": {
    "wildcard": {
      "url.path": {
        "value": "*download*",
        "boost": 1,
        "rewrite": "constant_score",
        "case_insensitive": true
      }
    }
  }
}

If you must remain at 7.9.1:

Define your mapping in such a way that Elasticsearch treats the field contents as lowercase. The following will mimic wildcard type (it's a keyword, so only one token) indexed as lowercase.

{
  "mappings": {
    "properties": {
      "url": {
        "type": "text",
        "analyzer": "lowercase-keyword"
      }
    }
  },
  "settings": {
    "analysis": {
      "analyzer": {
        "lowercase-keyword": {
          "type": "custom",
          "tokenizer": "keyword",
          "filter": "lowercase"
        }
      }
    }
  }
}

The query, without the case_insensitive parameter which is unsupported in this version:

{
  "query": {
    "wildcard": {
      "url": {
        "value": "*download*",
        "boost": 1,
        "rewrite": "constant_score"
      }
    }
  }
}

Example results (note that searching for "*download*" and "*DoWnLoAd*" with both work in the same way):

{
  "hits": {
    "total": {
      "value": 3,
      "relation": "eq"
    },
    "max_score": 1.0,
    "hits": {
    "total": {
      "value": 3,
      "relation": "eq"
    },
    "max_score": 1.0,
    "hits": [
      {
        "_index": "my-index",
        "_type": "_doc",
        "_id": "PtbQe3wByTvslqtrs7Cn",
        "_score": 1.0,
        "_source": {
          "url": "http://example.com/download"
        }
      },
      {
        "_index": "my-index",
        "_type": "_doc",
        "_id": "P9bQe3wByTvslqtrvbDt",
        "_score": 1.0,
        "_source": {
          "url": "http://example.com/Download"
        }
      },
      {
        "_index": "my-index",
        "_type": "_doc",
        "_id": "QNbQe3wByTvslqtrzbDw",
        "_score": 1.0,
        "_source": {
          "url": "http://example.com/DOWNLOAD"
        }
      }
    ]
  }
}
Michał Szajbe
  • 8,830
  • 3
  • 33
  • 39
-1

You can use case_insensitive parameter for wildcard query. This parameter was introduced in 7.10.0 version

Adding a working example with index data, mapping, search query, and search result

Index Mapping:

{
  "mappings": {
    "properties": {
      "url": {
        "properties": {
          "path": {
            "type": "wildcard"
          }
        }
      }
    }
  }
}

Index Data:

{
  "url":{
    "path":"xx/download"
  }
}

Search Query:

{
  "query": {
    "wildcard": {
      "url.path": {
        "value": "*Download*",
        "boost": 1,
        "rewrite": "constant_score",
         "case_insensitive": false
      }
    }
  }
}

Search Result:

No results will be there when you are searching for *Download* or *DOWNLOAD*

Update:

You can use the wildcard query with "case_insensitive": true parameter

Adding a sample index data, search query, and search result

Index Data:

{
  "url": {
    "path": "download"
  }
}
{
  "url": {
    "path": "DOWNLOAD"
  }
}
{
  "url": {
    "path": "Download"
  }
}

Search Query:

{
  "query": {
    "wildcard": {
      "url.path": {
        "value": "*DOWNLOAD*",
        "boost": 1,
        "rewrite": "constant_score",
        "case_insensitive": true
      }
    }
  }
}

Search Result:

"hits": [
      {
        "_index": "67210888",
        "_type": "_doc",
        "_id": "1",
        "_score": 1.0,
        "_source": {
          "url": {
            "path": "download"
          }
        }
      },
      {
        "_index": "67210888",
        "_type": "_doc",
        "_id": "2",
        "_score": 1.0,
        "_source": {
          "url": {
            "path": "Download"
          }
        }
      },
      {
        "_index": "67210888",
        "_type": "_doc",
        "_id": "3",
        "_score": 1.0,
        "_source": {
          "url": {
            "path": "DOWNLOAD"
          }
        }
      }
    ]
ESCoder
  • 15,431
  • 2
  • 19
  • 42
  • 1
    I think user is asking for just the opposite. case_insensitive should be true – Barkha Jain Apr 22 '21 at 13:28
  • yes. But both doesn't works. I've 7.9.1. Any alternatives? – markzzz Apr 22 '21 at 14:50
  • @markzzz if you want the result to be case insensitive, then I think your query should work. Can you please share some sample index data, which you are using ? – ESCoder Apr 22 '21 at 15:31
  • @markzzz any update here? If you are not getting the expected result, please share your sample index data, so that I can provide a working example for that – ESCoder Apr 23 '21 at 08:01
  • not sure how to share data. its within elk. I just place the wildcard, and nothing happens (i.e. it does error message) – markzzz Apr 23 '21 at 12:51
  • @markzzz that means there is no index mapping also which you have configured? – ESCoder Apr 23 '21 at 12:54
  • i did configured nothing, just place the query you posted – markzzz Apr 23 '21 at 13:01
  • @markzzz please go through the updated part of the answer, and let me know if this resolves your issue ? – ESCoder Oct 13 '21 at 17:24
  • Nope. i can discriminate all the combinations. What if the content is "DoWnlOAd"? I need a one-written rule – markzzz Oct 13 '21 at 17:26
  • @ESCoder case insensitive is not supported in this version, as already stated – markzzz Oct 14 '21 at 05:09