utrenkner
utrenkner

Reputation: 81

Create new Elasticsearch index from query?

SQL has the "INSERT INTO ... SELECT" statement to fill a table with data from a query. Does anything like this exist for Elasticsearch?

This would prevent me from mass deleting data from an existing index using a query - which is something the official Elasticsearch 2.1 guide warns against:

Don’t use delete-by-query to clean out all or most documents in an index. Rather create a new index and perhaps reindex the documents you want to keep.

(Source: https://www.elastic.co/guide/en/elasticsearch/plugins/current/plugins-delete-by-query.html).

Upvotes: 1

Views: 796

Answers (2)

svanschalkwyk
svanschalkwyk

Reputation: 36

Take a look at the create index API: https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-create-index.html

PUT /test
{
  "settings": {
    "number_of_shards": 1
  },
  "mappings": {
    "properties": {
      "field1": { "type": "text" }
    }
  }
}

Upvotes: 0

Val
Val

Reputation: 217324

You can use the excellent utility from taskrabbit called elasticdump.

There are many options to customize the import process. In your case, I would use the searchBody option and go with something like this:

elasticdump \
  --input=http://HOST:9200/source_index \
  --output=http://HOST:9200/target_index \
  --bulk=true \
  --searchBody='{"query": { "match_all": {} } }'

You can customize the query and only the matched documents from the source_index will be copied over to the target_index

Upvotes: 4

Related Questions