Elasticsearch high -level search skills and best practice
Elasticsearch high -level search skills and best practice
## profile
Elasticsearch is an open source distributed search and analysis engine, which supports real -time storage, search and analysis of large -scale data.It is widely used in all walks of life and is used to build high -performance search engines, log analysis systems, and e -commerce recommendation systems.
This article will introduce high -level search skills and best practices of Elasticsearch, including how to build complex query, use various query types, and optimize search performance.At the same time, we will provide relevant programming code and configuration examples to help readers better understand these search skills and best practices.
## Table of contents
1. Basic search and parameters
-Cathe inquiries
-Mult field query
-Stid query
-Caton inquiry
-Higher expression query
-Speed query
2. Compound query
-WOOL query
-CONSTANT SCORE query
-DIS MAX query
-Function score query
3. Gao Liang search results
4. Sorting and paging
5. Polymerization query
6. Invasion indexing principle and related optimization
7. Configure Elasticsearch
8. Performance adjustment skills
9. Analysis of the best practice case
Below we will introduce each theme through specific examples.
### 1. Basic search and their parameters
####
The matching query is the most basic query type in Elasticsearch. It will match the field string with the field in the index and return the result of matching.The following is an example of using matching query:
json
GET /my_index/_search
{
"query": {
"match": {
"title": "Elasticsearch"
}
}
}
In the above example, we check all documents containing "Elasticsearch" in the `My_index` index.
#### multi -field query
Sometimes we need to query in multiple fields and return the result of the matching field.Here are an example of using multi -field query:
json
GET /my_index/_search
{
"query": {
"multi_match": {
"query": "Elasticsearch",
"fields": ["title", "content"]
}
}
}
In the above examples, we query documents containing "Elasticsearch" in the `Title` and` Content` fields.
#### prefix query
The prefix query is used to match the text that specifies the beginning of the prefix.The following is an example of using prefix query:
json
GET /my_index/_search
{
"query": {
"prefix": {
"title": "ela"
}
}
}
In the above examples, we check the documents that start with "Ela" in the `Title` field.
#### query
The inquiries of the passing are allowed to match the text to match the text.The following is an example of using the inquiries using the passing symbol:
json
GET /my_index/_search
{
"query": {
"wildcard": {
"title": "el*sear*"
}
}
}
In the above example, we check the document with the matching mode "El*Sear*" in the `Title` field.
#### Regular expression query
Regular expression query allows us to use regular expressions to match text.The following is an example of inquiries using regular expressions:
json
GET /my_index/_search
{
"query": {
"regexp": {
"title": "el[a-z]*sear[a-z]*"
}
}
}
In the above example, we query the document with the regular expression mode "El [A-Z]*Sear [A-Z]*" in the `Title` field.
####
Scope query is used to match a value or date within a specified range.The following is an example of the scope of use:
json
GET /my_index/_search
{
"query": {
"range": {
"price": {
"gte": 100,
"lte": 200
}
}
}
}
In the above example, we check the documents between 100 to 200 in the `Price` field.
The above is only some examples of basic search. Elasticsearch also provides more powerful query types and parameters for us to use.Next we will introduce a compound query.
### 2. Composite query
Compound query is a combination of multiple queries, which can more flexibly meet our search needs.
#### Bool query
BOOL queries are the most commonly used composite query types in Elasticsearch, which combine multiple sub -queries through logical operators.Here are an example of using BOOL query:
json
GET /my_index/_search
{
"query": {
"bool": {
"must": [
{ "term": { "title": "Elasticsearch" } },
{ "range": { "price": { "gte": 100 } } }
],
"must_not": [
{ "term": { "category": "deprecated" } }
],
"should": [
{ "term": { "tags": "search" } },
{ "term": { "tags": "analysis" } }
],
"filter": {
"range": { "date": { "gte": "2022-01-01" } }
}
}
}
}
In the above example, we used BOOL to query to build a compound query.The `Must` condition represents the query that must be satisfied, the` Must_not` conditions indicate the query that cannot be met, the `specim` condition represents the query that should be met, and the` Filter` condition is used to narrow the range.In this way, combined with different conditions and logical operators, we can achieve more accurate inquiries.
#### constant score query
Constant Score query is a simple composite query type that specifies a fixed score for each qualified document without considering the actual matching degree.The following is an example of using the constant score query:
json
GET /my_index/_search
{
"query": {
"constant_score": {
"filter": {
"term": { "category": "search" }
},
"boost": 1.2
}
}
}
In the above examples, we use the Constant Score to query all documents containing "Search" and set a fixed score 1.2 for them.
#### Dis Max query
The DIS MAX query is a composite query type. It calculates the scores of multiple subquers and returns the highest score document.The following is an example of using DIS MAX query:
json
GET /my_index/_search
{
"query": {
"dis_max": {
"queries": [
{ "term": { "title": "Elasticsearch" } },
{ "term": { "content": "Elasticsearch" } }
],
"tie_breaker": 0.2
}
}
}
In the above example, we use DIS MAX to query to match the `title` and` Content` fields and return the highest score document.The parameter of `tie_breaker` is used to adjust the weight of the sub -query score.
#### Function Score
Function Score query is a composite query type. It adjusts the searches of the search results by applying the custom function to the score of each document.The following is an example of using the Function Score query:
json
GET /my_index/_search
{
"query": {
"function_score": {
"query": { "match_all": {} },
"functions": [
{ "filter": { "term": { "category": "search" } }, "weight": 2 },
{ "filter": { "term": { "category": "analysis" } }, "weight": 1 }
],
"boost_mode": "sum"
}
}
}
In the above example, we use Function Score to query all documents to set up different weights to different `category` fields through custom functions, and finally seek scores and as the final sorting basis.
### 3. High -liang search results
The highlighting of the matching keywords in the search results can help users better locate the results.Here are a search examples using high brightly:
json
GET /my_index/_search
{
"query": {
"match": {
"content": "Elasticsearch"
}
},
"highlight": {
"fields": {
"content": {}
}
}
}
In the above examples, we search for documents containing "Elasticsearch" in the `Content` field and using high -light display matching keywords.
### 4. Sorting and paging
The search results can be sorted according to the specified field by sorting, and the paging can display the search results according to the specified page number and size per page.The following is a search example of sorting and paging:
json
GET /my_index/_search
{
"query": {
"match_all": {}
},
"sort": [
{ "price": "asc" }
],
"from": 0,
"size": 10
}
In the above examples, we sort all documents and arranged according to the promotion order of the `Price` field.At the same time, set the `From` and` SIZE` parameters, indicating that starting from Article 0 records, 10 records are displayed on each page.
### 5. Juggling query
The aggregation query can statistics and analysis of the search results and return the corresponding aggregation results.The following is an example of using aggregate query:
json
GET /my_index/_search
{
"aggs": {
"avg_price": {
"avg": {
"field": "price"
}
}
}
}
In the above example, we calculate the average value of the `Price` field and return the aggregation results.
### 6. The principle of reverse indexing and related optimization
The inverted index is the core component of Elasticsearch. It sends and indexes each field of each document to facilitate fast search.We will introduce the principles of inverted indexes and explore how to improve the search performance through reasonable configuration and optimization.
### 7. Configure Elasticsearch
Configure Elasticsearch is an important step to ensure search performance and reliability.We will introduce the main configuration items and commonly used configuration strategies of Elasticsearch.
### 8. Performance adjustment skills
Through some performance tuning skills, we can further improve the search performance of Elasticsearch.We will share some practical skills and suggestions to help readers achieve a better search experience.
### 9. Best practice case analysis
Finally, we will analyze and summarize the best practice of Elasticsearch through some practical cases to provide readers' reference and reference.
The example code and configuration in this article are only used as a reference. Readers need to be adjusted and optimized according to their needs in practical applications.
It is hoped that this article can help readers better understand Elasticsearch's advanced search skills and best practices, and achieve good results in practical applications.I wish you all a greater success in the search field!