Fuzzy search

Hello, I am trying to do a fuzzy search for my data. But my query returns null

Create index

PUT /test_index
{
  "mappings": {
      "properties": {
        "testt": {
          "type": "text"
        }    
      }
    }
}

Mapping my index

GET /test_index/_mapping?pretty
{
  "test_index" : {
    "mappings" : {
      "properties" : {
        "testt" : {
          "type" : "text"
        }
      }
    }
  }
}

Query. I expect that the request will return a document, where the testt with the value Moscow

GET /test_index/_search?pretty
{
  "query": {
     "match": {
      "testt": {
        "query":     "Mascow",
        "fuzziness": "auto",
		"operator":  "and"
      }             
   	}
  }
}

Welcome to our community! :smiley:

Can you share a sample doc?

This my search GET /test_index/_search?pretty
pastebin result

Ok but an example of the document in Elasticsearch would be useful.

my example of the document

Working for me:

DELETE test
PUT /test
{
  "mappings": {
      "properties": {
        "testt": {
          "type": "text"
        }    
      }
    }
}


POST test/_doc/1
{
  "testt":"Moscow"
}
POST test/_search
{
  "query": {
    "match": {
      "testt": {
        "query": "Mascow",
        "fuzziness": "auto",
        "operator": "and"
      }
    }
  }
}

It really works, but do you know how to implement support for the Russian language?)

i just do it

PUT /test
{
  "mappings": {
      "properties": {
        "testt": {
          "type": "text",
           "analyzer": "russian"
        }    
      }
    }
}

and also tried like this:

{
  "settings": {
    "analysis": {
      "analyzer": "russian"
    }
  },
  "mappings": {
	  "properties": {
	    "testt": { "type": "string" }
	  }
  }
}

What was wrong with the first example in your last comment?

I'm trying to make sure that when the city is incompletely written, I get an approximately suitable list.
Example: i request with value "San" and take response "San Diego", "San Jose", "San Francisco", "Santa Rosa", "Santa Maria"
Now: i request with value "Masc" and take response nothing, but if request "Mascow" my response "Moscow"

Fuzzy search will only help you match where there's max 2 characters different between search term and indexed term.

To match "San" with "San Diego" in the city field it sounds like you want to use a prefix query and to use a keyword field with a lower case normalizer.

Just week ago i created index and my full address have city, street, number house (example: Moscow, str. Avtozavodskay, h. 26). My request with "avtozav" and he returned full address (look example)

(look example)

If we are to diagnose any problems we need JSON examples for a mapping, document and search otherwise we can't reproduce what is being discussed and be certain of what problem might exist.

mapping:

PUT /addresses
{
  "settings": {
    "analysis": {
      "analyzer": "russian"
    }
  },
  "mappings": {
	  "properties": {
	    "address": { "type": "text" }
	  }
  }
}

documents:

POST addresses/_doc/1
{
  "address": "Moscow, str. Avtozavodskay, h. 26"
}

POST addresses/_doc/2
{
  "address": "Moscow, str. Avtozavodskay, h. 32"
}

POST addresses/_doc/3
{
  "address": "Moscow, str. Lenina, h. 1"
}

search:

POST /address/_search
{
  "query": {
    "match": {
      "address": {
        "query": "str avtozavod",
        "fuzziness": "auto",
        "operator": "and"
      }
    }
  }
}

I expect to see

{
    "took": ...,
    "timed_out": ...,
    "_shards": {...},
    "hits": {
        "total": {
            "value": 2,
            "relation": "eq"
        },
        "max_score": ...,
        "hits": [
            {
                "_index": "address",
                "_type": "_doc",
                "_id": "1",
                "_score": 0.99045343,
                "_source": {
                    "address": "Moscow, str. Avtozavodskay, h. 26"
                }
            },
            {
                "_index": "address",
                "_type": "_doc",
                "_id": "2",
                "_score": 0.9904123,
                "_source": {
                    "address": "Moscow, str. Avtozavodskay, h. 32"
                }
            }
        ]
    }
}

Your settings for the analyzer look messed up.
Also, as I stated earlier, a max of 2 character differences are permitted between search term and indexed term so a search for avtozavod will not match avtozavodskay because that is 4 characters different. In the example below I have a 2 character difference and it works:

PUT /addresses
{

  "mappings": {
	  "properties": {
	    "address": { "type": "text", "analyzer": "russian" }
	  }
  }
}
POST addresses/_doc/1
{
  "address": "Moscow, str. Avtozavodskay, h. 26"
}

POST addresses/_doc/2
{
  "address": "Moscow, str. Avtozavodskay, h. 32"
}

POST addresses/_doc/3
{
  "address": "Moscow, str. Lenina, h. 1"
}

POST /addresses/_search
{
  "query": {
    "match": {
      "address": {
        "query": "stz avtozavodskzz",
        "fuzziness": "auto",
        "operator": "and"
      }
    }
  }
}
1 Like