0
votes

I have 2 indices, cities and places. Places one has a mapping like this:

{
    "mappings": {
        "properties": {
            "cityId": {
                "type": "integer"
            },
            "cityName": {
                "type": "text"
            },
            "placeName": {
                "type": "text"
            },
            "status": {
                "type": "keyword"
            },
            "category": {
                "type": "keyword"
            },
            "reviews": {
                "properties": {
                    "rating": {
                        "type": "long"
                    },
                    "comment": {
                        "type": "keyword"
                    },
                    "user": {
                        "type": "nested"
                    }
                }
            }
        }
    }
}

And City is index is mapped like this:

{
    "mappings": {
        "properties": {
            "state": {
                "type": "keyword"
            },
            "postal": {
                "type": "keyword"
            },
            "phone": {
                "type": "keyword"
            },
            "email": {
                "type": "keyword"
            },
            "notes": {
                "type": "keyword"
            },
            "status": {
                "type": "keyword"
            },
            "cityName": {
                "type": "text"
            },
            "website": {
                "type": "keyword"
            },
            "cityId": {
                "type": "integer"
            }
        }
    }
}

Initially we had a single document where cities had places embedded but I was having trouble searching nested places array so I changed the structure to this, I want to be able to search both cityName and placeName in a single query with fuzziness. I have a city including the word Welder's in it's name and also the some places inside the same location have the word Welder's in their name, which have a type:text. However when searched for welder both of the following queries see below don't return these documents, a search for welders OR welder's does return these documents. I am not sure why welder won't match with Welder's*. I didn't specify any analyzer during the creation of both the indices and neither am I explicitly defining it in the query can anyone help me out with this query so it behaves as expected:

Query 1: index = places


{
    "query": {
        "bool": {
            "should": [
                {
                    "match": {
                        "placeName": {
                            "query": "welder",
                            "fuzziness": 20
                        }
                    }
                },
                 {
                    "match": {
                        "cityName": {
                            "query": "welder",
                            "fuzziness": 20
                        }
                    }
                }

            ]
        }
    }
}

Query 2: index = places

{
    "query": {
        "match": {
            "placeName": {
                "query": "welder",
                "fuzziness": 20
            }
        }
    }
}

Can anyone post a query that when passed a word welder would return documents having Welder's in their name (should also work for other terms like these, this is just an example)

Edit 1 : This is a sample place document I would want to be returned by any of the queries posted above:

{
   cityId: 29,
   placeName: "Welder's Garage Islamabad",
   cityName: "Islamabad",
   status: "verified",
   category: null,
   reviews: []
}

1
Please add a sample document which should be returned in queryjaspreet chahal
Hi @jaspreetchahal I have added a sample document, please check he original question above. ThanksAbby Khan

1 Answers

0
votes

Using your mapping and query and fuzziness set as "20" I am getting document back. Fuzziness: 20 will tolerate 20 edit distance between searched word and welder's so even "w" will match with "welder's". I think this value is different in your actual query.

If you want to search for welder or welders and return welder's then you can use stemmer token filter

Mapping:

PUT indexfuzzy
{
  "mappings": {
    "properties": {
      "cityId": {
        "type": "integer"
      },
      "cityName": {
        "type": "text"
      },
      "placeName": {
        "type": "text",
        "analyzer": "my_analyzer"
      },
      "status": {
        "type": "keyword"
      },
      "category": {
        "type": "keyword"
      },
      "reviews": {
        "properties": {
          "rating": {
            "type": "long"
          },
          "comment": {
            "type": "keyword"
          },
          "user": {
            "type": "nested"
          }
        }
      }
    }
  },
  "settings": {
    "analysis": {
      "analyzer": {
        "my_analyzer": {
          "tokenizer": "standard",
          "filter": [
            "lowercase",
            "stem_possessive_english",
            "stem_minimal_english"
          ]
        }
      },
      "filter": {
        "stem_possessive_english": {
          "type": "stemmer",
          "name": "possessive_english"
        },
        "stem_minimal_english": {
          "type": "stemmer",
          "name": "minimal_english"
        }
      }
    }
  }
}

Query :

GET indexfuzzy/_search
{
    "query": {
        "bool": {
            "should": [
                {
                    "match": {
                        "placeName": {
                           "query": "welder"--> welder,welders,welder's will work
                        }
                    }
                },
                 {
                    "match": {
                        "cityName": {
                            "query": "welder"
                        }
                    }
                }

            ]
        }
    }
}

Result:

[
      {
        "_index" : "indexfuzzy",
        "_type" : "_doc",
        "_id" : "Jc-yx3ABd7NBn_0GTBdp",
        "_score" : 0.2876821,
        "_source" : {
          "cityId" : 29,
          "placeName" : "Welder's Garage Islamabad",
          "cityName" : "Islamabad",
          "status" : "verified",
          "category" : null,
          "reviews" : [ ]
        }
      }
    ]

possessive_english:- removes trailing 's from tokens minimal_english:- removes plurals

GET <index_name>/_analyze
{
  "text": "Welder's Garage Islamabad",
  "analyzer": "my_analyzer"
}

returns

{
  "tokens" : [
    {
      "token" : "welder", --> will be matched for welder's, welders
      "start_offset" : 0,
      "end_offset" : 8,
      "type" : "<ALPHANUM>",
      "position" : 0
    },
    {
      "token" : "garage",
      "start_offset" : 9,
      "end_offset" : 15,
      "type" : "<ALPHANUM>",
      "position" : 1
    },
    {
      "token" : "islamabad",
      "start_offset" : 16,
      "end_offset" : 25,
      "type" : "<ALPHANUM>",
      "position" : 2
    }
  ]
}