Skip to content

Instantly share code, notes, and snippets.

Created October 20, 2012 20:05
Show Gist options
  • Save anonymous/3924599 to your computer and use it in GitHub Desktop.
Save anonymous/3924599 to your computer and use it in GitHub Desktop.
Demonstation of Elastic Search Versions starting with -1 and not incrementing
package com.visiblehealth.service.elasticsearch;
import org.elasticsearch.action.admin.indices.refresh.RefreshRequest;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.search.SearchType;
import org.elasticsearch.client.Client;
import org.elasticsearch.common.settings.ImmutableSettings;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.node.Node;
import org.elasticsearch.node.NodeBuilder;
public class ElasticSearchServiceTest {
public static void main(String[] args) {
Settings settings = ImmutableSettings.settingsBuilder()
.put("cluster.name", "junit.cluster")
.put("name", "junit.node")
.put("discovery.zen.ping.multicast.enabled", "false")
.put("node.local", "true")
.put("index.store.type", "memory")
.put("index.store.fs.memory.enabled", "true")
.put("index.gateway.type", "none")
.put("gateway.type", "none")
.put("path.data", "target/elastic-search/data")
.put("path.logs", "target/elastic-search/logs")
.put("index.number_of_shards", "1")
.put("index.number_of_replicas", "0").build();
Node node = NodeBuilder.nodeBuilder().settings(settings).node();
Client client = node.client();
String index = "index";
String type = "type";
client.admin().indices().prepareCreate(index)
.setSettings(ImmutableSettings.settingsBuilder()
.put("number_of_shards", 1)
.put("number_of_replicas", 0)).execute()
.actionGet();
String id = "123";
String json = "{\"MyKey\":\"MyValue\"}";
IndexRequest indexRequest = new IndexRequest(index).type(type).id(String.valueOf(id)).source(json);
client.index(indexRequest).actionGet();
client.admin().indices().refresh(new RefreshRequest(index)).actionGet();
SearchResponse results = client.prepareSearch(index)
.setTypes(type)
.setSearchType(SearchType.DFS_QUERY_THEN_FETCH)
.setQuery(QueryBuilders.fieldQuery("MyKey", "MyValue"))
.setExplain(true) // todo does this impact performance?
.execute()
.actionGet();
if(-1 == results.getHits().getHits()[0].getVersion()){
System.err.println("Should start with 1");
}
json = "{\"MyKey\":\"MyValue2\"}";
indexRequest = new IndexRequest(index).type(type).id(String.valueOf(id)).source(json);
client.index(indexRequest).actionGet();
client.admin().indices().refresh(new RefreshRequest(index)).actionGet();
results = client.prepareSearch(index)
.setTypes(type)
.setSearchType(SearchType.DFS_QUERY_THEN_FETCH)
.setQuery(QueryBuilders.fieldQuery("MyKey", "MyValue"))
.setExplain(true) // todo does this impact performance?
.execute()
.actionGet();
if(results.getHits().getHits().length != 0){
System.err.println("Index Didn't get updated!");
}
results = client.prepareSearch(index)
.setTypes(type)
.setSearchType(SearchType.DFS_QUERY_THEN_FETCH)
.setQuery(QueryBuilders.fieldQuery("MyKey", "MyValue2"))
.setExplain(true) // todo does this impact performance?
.execute()
.actionGet();
if(-1 == results.getHits().getHits()[0].getVersion()){
System.err.println("Should start with 1");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment