Skip to content

ElasticSearch

alex [dot] kramer [at] g_m_a_i_l [dot] com edited this page Feb 13, 2021 · 9 revisions

Query Examples

Query for indices:

GET /_cat/indices/*index_name_query*/

Query across fields:

GET some_index_name/
{
  "query": {
    "query_string": {
      "query": "my special query"

    }
  }
}

Query two fields:

GET some_index_name/_search
{
"query": {
    "bool": {
      "should": [
        { "match": { "field_1": "some query string" }},
        { "match": { "field_2": "some other query string" }}
      ]
    }
  }
}


GET some_index_name/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "field_1": {
              "query": "some query string",
              "operator": "and"
            }
          }
        },
        {
          "match": {
            "field_2": "some other query string"
          }
        }
      ]
    }
  }
}

Query by:

  • index name prefix
  • required field
  • required non-empty field
  • fuzzy search field
  • required-to-be-empty field
  • order by field ascending
  • and limit results to 100
GET index_name_prefix*/_search?size=100
{
    "sort": [
        {
            "order_by_field_name": {
                "order": "asc"
            }
        }
    ],
    "query": {
        "bool": {
            "must": [
                {"exists": {"field": "required_field_name"}},
                {"regexp": {"non_empty_field_name": ".+"}},
                { "match": { "search_field_name": "my special search query" }}
              ],
            "must_not": [
              {
                "regexp": {
                  "required_to_be_empty_field_name": ".+"
                }
              }
            ]
        }
    }
}

Histogram by date:

GET index_name/_search
{
  "aggs": {
    "records_monthly": {
      "date_histogram": {
        "field": "date_field_name",
        "interval": "month",
        "min_doc_count": 1
      }
    }
  },
  "size": 0
}

Date range AND field match:

GET index_name/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "range": {
            "date": {
              "gte": "2018-01-01",
              "lte": "2019-01-01"
            }
          }
        },
        {
          "match": {
            "field_name": "some query"
          }
        }
      ]
    }
  }
}

First 100 records:

GET some_index/_search?size=100
{
  "query": {
    "query_string": {
      "query": "some query"

    }
  }
}
Clone this wiki locally