Skip to content

Instantly share code, notes, and snippets.

@ivaylopivanov
Last active January 11, 2018 11:05
Show Gist options
  • Save ivaylopivanov/efceb026056ea97bf5bdda00a0fedbc5 to your computer and use it in GitHub Desktop.
Save ivaylopivanov/efceb026056ea97bf5bdda00a0fedbc5 to your computer and use it in GitHub Desktop.
CURL requests for elasticsearch 6.0.0
curl -XPOST 'localhost:9200/twitter/_update_by_query?pretty' -H 'Content-Type: application/json' -d'
{
  "script": {
    "source": "ctx._source.message.add(params.message)",
    "params": {
      "message": "baz"
    }
  },
  "query" : {
    "bool" : {
      "filter" : {
        "terms" : {
          "user" : ["foo", "bar"]
        }
      }
    }
  }
}
'
curl -XPUT 'localhost:9200/twitter/tweet/1?pretty' -H 'Content-Type: application/json' -d'
{
    "user" : "dada",
    "message" : "trying out Elasticsearch"
}
'
curl -XPUT 'localhost:9000/twitter?pretty' -H 'Content-Type: application/json' -d \
'{
  "mappings": {
    "tweet": {
      "dynamic_templates": [
        {
          "cusotm_text": {
            "match":   "*",
            "mapping": {
              "type": "text",
              "analyzer": "my_analyzer"
            }
          }
        }
      ]
    }
  },
  "settings": {
    "analysis": {
        "filter" : {
          "my_word_delimiter" : {
              "type" : "word_delimiter",
              "preserve_original": "true"
          }   
      },
      "analyzer": {
        "my_analyzer": {
            "type": "custom",
            "tokenizer": "whitespace",
            "filter": [
              "standard",
              "lowercase",
              "stop",
              "my_word_delimiter"
            ]
        }
      }
    }
  }
}'

curl -XGET 'localhost:9200/twitter/_search?pretty' -H 'Content-Type: application/json' -d'
{
  "size": 100,
  "sort": {
    "_score": "asc"
  },
  "query": {
    "bool": {
      "should": [
        {
          "bool": {
            "should": [
              {
                "bool": {
                  "must_not": {
                    "exists" : { "field" : "foo" }
                  }
                }
              },
              {
                "bool": {
                  "must": {
                    "match": { "foo" : "bar" }
                  }
                }
              }
            ],
            "minimum_should_match": 1
          }
        }
      ],
      "minimum_should_match": 1
    }
  }
}
'

Create the property and then append.

curl -XPOST 'localhost:9200/twitter/_update_by_query?pretty' -H 'Content-Type: application/json' -d'
{
  "script": {
    "source": "ctx._source.message.add(params.message); if (!ctx._source.containsKey(\"ok\")) { ctx._source.ok = [] } ctx._source.ok.add(params.message);",
    "params": {
      "message": "foo"
    }
  },
  "query" : {
    "bool" : {
      "filter" : {
        "terms" : {
          "user" : ["bar"]
        }
      }
    }
  }
}
'
curl -XPUT 'localhost:9234/twitter?pretty' -H 'Content-Type: application/json' -d \
'{
  "mappings": {
    "tweet": {
      "properties": {
        "slug": {
          "type": "text",
          "analyzer": "my_analyzer"
        }
      }
    }
  },
  "settings": {
    "analysis": {
        "filter" : {
          "my_word_delimiter" : {
              "type" : "word_delimiter",
              "preserve_original": "true"
          }   
      },
      "analyzer": {
        "my_analyzer": {
            "type": "custom",
            "tokenizer": "whitespace",
            "filter": [
              "standard",
              "lowercase",
              "stop",
              "my_word_delimiter"
            ]
        }
      }
    }
  }
}'

Remove property from object for all documents where the user != "baz"

curl -XPOST 'localhost:9200/twitter/_update_by_query?pretty' -H 'Content-Type: application/json' -d'
{
  "script": {
    "source": "if (ctx._source.containsKey(\"foo\") && ctx._source.foo.containsKey(\"bar\")) { ctx._source.foo.remove(\"bar\") }"
  },
  "query" : {
    "bool" : {
      "must_not" : {
        "terms" : {
          "user" : ["baz"]
        }
      }
    }
  }
}
'
curl -XPOST 'localhost:9200/twitter/tweet/1/_update?pretty' -H 'Content-Type: application/json' -d'
{
    "doc" : {
        "name" : "new_name",
        "message": []
    }
}
'
curl -XPOST 'localhost:9200/twitter/_update_by_query?pretty' -H 'Content-Type: application/json' -d'
{
  "script": {
    "source": "ctx._source.message=[\"baz\", 2, 3]"
  },
  "query" : {
    "bool" : {
      "filter" : {
        "terms" : {
          "user" : ["foo", "bar"]
        }
      }
    }
  }
}
'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment