Relevance in practice

Updated at:

Tokenization, matching, relevance, and sort expressions

This topic explains in detail why search results sometimes do not meet the expectations of users. It also uses this issue as a starting point to describe the search relevance features of OpenSearch and the directions of future work.

In general, search is implemented in one of two common ways:

  1. LIKE queries in databases. These queries can be understood as simple containment checks.

  2. Search engines such as Baidu and Google. These search engines use tokenization to split a query into multiple terms based on semantics, which is one of the most difficult parts of a search engine. The search engine matches documents against combinations of the terms, scores the matched documents, sorts them by score, and returns the results to the user.

OpenSearch works in basically the same way as these search engines. Therefore, three factors affect search results: 1. Tokenization method; 2. Matching method; 3. Relevance scoring.

The following sections describe how each of these three factors behaves in OpenSearch.

The following sections also describe the effects and applicable scenarios of each field for your reference.

Tokenization method

You must be familiar with the various tokenization methods before you perform the operations in this topic. Make sure that you read the Built-in analyzers topic first.

Matching method

How it works

Tokenization produces multiple terms. The matching method determines how documents are retrieved based on these terms. By default, OpenSearch uses AND internally, which means that a document can be retrieved only if it contains all the terms. This applies to a single keyword. In addition, the system supports multiple matching methods, such as AND, OR, RANK, NOTAND, and (). The precedence of these operators in descending order is (), ANDNOT, AND, OR, and RANK.

Examples

Operator

Usage

Description

query=title:"apple phone"

Queries documents whose title contains both apple and phone.

AND

query=title:'apple' AND cate:'phone'

Intersection. Queries documents whose title contains apple and whose cate contains phone.

OR

query=title:'apple' OR cate:'phone'

Union. Queries documents whose title contains apple or whose cate contains phone.

RANK

query=title:'apple' RANK cate:'phone'

Queries documents whose title contains apple. Documents whose cate contains phone receive a higher score.

ANDNOT

query=title:'apple' ANDNOT cate:'phone'

Queries documents whose title contains apple but whose cate does not contain phone.

Cases

  1. Q: My document contains "dinner ready". Searches for "dinner" and "dinner ready" both retrieve the document, but a search for "dinner ready now" returns no results. Why?

    A: OpenSearch requires that all tokenization results match before a document can be retrieved. The term "now" in the preceding query does not appear in the document, so the document cannot be retrieved. You can resolve this issue by using query analysis.

  2. Q: I want to find only documents in which specific terms appear at the beginning, such as documents that start with "KFC". A: Position-based retrieval is not supported.

Relevance scoring

The preceding sections describe retrieval-related technologies. After documents are retrieved, relevance determines how they are sorted. OpenSearch provides the sort clause to support custom sorting. If you do not specify sort, the default value sort=-RANK is used. The sort clause supports multi-dimensional sorting in ascending or descending order. For example, sort=-RANK;+bonus sorts documents first by relevance in descending order, and then sorts documents that have the same relevance score by bonus in ascending order. This section focuses on the usage of RANK. RANK represents the relevance settings in OpenSearch, which consist of two parts: basic sort (rough sort) and business sort (fine sort).

How it works

OpenSearch scores relevance by using the following strategy: OpenSearch takes rank_size (currently 1 million) retrieved documents and scores them based on the rough sort expression. Then, OpenSearch takes the top N results (in the hundreds) with the highest rough sort scores, scores them based on the fine sort expression, and sorts them. Finally, OpenSearch returns the corresponding results to the user based on the start and hit settings. If the number of results requested by the user exceeds the number N of fine sort results, the remaining results are displayed in the order of their rough sort scores.

  1. Basic sort (rough sort) expression: As described in the preceding section, rough sort has a significant impact on performance (latency). However, rough sort is also very important. Otherwise, high-quality documents may fail to enter fine sort and therefore cannot be displayed in the final results. Therefore, rough sort expressions must be as simple and effective as possible. Rough sort in OpenSearch supports only a few factors, such as several simple forward index fields, static BM25, and timeliness scores.

  2. Business sort (fine sort) expression: The rough sort expression selects the N higher-quality documents for detailed sorting. Fine sort expressions support complex mathematical calculations and logic. OpenSearch also provides a wide range of functions and features for typical scenarios, such as O2O, to meet common relevance requirements.

In addition, the system provides built-in application schemas and sort expressions for multiple scenarios for your reference and use.

Examples

Scenario

Expression

Description

Forum - rough sort

static_bm25()

Simplified text score

Forum - fine sort

text_relevance(title)*3+text_relevance(body)

+

if(text_relevance(title)>0.07,timeliness(create_timestamp),timeliness(create_timestamp)*0.5)

+

(topped+special+atan(hits)*0.5+atan(replies))*0.1

Text score

,

timeliness score

,

and scores of other attributes

O2O - rough sort

sold_score+general_score*2

Sales volume and overall store score (calculated offline)

O2O - fine sort

2*sold_score+0.5*reward

-

10*distance(lon,lat,u_posx,u_posy)

+

if ((flags&2) =2, 2, 0)+if(is_open=5,10,0)

+

special_score

Sales volume, delivery speed, and on-time rate

,

distance

,

whether the store is busy, whether the store is within business hours

,

and manual intervention

Novel - rough sort

static_bm25()*0.7+hh_hot*0.00003

Text score and popularity

Novel - fine sort

pow(min(0.5,max(text_relevance(category),max(text_relevance(title), text_relevance(author)))),2)

+

general_score*2

+

1.5*(1/(1+pow(2.718281,-((log10(hh_hot)-2)*2-5)))))

Category relevance, title relevance, author relevance

,

novel quality

,

and novel popularity

E-commerce - rough sort

static_bm25()+general_score*2+timeliness(end_time)

Text score, overall item score, and expiration time

E-commerce - fine sort

text_relevance(title)*3+text_relevance(category)

+

general_score*2+boughtScore*2

+

tag_match(ctr_query_value,doc_value,mul,sum,false,true)+..

Text relevance, category relevance

,

item popularity, seller score

,

CTR prediction, feature rule score, and more

Cases

  1. Q: The fine sort expression text_relevance(seller_id) reports that the field cannot be found. A: text_relevance() supports only fields of the TEXT and SHORT_TEXT types. Other types are not supported.

  2. Q: A query returns error 2112. What is the cause? A: The query statement (query clause) must match the formula. For example, if you specify query=default:'keyword', where default contains the title and body fields, and the formula specifies text_relevance(title)+text_relevance(author), an error is reported because author does not exist in default.

Tips

  1. Sort expression scores are calculated for each document during the query phase. Therefore, you can precalculate the query-independent parts offline and store the results in a new field such as general_score. During sorting, you only need to use the general_score field. This avoids extensive calculations and improves query performance.

  2. The tag_match feature allows you to perform multi-dimensional operations between features in the query and features in documents. This feature is widely used in e-commerce scenarios. If you have similar requirements, we recommend that you explore this feature.

  3. OpenSearch provides a wide range of functions and features. If used properly, they can deliver very powerful capabilities.

  4. Relevance consists of many components. You need to continuously adjust the weights of these components based on the search sorting results to achieve search results that satisfy your users.