当前位置:网站首页>Es common query, sorting and aggregation statements

Es common query, sorting and aggregation statements

2022-04-23 15:58:00 aserendipper

One 、 Accurately query a field

1、 The two query criteria are and The relationship between

Inquire about text Type , You need to add... After the field .keyword, If the field is keyword type , There is no need to add

{
  "size": 20,                            // Number of query results 
  "query": {
    "bool": {
      "must": [
        {
          "term": { 
            "description.keyword": {     // Field name         
              "value": " Investment advisory "         // field value 
            }
          }
        },
        {
          "term": {
            "tags_v2.id": {             
              "value": "tag-ec0dcc7af3" 
            }
          }
        }
      ]
    }
  }
}

Inquire about nested Type field , You need to add sub fields or use the following query statement

{
  "query": {
    "bool": {
      "must": [
        {
          "nested": {
            "path": "tags_v2",           // Field name 
            "query": {
              "term": {
                "tags_v2.id": {
                  "value": "tag-ec0dcc7af3"
                }
              }
            }
          }
        }
      ]
    }
  }
}

2、 The two query criteria are or The relationship between

{
  "query": {
    "bool": {
      "should": [
        {
          "term": {
            "app_name": {
              "value": " Zhilushui "
            }
          }
        },
        {
          "term": {
            "app_type": {
              "value": " application "
            }
          }
        }
      ]
    }
  }
}

3、 Multiple fields, multiple logical relationships
The relationship between the following three fields is :bid_type and (address3 or address4)

{
  "query": {
    "bool": {
      "must": [
        {
          "term": {
            "bid_type": {
              "value": " Bid winning announcement "
            }
          }
        },
        {
          "bool": {
            "should": [
              {
                "term": {
                  "address3": {
                    "value": "370100"
                  }
                }
              },
              {
                "term": {
                  "address4": {
                    "value": "370215"
                  }
                }
              }
            ]
          }
        }
      ]
    }
  }
}

Two 、 Query data that is not equal to this value

{
  "query": {
    "bool": {
      "must_not": [
        {
          "term": {
            "address1": {
              "value": "150"
            }
          }
        }
      ]
    }
  }
}

3、 ... and 、 Fuzzy query a field

1、 The query starts with a word (description The field starts with investment advisory )

{
  "query": {
    "bool": {
      "must": [
        {
          "prefix": {
            "description.keyword": {
              "value": " Investment advisory ",
              "boost": 10
            }
          }
        }
      ]
    }
  }
}

2、 The query contains a word (description The field contains investment advice )

{
  "query": {
    "bool": {
      "should": [
        {
          "match_phrase_prefix": {
            "description": {
              "query": " Investment advisory "
            }
          }
        }
      ]
    }
  }
}

Four 、 Query whether this field exists

1、 Query the data that exists in this field

{
    "query": {
        "exists": {
            "field": "coop_industry1"
        }
    }
}

2、 Query data that does not exist in this field

{
    "query": {
        "bool": {
            "must_not": [
                {
                    "exists": {
                        "field": "logo"
                    }
                }
            ]
        }
    }
}

5、 ... and 、 Composite query

The following statement query logic is cid be equal to 1631880 also attention_num stay [50,60] And data_status by 0 perhaps 1 The data of

{
    "query":{
        "bool":{
            "must":{
                "term":{
                    "cid":"1631880"
                }
            },
            "must_not":{
                "range":{
                    "attention_num":{
                        "gte":60,
                        "lte":50
                    }
                }
            },
            "should":[
                {
                    "term":{
                        "data_status": 0
                    }
                },
                {
                    "term":{
                        "data_status": 1
                    }
                }
            ]
        }
    }
}

6、 ... and 、 Sort

{
  "query": {
    "bool": {
      "should": [
        {
          "match_phrase_prefix": {
            "description": {
              "query": " Investment advisory "
            }
          }
        }
      ]
    }
  },
  "sort": [
    {
      "address2": {
        "order": "desc"        // Descending 
      },
      "address3": {
        "order": "asc"         // Ascending 
      }
    }
  ]
}

7、 ... and 、 Aggregate query

1、 Yes keywork,long And other fields

{
  "size": 1,                             
  "aggs": {
    "capital_ratio": {               // Write casually         
      "terms": {
        "field": "capital_ratio",    // Field name   
        "size": 10,                  // The aggregation result shows the number of 
        "order": {
          "_term": "asc"             // In ascending order 
        }
      }
    }
  }
}

2、 Yes nested Fields are aggregated

{
    "aggs": {
        "coop_industry1_aggs": {                // Write casually 
            "nested": {
                "path": "coop_industry1"        // Field name 
            },
            "aggs": {
                "coop_industry1": {             // Write casually 
                    "terms": {
                        "field": "coop_industry1.name",  // Field name 
                        "size": 10             // The number of results displayed after aggregation 
                    }
                }
            }
        }
    }
}

3、 Aggregate query on time type

{ 
    "query": {
        "bool": {
            "must": [
                {
                    "range": {
                        "found_date": {
                            "gte": 1533556800000,
                            "lte": 1533806520000
                        }
                    }
                }
            ]
        }
    },
    "size": 0,
    "aggs": {
        //  Take your own aggregation name , Write casually 
        "found_date": {
           // es Provided time processing function 
            "date_histogram": {
                //  The name of the field that needs to be aggregated ,  The field type needs to be date
                "field": "found_date",
                //  Aggregate by what time period ,  Here is 30 minute ,  Usable interval Given above 
                "interval": "30m",
                //  Set time zone ,  This is equivalent to the time of the East eighth district 
                "time_zone": "+08:00",
                //  Return value format ,HH Capitalization , Otherwise, you can't distinguish the morning 、 Afternoon 
                "format": "yyyy-MM-dd HH",
                //  The default is 0, The quantity in the bucket is greater than min_doc_count
                "min_doc_count": 0
            }
        }
    }
}

版权声明
本文为[aserendipper]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231557110761.html