Refreshing an index

Elasticsearch allows the user to control the state of the searcher using forced refresh on an index. If not forced, the newly indexed document will be only searchable after a fixed time interval (usually one second).

Getting ready

You need an up-and-running Elasticsearch installation, as used in the Downloading and installing Elasticsearch recipe in Chapter 2, Downloading and Setup.

To execute curl via the command line, you need to install curl for your operative system.

To correctly execute the following commands, use the index created in the Creating an index recipe.

How to do it...

The HTTP method used for both operations is POST. The URL formats for refreshing an index, are:

http://<server>/<index_name(s)>/_refresh

The URL formats, for refreshing all the indices in a cluster, are:

http://<server>/_refresh

For refreshing an index, we will perform the following steps:

  1. If we consider the type order of the previous chapter, the call will be:
    curl -XPOST 'http://localhost:9200/myindex/_refresh'
    
  2. The result returned by Elasticsearch should be as follows:
    {"_shards":{"total":4,"successful":2,"failed":0}}
    

How it works...

Near Real-Time (NRT) capabilities are automatically managed by Elasticsearch, which automatically refreshes the indices every second if data is changed in them.

To force a refresh before the internal Elasticsearch interval, you can call the refresh API on one or more indices (more indices are comma separated) or on all the indices.

Elasticsearch doesn't refresh the state of an index at every inserted document as to prevent poor performance due to the excessive I/O required in closing and reopening file descriptors.

Tip

You must force the refresh to have your last index data available for searching.

Generally, the best time to call the refresh is after having indexed a lot of data to be sure that your records are searchable instantly. It's also possible to force a refresh during a document indexing, adding refresh=true as a query parameter. For example:

curl -XPOST  
    'http://localhost:9200/myindex/order/2qLrAfPVQvCRMe7Ku8r0Tw?  
    refresh=true' -d '{
        "id" : "1234",
    "date" : "2013-06-07T12:14:54",
    "customer_id" : "customer1",
    "sent" : true,
 "in_stock_items" : 0,
 "items":[
        {"name":"item1", "quantity":3, "vat":20.0},
        {"name":"item2", "quantity":2, "vat":20.0},
        {"name":"item3", "quantity":1, "vat":10.0}
    ]
}'

See also

  • Refer to the Flushing an index recipe in this chapter to force indexed data writing on disk and the ForceMerge an index recipe to optimize an index for searching
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.119.167.87