admin管理员组

文章数量:1287101

I'm using the fuzzy search option in ElasticSearch. It's pretty cool.

But I came across an issue when doing search for values that have spaces. For example say I have two values:

"Pizza"
"Pineapple Pizza"

and I search for Pizza using this query:

        client.search({
            index: 'food_index',
            body: {
                query: {
                    fuzzy: {
                        name: {
                            value: "Pizza",
                            transpositions: true,
                        }
                    },
                }
            }
        })

The values returned are:

"Pizza"
"Pineapple Pizza"

Which is expected. But if I enter in the value "Pineapple Pizza" in my query:

        client.search({
            index: 'food_index',
            body: {
                query: {
                    fuzzy: {
                        name: {
                            value: "Pineapple Pizza",
                            transpositions: true,
                        }
                    },
                }
            }
        })

The values returned are:

""

Empty

Why is that? It should be an exact match. I'm contemplating switching all names that have spaces in them to underscores. So "Pineapple Pizza" would be "Pineapple_Pizza" (This solution works for me). But I'm asking this question as to hopefully finding a better alternative. What am I doing wrong here?

I'm using the fuzzy search option in ElasticSearch. It's pretty cool.

But I came across an issue when doing search for values that have spaces. For example say I have two values:

"Pizza"
"Pineapple Pizza"

and I search for Pizza using this query:

        client.search({
            index: 'food_index',
            body: {
                query: {
                    fuzzy: {
                        name: {
                            value: "Pizza",
                            transpositions: true,
                        }
                    },
                }
            }
        })

The values returned are:

"Pizza"
"Pineapple Pizza"

Which is expected. But if I enter in the value "Pineapple Pizza" in my query:

        client.search({
            index: 'food_index',
            body: {
                query: {
                    fuzzy: {
                        name: {
                            value: "Pineapple Pizza",
                            transpositions: true,
                        }
                    },
                }
            }
        })

The values returned are:

""

Empty

Why is that? It should be an exact match. I'm contemplating switching all names that have spaces in them to underscores. So "Pineapple Pizza" would be "Pineapple_Pizza" (This solution works for me). But I'm asking this question as to hopefully finding a better alternative. What am I doing wrong here?

Share Improve this question edited Oct 26, 2019 at 21:33 JD333 asked Oct 26, 2019 at 21:15 JD333JD333 5331 gold badge9 silver badges22 bronze badges 1
  • what is the analyzer used on name field? – sidgate Commented Oct 27, 2019 at 7:06
Add a ment  | 

1 Answer 1

Reset to default 11

Fuzzy queries are term level queries. It means searched text is not analyzed before matching the documents. In your case standard analyzer is used on field name, which splits "Pineapple Pizza" in two tokens Pineapple and pizza. Fuzzy query is trying to match search text "Pineapple pizza" to any similar term in index and there is no entry in index for the whole word pineapple pizza(it is broken in two words.)

You need to use match query with fuzziness set to analyze query string

{
  "query": {
        "match" : {
            "item" : {
                "query" : "Pineappl piz",
                "fuzziness": "auto"
            }
        }
    }
}

Response :

 [
      {
        "_index" : "index27",
        "_type" : "_doc",
        "_id" : "p9qQDG4BLLIhDvFGnTMX",
        "_score" : 0.53372335,
        "_source" : {
          "item" : "Pineapple Pizza"
        }
      }
    ]

You can also use fuzziness on keyword field which stores entire text in index

{
  "query": {
    "fuzzy": {
      "item.keyword": {
        "value":"Pineapple pizz"
      }
    }
  }
}

EDIT1:

{
  "query": {
        "match" : {
            "item" : {
                "query" : "Pineapple pizza",
                "operator": "and",
                "fuzziness": "auto"
            }
        }
    }
}

"operator": "and" --> all the tokens in query must be present in document. Default is OR , if any one token is present document is present. There are other possible binations where you can define how many tokens should match in percent term

本文标签: javascriptFuzzy search in ElasticSearch doesn39t work with spacesStack Overflow