[Elasticsearch] Partial match (iii)-Instant Search during query

Source: Internet
Author: User

This chapter is translated from the partial matching chapter of the official Elasticsearch guide.


Instant Search during query (Query-time search-as-you-type)

Now let's look at how prefix matching can help with full-text search. The user is accustomed to seeing the search results before completing the input-this is called an Instant Search (Instant search, or Search-as-you-type). This not only allows users to see search results in less time, but also leads them to the results that are real in our index.

For example, if the user enters Johnnie Walker BL, we will show the results of Johnnie Walker Black label and Johnnie Walker Blue label before the user input is complete.

As always, there are many ways to achieve our goal! First we start with the simplest way. You don't need to prepare your data in any way, enabling Instant Search on any full-text field (Full-text field).

In phrase matching (Phrase Matching), we introduced the Match_phrase query, which can match all the specified words according to the order of the words. For an instant search during a query, we can use a special case of the query, the Match_phrase_prefix query:

{"  Match_phrase_prefix : { brand"  :  " Johnnie Walker BL  }} 

The second and Match_phrase queries work essentially the same way, except that they take the last word in the query string as a prefix. In other words, the previous example looks for the following:

    • Johnnie
    • Followed by Walker.
    • Followed by a word that begins with BL

If we execute the query through the Validate-query API, it will have the following explanation:

"Johnnie Walker bl*"

Like the Match_phrase query, it accepts a slop parameter (see here) to make the order and relative position between words less restrictive:

{ "match_phrase_prefix" : { "brand" : { "query" : "Walker Johnnie BL" , "slop" :Ten}    }}

However, the last word in the query string is always treated as a prefix.

When we introduced the prefix query earlier, we talked about some of the prefix queries that need to be noted-prefix how resources are consumed when querying. The same problem is faced when using Match_phrase_prefix queries. A prefix a you can match very, very many entries. Matching so many entries will not only consume a lot of resources, but also be of little use to the user.

We can limit the effect of the prefix extension (Prefix Expansion) by setting the parameter max_expansions to a reasonable value, such as 50:

{ "match_phrase_prefix" : { "brand" : { "query" : "Johnnie Walker bl" , "max_expansions" : -}    }}

The Max_expansions parameter controls the number of entries that can match the prefix. It will find the first entry that begins with BL and then start collecting (in alphabetical order) until all entries beginning with BL are traversed or get more entries than max_expansions.

Do not forget that the query is executed once every time the user has typed in a character, so it needs to be faster. If the first result set does not meet the user's expectations, they will continue to input until they get the results they want.


[Elasticsearch] Partial match (iii)-Instant Search during query

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.