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": {
            "filemetadata.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": {
      "filemetadata.title": "part of the name.pdf"
    }
  },
  "size": 20
}

Must match x and not y nor z

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

Navigate the JSON tree

(find file by ID)

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

(find if there are any confluence items)

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