(6) ElasticSearch search actual combat

1. search

1) Exact match (Term Query)

Exact match finds documents that exactly match the specified value in the specified field. Here's an example of an exact match, finding documents where the field title has the value "Elasticsearch is powerful":

POST /my_index/_search
{
    
    
  "query": {
    
    
    "term": {
    
    
      "title": "Elasticsearch is powerful"
    }
  }
}

2) Full text search (Match Query)

A match query is a full-text search query that searches for documents that match a query string in a specified field. It tokenizes the query string to match the words in the field and returns matching documents.
Note the use of match, when the data type is required text

(1) Single:

To perform a single match query, you can use the following sample code:

POST /my_index/_search
{
    
    
  "query": {
    
    
    "match": {
    
    
      "content": "Elasticsearch"
    }
  },
  "size": 10,         // 指定每页返回的文档数量
  "from": 0           // 指定从哪个文档开始返回,即偏移量
}

(2) All:

To perform a full match_all query, you can use the following sample code:

POST /my_index/_search
{
    
    
  "query": {
    
    
    "match_all": {
    
    
    }
  },
  "size": 10,         // 指定每页返回的文档数量
  "from": 0           // 指定从哪个文档开始返回,即偏移量
}

(3) Multiple fields

To match multiple fields in Elasticsearch, you can use multi_match query. The multi_match query allows you to match multiple fields in one query and return matching results.

Here is an example of using a multi_match query for multiple field matching:

POST /my_index/_search
{
    
    
  "query": {
    
    
    "multi_match": {
    
    
      "query": "Elasticsearch",
      "fields": ["title", "content"],
      "operator": "and"
    }
  }
}

It should be noted that by default, Elasticsearch uses the OR operator to combine the matching results of multiple fields. That is, as long as any field matches successfully, the document will be returned.
If you want to change the default behavior, you can specify to use the AND operator by setting the operator parameter. This way, only documents that match in more than one field at the same time will be returned.

(4) Phrase matching

In Elasticsearch, the match_phrase query is used for phrase matching, taking into account the order of words, and returning documents containing the complete phrase. Here is an example query using match_phrase:

POST /my_index/_search
{
    
    
  "query": {
    
    
    "match_phrase": {
    
    
      "content": "quick brown fox"
    }
  }
}

(4) Prefix phrase matching

In Elasticsearch, a match_phrase_prefix query is a query type that combines phrase matching and prefix matching. It is used to match documents that contain a specific phrase prefix.

Here is an example query using match_phrase_prefix:

POST /my_index/_search
{
    
    
  "query": {
    
    
    "match_phrase_prefix": {
    
    
      "content": {
    
    
        "query": "quick brown",
        "slop": 2
      }
    }
  }
}

The match_phrase_prefix query takes into account the order of phrase prefixes and returns documents containing matching phrase prefixes. By setting the "slop" parameter, the maximum number of gaps allowed between terms can be specified. By default, slop has a value of 0, which requires terms to appear next to each other in strict order. More gaps can be tolerated by increasing the value of slop.

Guess you like

Origin blog.csdn.net/csdn570566705/article/details/131208249