Solution 1: If you can make each character in your database as a separate word, you can use phrase queries to search for a substring.
For example, suppose that my_table contains a single human column:
person ------ John Doe Jane Doe
you can change it to
person ------ J ohn D oe J ane D oe
To search for the substring "ohn" use the phrase query:
SELECT * FROM my_table WHERE person MATCH '"ohn"'
Beware that “JohnD” matches “John Doe,” which may be undesirable. To fix this, change the space character in the source line to something else.
For example, you can replace the space character "$":
person ------ J ohn $ D oe J ane $ D oe
Solution 2: Following the idea of solution 1, you can make each character a separate word using a custom tokenizer and use phrase queries to query substrings.
The advantage over solution 1 is that you do not need to add spaces to your data, which can unnecessarily increase the size of the database.
The downside is that you have to implement a custom tokenizer. Fortunately, I have one of them for you . The code is in C, so you need to figure out how to integrate it with your Java code.
source share