Elasticsearch Practical Articles | Dark Horse Travel

1. Hotel search and pagination

Requirements: Realize the hotel search function of Heima Tourism, complete keyword search and pagination

1. Define the entity class and receive the front-end parameters

If the search condition is empty, all the data will be returned, and if it is not empty, the key will be searched

2. Conditional filtering

Modify the entity class, add a few parameters, the parameters are the conditions

The city city, brand brand, and star name are all exact matches, while the price price is a range match. The filter conditions should be placed in the filter, so that they will not participate in the score calculation and improve performance

Note: Use AND relationship between multiple conditions, use BooleanQuery for combined conditions, and make non-null judgments for parameters

3. Nearby hotels

The front end takes my location to request our interface, we use this location to find it in es, sorted by kilometers

When we take the object, we need to take an extra sort field, indicating how many kilometers away the target is from us, and return it to the front end 

4. Advertisement formulation

Requirement: The default is to sort by relevance, but we want to advertise some hotels and put them in the front. Let the specified hotel be specified in the search result ranking.

  1. Add the isAD field to the hotelDoc class, boolean type, indicating whether to advertise
  2. Pick a few favorite hotels, add the isAD field to the document data, set it to true
  3. Modify the search method, add the function score function, and increase the weight of the isAD value to true

First added 4 ads

If the filter condition is met, the calculation function will be executed, the weight will be increased, and the weight will be directly multiplied by 10

 

Guess you like

Origin blog.csdn.net/weixin_54232666/article/details/130306627