Skip to content

Elasicsearch Queries

TODO Examples

Finding Unique Values in Elasticsearch

ADD FIELD DATA

Elasticsearch Fielddata - A Complete Guide Including Default Settings

My Examples

Get basic info

GET /

Get licence

GET /_license

Get cluster stats

GET /_cluster/health?pretty

Get list of indexes

GET /_aliases?pretty=true

Return some result and get total number of items in index

GET /discord-2021/_search
{
    "track_total_hits": true
}

Display Mapping/schema

GET /discord-2021/_mapping

Return random results

GET /discord-2021/_search
{ 
  "query" : {
    "function_score": {
        "random_score": {}
    }
  }
}

Search example stock symbol

GET /discord-2021/_search
{ 
  "query" : {
    "query_string": {
      "query": "GME",
      "default_field": "messageContent"
    }
  }
}

Sort by most recent

GET /discord-2021/_search
{ 
  "query" : {
    "query_string": {
      "query": "GME",
      "default_field": "messageContent"
    }
  },
  "sort": [
      {
        "timestamp": {
          "order": "desc"
        }
      }
    ]
}

Filter based on time alone

GET /discord-2021/_search
{ 
  "query" : {
    "range": {
      "@timestamp": {
        "gte": "2021-01-01",
        "lte": "2021-06-01"
      }
    }
  },
  "sort": [
      {
        "@timestamp": {
          "order": "desc"
        }
      }
    ]
}

Filter based on time and stock sorted by most recent

GET /discord-2021/_search
{
    "query": {
        "bool": {
            "must": {
                "query_string": {
                    "query": "GME",
                    "default_field": "messageContent"
                }
            },
            "should": {
                "range": {
                    "@timestamp": {
                        "gte": "2021-01-01",
                        "lte": "2021-06-01"
                    }
                }
            }
        }
    },
    "sort": [{
        "@timestamp": {
            "order": "desc"
        }
    }]
}

Get number of unique values in a field

GET /discord-2021/_search?size=0
{
    "aggs": {
        "restaurant_count": {
            "cardinality": {
                "field": "guildName.keyword"
            }
        }
    }
}

Get unique values from a field

GET /discord-2021/_search
{
    "aggs": {
        "city": {
            "terms": {
                "field": "guildName.keyword"
            }
        }
    }
}

Sort by size of text field

POST /discord-2021/_search
{
   "query": {
      "exists": {
        "field": "messageContent.keyword"
      }
   },
   "sort": {
      "_script": {
         "script": "doc['messageContent.keyword'].value.length()",
         "type": "number",
         "order": "desc"
      }
   }
}

Number of messages per guild

POST /discord-2021/_search
{
   "query": {
      "exists": {
        "field": "messageContent.keyword"
      }
   },
   "sort": {
      "_script": {
         "script": "doc['messageContent.keyword'].value.length()",
         "type": "number",
         "order": "desc"
      }
   }
}

Number of messages per User

POST /discord-2021/_search
{
   "query": {
      "exists": {
        "field": "messageContent.keyword"
      }
   },
   "sort": {
      "_script": {
         "script": "doc['messageContent.keyword'].value.length()",
         "type": "number",
         "order": "desc"
      }
   }
}

Number of messages per Channel

POST /discord-2021/_search
{
   "query": {
      "exists": {
        "field": "messageContent.keyword"
      }
   },
   "sort": {
      "_script": {
         "script": "doc['messageContent.keyword'].value.length()",
         "type": "number",
         "order": "desc"
      }
   }
}