Skip to content

Instantly share code, notes, and snippets.

@karussell
Created May 31, 2011 11:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save karussell/1000366 to your computer and use it in GitHub Desktop.
Save karussell/1000366 to your computer and use it in GitHub Desktop.
Version Returning -1 on the first search
package de.jetwick.es;
import org.elasticsearch.index.query.xcontent.QueryBuilders;
import org.elasticsearch.client.action.search.SearchRequestBuilder;
import java.io.IOException;
import org.elasticsearch.action.admin.cluster.health.ClusterHealthRequest;
import org.elasticsearch.action.admin.indices.create.CreateIndexRequest;
import org.elasticsearch.action.admin.indices.refresh.RefreshRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.Client;
import org.elasticsearch.client.action.index.IndexRequestBuilder;
import org.elasticsearch.common.settings.ImmutableSettings;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.json.JsonXContent;
import org.elasticsearch.indices.IndexAlreadyExistsException;
import org.elasticsearch.node.Node;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import static org.elasticsearch.node.NodeBuilder.*;
/**
* @author Peter Karich, jetwick_@_pannous_._info
*/
public class NewClass {
public static void main(String[] args) throws IOException {
new NewClass().start();
}
private Logger logger = LoggerFactory.getLogger(getClass());
private Client client;
public void start() throws IOException {
Node node = nodeBuilder().
local(true).
settings(ImmutableSettings.settingsBuilder().
put("index.number_of_shards", 2).
put("index.number_of_replicas", 0).
put("gateway.type", "none").
build()).local(true).
build().
start();
client = node.client();
String indexName = "index1";
createIndex(indexName);
waitForYellow(indexName);
XContentBuilder docBuilder = JsonXContent.unCachedContentBuilder().startObject();
docBuilder.field("x", "nice to meet you");
docBuilder.endObject();
IndexRequestBuilder irb = client.prepareIndex(indexName, "something", "1").
setVersion(0).setSource(docBuilder);
irb.execute().actionGet();
refresh(indexName);
SearchRequestBuilder srb = client.prepareSearch(indexName);
srb.setQuery(QueryBuilders.matchAllQuery());
SearchResponse rsp = srb.execute().actionGet();
logger.info("version:" + rsp.hits().hits()[0].getVersion());
DeleteRequestBuilder dr = client.prepareDelete(indexName, "something", "1");
dr.execute().actionGet();
refresh(indexName);
node.stop();
}
public void createIndex(String indexName) {
try {
client.admin().indices().create(new CreateIndexRequest(indexName)).actionGet();
} catch (IndexAlreadyExistsException ex) {
logger.info(indexName + " already exists!", ex);
}
}
public void waitForYellow(String indexName) {
client.admin().cluster().health(new ClusterHealthRequest(indexName).waitForYellowStatus()).actionGet();
}
public void refresh(String... indices) {
client.admin().indices().refresh(new RefreshRequest(indices)).actionGet();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment