Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

wildcard queries with multiple fields in ES?

Can anyone please tell me how can i write wildcard queries with multiple fields in elasticsearch i have searched a lot on this but some one told me to use query string or multi match but the problem in my case is query string is not working for me my code is given below for a single field wildcard query if anyone know anything about this please share some light on this

"query": {
      "wildcard" : { "places_area1.city.raw_wildcard" : last_wildcard_string }
 }

UPDATE Mappings

"settings": {
        "index": {
          "analysis": {
            "analyzer": {
              "synonym_wildcard": {
                "tokenizer": "whitespace",
                "filter": ["filter_wildcard"]
              },
              "synonym_term": {
                "tokenizer": "keyword",
                "filter": ["filter_term"]
              },
              "simple_wildcard": {
                "tokenizer": "whitespace"
              }
            },
            "filter": {
              "filter_term": {
                "tokenizer": "keyword", // here you have to write this only for tokenizer keyword but not for whitespace
                "type": "synonym",
                "synonyms_path": "synonyms.txt",
              },
              "filter_wildcard": {
                "type": "synonym",
                "synonyms_path": "synonyms.txt",
              }
            }
          }
        }
      },
      mappings : {
        places_area1: {
          properties:{
            area1         : {"type" : "string", "index": "analyzed", "analyzer": "simple_wildcard"},
            city         : {"type" : "string", "fields": {
              "raw": {
                "type": "string",
                "analyzer": "synonym_term"
              },
              "raw_wildcard": {
                "type": "string",
                "analyzer": "synonym_wildcard"
              }
            } },
          }
        }
      }
    }

Thank You In Advance

like image 719
aman verma Avatar asked Nov 19 '15 22:11

aman verma


1 Answers

Are you looking for something like this?

{
  "query": {
    "query_string": {
      "fields": ["title", "description", "state"], 
      "query": "Ban*",
      "lowercase_expanded_terms": false
    }
  }
}

Why do you need "lowercase_expanded_terms": false?

Since you are using keyword and whitespace tokenizer, word Bangalore is indexed as Bangalore and while using query string wildcard Ban* is lowercased because it is default setting and ES is looking for words starting with ban and hence it does not find Bangalore. You could also resolve this by adding lowercase filter to your mappings

Also it was working previously because you did not specify any analzyer and by default ES uses standard analzyer and it internally uses lowercase token filter

like image 58
ChintanShah25 Avatar answered Nov 15 '22 07:11

ChintanShah25