Start using ElasticSearch (8) fuzzy search, multi-keyword highlighting

   根据之前不懈努力的结果,我们终于把基本功能完成了,以为可以交差了。但是其实才刚刚开始!

Let's see what's missing?
1. Full keyword matching (equal to ==)
2. Partial matching (equal to like, here is also divided into pre-matching, post-matching, with spaces and multiple keywords, etc.)
3. The keyword highlighting with spaces and multiple keywords

will be done for now, one step Step by step, too much step is easy to pull the egg.

Start using ElasticSearch (8) fuzzy search, multi-keyword highlighting

This is the template we defined before. It basically satisfies the simple matching conditions. Now let's optimize it slowly.

Start using ElasticSearch (8) fuzzy search, multi-keyword highlighting

First, we define a prefix query, which is the most basic general query.
For example, if the user enters the first few digits of the number, it can be quickly matched. The disadvantage is that the prefix must not be broken, otherwise it will not be searched
. For example, the searched data is: 123 678 I input 123 and it can be searched, but 678 is not the prefix. Can't get out.

Start using ElasticSearch (8) fuzzy search, multi-keyword highlighting

Here I used matchphraseprefix. This search can be used for phrase matching. For example, if I only enter "I am Chinese",
it will search based on word segmentation. As long as there is a match, the result will be returned. Here also allows the last phrase and any word segmentation prefix in the text
Match, so when I type XPZ, XPZ matches the second phrase of Opt xpz... But the problem is that there is a
special situation on our side . The user may know the letters of the prefix at the beginning and the letters in the middle. In this case, segment matching is required.
Obviously the current two searches are not supported

Start using ElasticSearch (8) fuzzy search, multi-keyword highlighting
The above match query will be segmented according to "XPZ" "TX". But there is a case, the user only remember the middle
-segment digital how to do it, I feel conditions are not satisfied now!

Start using ElasticSearch (8) fuzzy search, multi-keyword highlighting

So I added a wildcard query to the search criteria, which means any wildcard in Chinese. In fact, it is equivalent to the like syntax in sql. If you
want to match a wildcard before and after, you can use it? . Here we don't know how many wildcards there are before and after, so we use it to represent the query
"There are N unknown words in the front" 1687 "There are N unknown words in the back" . But do we still remember the good full-text search function?
The preceding are all designated fuzzy matches, but a certain field does not seem to be a full-text search.
Start using ElasticSearch (8) fuzzy search, multi-keyword highlighting
So I added the query_string query provided by es below , which is similar to the match function. Wildcards
, ~ etc. can also be used here . You can also use regular expressions. I set allow_leading_wildcard=false to disable the leading wildcard. Basically a
simple general query is basically completed. Then we add these to the template to try out the effect.
Start using ElasticSearch (8) fuzzy search, multi-keyword highlighting
Start using ElasticSearch (8) fuzzy search, multi-keyword highlighting
Here I use typeahead to receive the data source as a reminder highlight, let's run it.

Start using ElasticSearch (8) fuzzy search, multi-keyword highlighting
Start using ElasticSearch (8) fuzzy search, multi-keyword highlighting

Did you find any problems? When I search for a keyword, it can be highlighted, but when I separate two keywords with a space, it cannot be highlighted.
This is definitely not a problem with ES, ES is only responsible for providing queries. Then the problem is located on typeahead. I searched the document and found that
typeahead itself only supports single keyword highlighting by default. If you want to highlight multiple keywords, you must quote mark.js.

Start using ElasticSearch (8) fuzzy search, multi-keyword highlighting

After the reference is successful, you also need to modify the original binding typeahead writing.

Start using ElasticSearch (8) fuzzy search, multi-keyword highlighting
Because of the mark used, you need to set the original highlighting to false. Other writing methods are relatively simple. Run it after modification.

Start using ElasticSearch (8) fuzzy search, multi-keyword highlighting

It's finally a success! Later I will do pinyin query, simplified and traditional processing. Stop here for a while.

Guess you like

Origin blog.51cto.com/15034497/2590944