0
votes

Hello all i am facing two problems in ES

  1. I have a 'city' 'New York' in ES now i want to write a term filter such that if given string exactly matches "New York" then only it returns but what is happening is that when my filter matches "New" OR "York" for both it returns "New York" but it is not returning anything for "New York" my mapping is given below please tell me which analyzer or tokenizer should i use inside mapping

Here are the settings and mapping:

"settings": {
        "index": {
          "analysis": {
            "analyzer": {
              "synonym": {
                "tokenizer": "whitespace",
                "filter": ["synonym"]
              }
            },
            "filter": {
              "synonym": {
                "type": "synonym",
                "synonyms_path": "synonyms.txt"
              }
            }
          }
        }
      },
      mappings : {
        "restaurant" : {
          properties:{
            address         : {
                properties:{
                    city         : {"type" : "string", "analyzer": "synonym"},
                }
            }
          }
        }
  1. Second problem is that when i am trying to use wildcard query on lowercase example "new*" then ES is not returning not anything but when i am trying to search uppercase example "New*" now it is returning "New York" now i in this second case i want to write my city mappings such that when i search for lowercase or uppercase for both ES returns the same thing i have seen ignore case and i have set it to false inside synonyms but still i am not able to search for both lowercase and uppercases.

         "synonym": {
                "type": "synonym",
                "synonyms_path": "synonyms.txt",
                "ignore_case": true   // See here 
              }
    
1
how is your synonym file? is it like ny, nyc , new york city => new york or ny, nyc ,new york city, new york because the way you specify synonyms will have implications - ChintanShah25
new york is not there in synonyms file - aman verma
@amanverma provide the content of synonyms.txt file relevant to the New York search. - Andrei Stefan
no synonyms file has nothing to do with ne york i have different places there leme show you what it contains mumbai, bombay calcutta, kolkata puducherry, pondicherry Bangalore, Bengaluru - aman verma

1 Answers

1
votes

I believe you didn't provide enough details, but hoping that my attempt will generate questions from you, I will post what I believe it should be a step forward:

The mapping:

PUT test
{
  "settings": {
    "index": {
      "analysis": {
        "analyzer": {
          "synonym": {
            "tokenizer": "whitespace",
            "filter": [
              "synonym"
            ]
          },
          "keyword_lowercase": {
            "type": "custom",
            "tokenizer": "keyword",
            "filter": [
              "lowercase"
            ]
          }
        },
        "filter": {
          "synonym": {
            "type": "synonym",
            "synonyms_path": "synonyms.txt",
            "ignore_case": true
          }
        }
      }
    }
  },
  "mappings": {
    "restaurant": {
      "properties": {
        "address": {
          "properties": {
            "city": {
              "type": "string",
              "analyzer": "synonym",
              "fields": {
                "raw": {
                  "type": "string",
                  "index": "not_analyzed"
                },
                "raw_ignore_case": {
                  "type": "string",
                  "analyzer": "keyword_lowercase"
                }
              }
            }
          }
        }
      }
    }
  }
}

Test data:

POST /test/restaurant/1
{
  "address": {"city":"New York"}
}
POST /test/restaurant/2
{
  "address": {"city":"new york"}
}

Query for the first problem:

GET /test/restaurant/_search
{
  "query": {
    "filtered": {
      "filter": {
        "term": {
          "address.city.raw": "New York"
        }
      }
    }
  }
}

Query for the second problem:

GET /test/restaurant/_search
{
  "query": {
    "query_string": {
      "query": "address.city.raw_ignore_case:new*"
    }
  }
}