Lucene query: bla~* (match words that start with something fuzzy), how?

前端 未结 4 1410
旧时难觅i
旧时难觅i 2020-12-05 15:59

In the Lucene query syntax I\'d like to combine * and ~ in a valid query similar to: bla~* //invalid query

Meaning: Please match words that begin with \"bla\" or so

4条回答
  •  醉酒成梦
    2020-12-05 16:53

    I do not believe Lucene supports anything like this, nor do I believe it has a trivial solution.

    "Fuzzy" searches do not operate on a fixed number of characters. bla~ may for example match blah and so it must consider the entire term.

    What you could do is implement a query expansion algorithm that took the query bla~* and converted it into a series of OR queries

    bla* OR blb* OR blc OR .... etc.
    

    But that is really only viable if the string is very short or if you can narrow the expansion based on some rules.

    Alternatively if the length of the prefix is fixed you could add a field with the substrings and perform the fuzzy search on that. That would give you what you want, but will only work if your use case is sufficiently narrow.

    You don't specify exactly why you need this, perhaps doing so will elicit other solutions.

    One scenario I can think of is dealing with different form of words. E.g. finding car and cars.

    This is easy in English as there are word stemmers available. In other languages it can be quite difficult to implement word stemmers, if not impossible.

    In this scenario you can however (assuming you have access to a good dictionary) look up the search term and expand the search programmatically to search for all forms of the word.

    E.g. a search for cars is translated into car OR cars. This has been applied successfully for my language in at least one search engine, but is obviously non-trivial to implement.

提交回复
热议问题