ElasticSearch advanced search

  Continuing from the previous chapter on basic retrieval, in the blog of the previous chapter, I inserted a lot of data into ES, and then I will perform some advanced operations based on the previous data.
  The advanced query of ES has its own language-QueryDSL
  ES provides a JSON-style DSL that can perform queries, called QueryDSL. This language is very comprehensive, but a bit more complicated. Let’s experience it first.
Insert picture description here
  You can see that QueryDSL queries send requests through get and carry a json data. , This json data contains our query conditions, and its syntax format is as follows

{
    
    
	查询名1:{
    
    
		条件1:值1
	}
	查询名2:{
    
    
		条件2:值2
	}.....
}

match word segmentation matching query

  The match query will perform word segmentation matching on the retrieval conditions, as long as the data matches one of the characters, it will be queried, and finally returned according to the score

# 语法格式
# 查询每个字段值为多少的数据,支持模糊查询
GET bank/_search  # 请求体
{
    
    
  "query": {
    
    	# 查询
    "match": {
    
    	# 匹配查询
      "FIELD": "TEXT"	# 字段:值
    }
  }
}

Insert picture description here

match_phrase phrase match

  match_phrase will retrieve the value of the retrieval condition as a whole

GET bank/_search
{
    
    
  "query": {
    
    
    "match_phrase": {
    
    
      "FIELD": "PHRASE"
    }
  }
}

Insert picture description here

multi_match multi-field matching

  Query data that contains a certain value in multiple fields, and perform word segmentation matching on the query conditions

GET bank/_search
{
    
    
  "query": {
    
    
    "multi_match": {
    
    
      "query": "",	# 查询条件
      "fields": []	# 匹配的字段
    }
  }
}

Insert picture description here

bool compound query

  Compound query can combine multiple query conditions, which is equivalent to the and condition in mysql, and all conditions need to be met when querying

GET bank/_search
{
    
    
  "query": {
    
    
    "bool": {
    
    
      "must": [ # 必须满足这个条件
        {
    
    }
      ],
      "must_not": [ # 必须不是这个条件
        {
    
    }
      ],
      "should": [ # 可以包含这个条件
        {
    
    }
       ]
       .....
  }
}

Insert picture description here

term match query

  Term and match can do exact matching. The difference is that term cannot do fuzzy query. Term will query the value of the retrieval condition as a whole. However, ES has data analysis problems when storing text, which is equivalent to word segmentation storage. Therefore, the term cannot find data when querying multiple texts. It is recommended to use term when querying non-text

GET bank/_search
{
    
    
  "query": {
    
    
    "term": {
    
    
      "FIELD": {
    
    
        "value": "VALUE"
      }
    }
  }
}

Insert picture description here

aggregations

  Aggregation provides the ability to group and extract data from data. The simplest aggregation method is roughly equivalent to sql group by and sql aggregation functions.

GET bank/_search
{
    
    
  "aggs": {
    
    
    "NAME": {
    
    
      "AGG_TYPE": {
    
    }
    }
  }
}

  Example 1: Search for the age distribution and average age of all people whose address contains mill.
Insert picture description here
  Example 2: Aggregate by age and request the average salary of these age groups.
Insert picture description here
  Example 3: Query all age distributions and the average of M and F in the age group. Salary and overall average salary
Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_45481406/article/details/112548550