1

I'm using BoolqueryBuilder for searching values with java spring boot backend.
And I need to search in elastic search values with special characters Like '@#$%test test search'

  • Is there any way to fetch search values with querystring / matchquery / wildcard.
  • My elastic search index name is document. and field name is value
  • Is it needed to create custom analyzer for this search to happen.
  • Also it shouldn't affect normal text and numerical search And it should be case sensitive.

1 Answers1

0

Here is an example of how you can search your data

PUT index_name/_doc/1
{
  "field_name": "@#$%test test search"
}

GET index_name/_search
{
  "query": {
    "query_string": {
      "default_field": "field_name",
      "query": "(@#)*"
    }
  }
}

Reference: https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-query-string-query.html#_reserved_characters


The above method is not the best one, so I want to give more information and explain how you can search your data more efficiently. In the below picture, you will see how elasticsearch standard analyze works both during indexing and searching. enter image description here

If you don't need an analyzer (probably you don't need it in your scenario) you can use keyword field type which is not analyzed.

When you index the document as a string in Elasticsearch, ECS will automatically create 2 fields for each string, one of which will be named field_name of text type and the other will be named field_name.keyword of keyword type. Here is an example:

PUT index_name/_doc/1
{
  "field_name": "@#$%test test search"
}

GET index_name/_search
{
  "query": {
    "wildcard": {
      "field_name.keyword": "@#$*"
    }
  }
}

GET index_name/_search
{
  "query": {
    "query_string": {
      "default_field": "field_name.keyword",
      "query": "@#*%* AND *test*"
    }
  }
}

case insensitive

GET index_name/_search
{
  "query": {
    "wildcard": {
      "field_name.keyword": {
        "value": "*TesT*",
        "case_insensitive": true
      }
    }
  }
}
Musab Dogan
  • 1,811
  • 1
  • 6
  • 8