2

Legend:

' is the quotation mark for the better reading of each string value.
(star) is *

string values like 'Karlsruhe-Durlach', 'Karlsruhe', 'Karlsruhe)Südstadt' and 'München' are stored in elastic search.

My problem:

A search string like '(star)ruhe-Durl(star)' should be matched with the stored string 'Karlsruhe-Durlach'.

My idea was:

GET _search
{
    "query": {
        "query_string": {
          "query": "*ruhe-Durl*"
        }     
    }
}

=> No result

If the string '"Karlsruhe-Durlach"' is searched:

GET _search
{
    "query": {
        "query_string": {
          "query": "\"Karlsruhe-Durlach\""
        }     
    }
}

=> 'Karlsruhe-Durlach' will be the result.

If the string '(star)"rlsruhe-Durlac"(star)' is searched:

GET _search
{
    "query": {
        "query_string": {
          "query": "*\"rlsruhe-Durlac\"*",
          "default_field" : "city"
        }     
    }
}

=> 'Karlsruhe-Durlach', 'Karlsruhe', 'Karlsruhe)Südstadt' and 'München' will be the result.

If the string '(star)urlac(star)' is searched:

GET _search
{
    "query": {
        "query_string": {
          "query": "*urlac*"
        }     
    }
}

=> 'Karlsruhe-Durlach' will be the result.

The index was created as follows:

{
  "settings":{
     "index":{
        "analysis":{
           "analyzer":{
              "german":{
                 "tokenizer":"keyword",
                 "filter":"standard"
              }
           }
        }
     }
  },
 "mappings":{
     "organization":{
        "properties":{
           "id":{
              "analyzer": "german",
              "index": "not_analyzed",
              "type":"string"
           }
        }
     }
  }
}

How should the infix search string with hyphen be written? A infix search string like '(star)ruhe-Durl(star)' should be matched only with 'Karlsruhe-Durlach'. Is there an error at the infix search string or at the index creation?

bad_coder
  • 11,289
  • 20
  • 44
  • 72
Phil
  • 21
  • 1

1 Answers1

0

You can use the Wildcard query: https://www.elastic.co/guide/en/elasticsearch/guide/current/_wildcard_and_regexp_queries.html

 {
       "query": { 
           "wildcard":{
              "id": "*rlsruhe-Durlac*"
            }
       }
    }

This will help you search infix.

Divya Sriram
  • 138
  • 6