Search code examples
elasticsearchnestelasticsearch-marvel

Fuzziness in date type filed


I have date field in my mapping and I want to do fuzzy search on my field Below is my code

GET _search
  {
       "query": {
        "fuzzy": {
             "death_date": {
              "value": "3548"              

         }
     }
   }
}

Current result don't return data based as per expectation. Although I have 3548 value it's score is less that 3549 value which appears on the top of the result I have changed my query to include range parameter as suggested

  GET _search
    {
     "query" : {
     "bool": {
     "must":   
    {
      "match": {
        "marriages.marriage_year": "1630"
      }
    },
   "should": 
      {
      "match": {
        "first_name":
        { "query" : "mary",
        "fuzziness":"2"
       }
      }
    },
    "must": 
    {
      "range" : {
        "marriages.marriage_year": {
          "gt" : "1620",
          "lte" : "1740"
           }
          }
         }
      }
      }
     }

It is returning data with marriages.marriage_year= "1630" with Mary as first_name as highest score.I also want to include marriages.marriage_year between 1620 - 1740 which are not shown in the results. It is showing data only for marraige_year 1630


Solution

  • Fuzzy query is meant to work on string fields to accommodate for typing errors. It gives you result on basis of edit distance.
    It doesn't make sense to use it on numeric fields. As 1000, 9000 has distance 1 only but they are far apart. You can do a range query as suggested by Russ or if you are bothered about edit distance and not range, index it as string field and then do fuzzy query.