ElasticSearch Query Examples

Find an exact match of a nested field (note that .keyword has to be added to string fields)

{
  "query": {
    "bool": {
      "must": [
        {
          "term": {
            "metadata.title.keyword": "File test.pdf"
          }
        }
      ]
    }
  },
  "size": 200
}

Find exact match by field

{
  "query": {
    "bool": {
      "must": [
        {
          "term": {
            "url": "https://www.google.com/search?hl=es&q=elasticsearch"
          }
        }
      ]
    }
  },
  "size": 200
}

Find a partial match by field

{
  "query": {
    "match": {
      "metadata.title": "part of the name.pdf"
    }
  },
  "size": 20
}

Must match x and not y nor z

{
  "_source": {
    "exclude": [
      "description"
    ]
  },
  "query": {
    "bool": {
      "must": [
        {
          "term": {
            "metadata.drive": "google"
          }
        }
      ],
      "must_not": [
        {
          "match": {
            "metadata.language": "EN"
          }
        },
        {
          "match": {
            "metadata.language": "KO"
          }
        }
      ]
    }
  },
  "size": 200
}

Navigate the JSON tree

(find file by ID)

{
  "query": {
    "bool": {
      "must": [
        {
          "term": {
            "metadata.source.internalfields.id.keyword": "se0feeeacf30922"
          }
        }
      ]
    }
  },
  "size": 200
}

(find if there are any confluence items)

{
  "_source": {
    "exclude": [
      "description"
    ]
  },
  "query": {
    "bool": {
      "must": [
        {
          "term": {
            "metadata.drive.keyword": "google"
          }
        }
      ]
    }
  },
  "size": 10
}