1 Getting Started -- 5 Exploring Data

The search API
GET /bank/_search?q=*&sort=account_number:asc&pretty
rest searches all data under the bank index, and returns according to the ascending account_number
Similar to the following search method
GET /bank/_search The
body is:
{
  "query": { "match_all": {} },
  "sort": [
    { "account_number": "asc" }
  ]
}
Preliminary learning of search language
Search all documents below, sort them in descending order of balance, and return documents from 11 to 20, from Position starts, size documents
GET /bank/_search
{
  "query": { "match_all": {} },
  "sort": { "balance": { "order": "desc" } }
  "from": 10,
  "size": 10
} To
perform a search
, you can use the following method _source when you do not need to return all fields, which defines only two fields to be returned
GET /bank/_search
{
  "query": { "match_all": {} },
  "_source": ["account_number", "balance"]
}

If we want to search based on a certain condition, we can use the match field:
search for documents with account_number 20 as follows
GET / bank/_search
{
  "query": { "match": { "account_number": 20 } }
}
The following searches for documents whose address contains "mill"
GET /bank/_search
{
  "query": { "match": { "address" : "mill" } }
} The
following searches for documents whose address contains "mill" or contains "lane"
GET /bank/_search
{
  "query": { "match": { "address": "mill lane" } }
}
The following searches for documents whose address contains the phrase "mill lane"
GET /bank/_search
{
  "query": { "match_phrase": { "address": "mill lane" } }
}

bool searches can combine small searches into one large search as
follows search for documents whose address contains mill and lane
GET /bank/_search
{
  "query": {
    "bool": {
      "must": [
        { "match": { "address ": "mill" } },
        { "match": { "address": "lane" } }
      ]
    }
  }
}
(must means must be satisfied)

Instead, search for documents whose address contains mill or lane
GET /bank/ _search
{
  "query": {
    "bool": {
      "should": [
        { "match": { "address": "mill" } },
        { "match": { "address": " lane" } }
      ]
    }
  }
}
(should means that any one of the conditions is satisfied)

Search for documents whose address does not contain either mill or lane
GET /bank/_search
{
  "query": {
    "bool": {
      "must_not": [
        { "match": { "address": "mill" } },
        { "match": { "address": "lane" } }
      ]
    }
  }
}
(must_not stands for exclusion)

The above three types can
be Search for documents with age 40 and state not ID
GET /bank/_search
{
  "query": {
    "bool": {
      "must": [
        { "match": { "age": "40" } }
      ],
      "must_not": [
        { "match": { "state": "ID" } }
      ]
    }
  }
}
perform filtering
filter performs filtering (in fact, it performs interval search filtering on non-strings)
if you search for documents with 20000<=blance && blance<=30000
GET /bank/_search
{
  "query": {
    "bool": {
      "must": { " match_all": {} },
      "filter": {
        "range": {
          "balance": {
            "gte": 20000,
            "lte": 30000
          }
        }
      }
    }
  }
}

Perform the aggregation
as follows to find each type according to the field state aggregation The number of (default is in descending order, returns the first 10 records similar to sql: SELECT state, COUNT(*) FROM bank GROUP BY state ORDER BY COUNT(*) DESC)
GET /bank/_search
{
  "size": 0,
  "aggs": {
    "group_by_state": {
      "terms": {
        "field": "state"
      }
    }
  }
}
(note: set size to 0 because we don't want to see the hit information)

as follows On the basis of the above, we want to know the average of the balance
GET / bank/_search
{
  "size": 0,
  "aggs": {
    "group_by_state": {
      "terms": {
        "field": "state"
      },
      "aggs": {
        "average_balance": {
          "avg": {
            " field": "balance"
          }
        }
      }
    }
  }
}
On the basis of the above, we want to sort by average in descending order
GET /bank/_search
{
  "size": 0,
  "aggs": {
    "group_by_state": {
      "terms": {
        "field": "state",
        "order": {
          "average_balance": "desc"
        }
      },
      "aggs": {
        "average_balance": {
          "avg ": {
            "field": "balance"
          }
        }
      }
    }
  }
}

As follows, we first use age, then gender, and then find the average of balance
GET /bank/_search
{
  "size": 0,
  "aggs": {
    "group_by_age": {
      "range": {
        "field": "age",
        "ranges": [
          {
            "from": 20,
            "to": 30
          },
          {
            "from": 30,
            "to": 40
          },
          {
            "from": 40,
            "to": 50
          }
        ]
      },
      "aggs": {
        "group_by_gender": {
          "terms": {
            "field": "gender.keyword"
          },
          "aggs": {
            "average_balance": {
              "avg": {
                "field":"balance"
              }
            }
          }
        }
      }
    }
  }
}

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326801613&siteId=291194637