Some commonly used queries in Elasticsearch

Tram Ho

Hello everyone, the day before I introduced you to a package Scout Elasticsearch Driver In Laravel, so today I would like to introduce to you some common queries when I perform the search feature in projects.

Some queries

Before starting, remember to install elasticsearch, import data and index it ???

Suppose I have 1 VD illustrating later. In Model Customer, I have declared mapping as follows:

1. Match query

Match query is a basic and prominent query of elasticsearch, it returns the result if any documment containing query passed. Example: I have the following query:

=> The results found will contain all documment containing Ngoc or Nguyễn will be returned.

2. Match-phrase query

Like match query, match_phrase query is exact search. It only returns documments that contain exactly the same query as the query. For example:

=> The results will only return those documment with text Nguyen Thi Bich Ngoc . Correct in the order and text transmitted.

3. Multi-match query

In essence, a multi-match query is like search by match query and match-phrase query, but it searches on many different fields. For example:

Inside:

  • query: is the query you transmitted
  • fields: are the fields you want to search in. Here I want to search for documment with Bich Ngoc text in field name and depscription Customer
  • type: is the type you want to search. By default, when you do not pass this field, you will understand that you are searching in a match style. There are many types of types we can use, mn see more here .

In the example above, I used exact search (match-phrase query). It will look for two fields name and depscription to see if any documment with Bich Ngoc text will return results. Well here search in the OR form, 1 field in the query text field will return results.

4. Term query

This type of query is often used for searching fields with numeric data types, dates. If we want to search for text we should use match query.

WARNING:

Avoid using the term query for text fields.

By default, Elasticsearch changes the values ​​of text fields as part of analysis. This can make finding exact matches for text field values ​​difficult.

To search text field values, use the match query instead.

Term query is the exact type of search. The following example will query to retrieve the customer with id = 1.

5. Terms query

Use to search with multiple queries at once. The retrieved result is still in the OR style:

=> The result will return customer with id = 1 or id = 2.

6. Range query

A type of query that returns documments within a query condition range. Range query is often used to search for time range, age range, etc.

Some parameters:

  • gt: Larger option
  • gte: Optionally larger or equal
  • lt: Smaller option
  • lte: Option less than or equal As in the above example, the results retrieve the records whose birthdays are from January 1, 1997 to December 12, 1997.

7. Bool query

A type of query that allows to combine many different queries at once. For example:

I use must and should:

  • must: same as AND condition. That is, the results retrieved must satisfy all queries passed up.
  • should: search under OR conditions. You only need to satisfy one of the query conditions to be passed.

Conclude

Above is a variable type query that I use often in elasticsearch. Also mn can see more queries here dentist

Share the news now

Source : Viblo