Created
July 20, 2011 23:09
-
-
Save lmader/1096154 to your computer and use it in GitHub Desktop.
Demonstrates a problem with the uid not getting stored in the index.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* | |
* Demonstrates a problem with the uid not getting stored in the index. | |
* Note that the _source for the child document must be disabled to see the problem. | |
* When the problem occurs, the match_all query fails. | |
* The program loops on creating parent/child docs and then executing the match_all query. | |
* Eventually fails with: | |
* | |
* [shard [[ISnbIZInTo2W6N_DSq1sdg][acme][2]], reason | |
* [RemoteTransportException | |
* [[Ahura][inet[/10.10.30.64:9300]][search/phase/fetch/id]]; nested: | |
* FetchPhaseExecutionException[[acme][2]: | |
* query[ConstantScore(*:*)],from[0],size[10]: Fetch Failed [Failed to load | |
* uid from the index]]; ]] | |
* | |
* | |
* | |
* Depends only on the libs that come with elasticsearch: | |
* elasticsearch-0.16.2.jar | |
* lucene*.jar | |
* | |
* Place the ElasticTest.java file and the above jars in the same folder. | |
* | |
* Compile: | |
* javac -cp .:* ElasticTest.java | |
* Run: | |
* java -cp .:* ElasticTest | |
*/ | |
package com.intrepidls; | |
import java.io.IOException; | |
import java.util.ArrayList; | |
import java.util.Arrays; | |
import java.util.HashMap; | |
import java.util.List; | |
import java.util.Map; | |
import java.util.UUID; | |
import org.elasticsearch.action.admin.indices.create.CreateIndexRequest; | |
import org.elasticsearch.action.search.SearchRequest; | |
import org.elasticsearch.action.search.SearchResponse; | |
import org.elasticsearch.action.search.ShardSearchFailure; | |
import org.elasticsearch.client.Client; | |
import org.elasticsearch.client.Requests; | |
import org.elasticsearch.client.action.index.IndexRequestBuilder; | |
import org.elasticsearch.common.settings.Settings; | |
import org.elasticsearch.common.xcontent.XContentBuilder; | |
import org.elasticsearch.common.xcontent.XContentFactory; | |
import org.elasticsearch.node.Node; | |
import org.elasticsearch.node.NodeBuilder; | |
import org.elasticsearch.search.SearchHit; | |
import org.elasticsearch.transport.RemoteTransportException; | |
public class ElasticTestParentChild2 { | |
private Node elasticNode; | |
private Client client; | |
private static final String CLUSTER_NAME = "lmader"; | |
private static final String PARENT_TYPE_NAME = "contentParent"; | |
private static final String CHILD_TYPE_NAME = "contentChild"; | |
private static final String INDEX_NAME = "acme"; | |
/** | |
* Constructor. Initialize elastic and create the index/mapping | |
*/ | |
public ElasticTestParentChild2() { | |
NodeBuilder nodeBuilder = NodeBuilder.nodeBuilder(); | |
Settings settings = nodeBuilder.settings().put("cluster.name", CLUSTER_NAME).build(); | |
this.elasticNode = nodeBuilder.settings(settings).client(true).node(); | |
this.client = this.elasticNode.client(); | |
String mapping = | |
"{\"contentFiles\": {" + | |
"\"_source\" : {" + | |
"\"enabled\" : false" + | |
"}," + | |
"\"_parent\": {" + | |
"\"type\" : \"content\"" + | |
"}}}"; | |
try { | |
client.admin().indices().create(new CreateIndexRequest(INDEX_NAME).mapping(CHILD_TYPE_NAME, mapping)).actionGet(); | |
} catch (RemoteTransportException e){ | |
// usually means the index is already created. | |
} | |
} | |
public void shutdown() { | |
client.close(); | |
elasticNode.close(); | |
} | |
/** | |
* Index a parent doc | |
*/ | |
public void indexParent(String id, Map<String, Object> objectMap) throws IOException { | |
XContentBuilder builder = XContentFactory.jsonBuilder(); | |
// index content | |
client.prepareIndex(INDEX_NAME, PARENT_TYPE_NAME, id).setSource(builder.map(objectMap)).execute().actionGet(); | |
} | |
/** | |
* Index the file as a child doc | |
*/ | |
public void indexChild(String id, Map<String, Object> objectMap) throws IOException { | |
XContentBuilder builder = XContentFactory.jsonBuilder(); | |
IndexRequestBuilder indexRequestbuilder = client.prepareIndex(INDEX_NAME, CHILD_TYPE_NAME, id); | |
indexRequestbuilder = indexRequestbuilder.setParent(id); | |
indexRequestbuilder = indexRequestbuilder.setSource(builder.map(objectMap)); | |
indexRequestbuilder.execute().actionGet(); | |
} | |
/** | |
* Execute a search based on a JSON String in QueryDSL format. | |
* | |
* Throws a RuntimeException if there are any shard failures to | |
* elevate the visibility of the problem. | |
*/ | |
public List<String> executeSearch(String source) { | |
SearchRequest request = Requests.searchRequest(INDEX_NAME).source(source); | |
List<ShardSearchFailure> failures; | |
SearchResponse response; | |
response = client.search(request).actionGet(); | |
failures = Arrays.asList(response.getShardFailures()); | |
// throw an exception so that we see the shard failures | |
if (failures.size() != 0) { | |
String failuresStr = failures.toString(); | |
if (!failuresStr.contains("reason [No active shards]")) { | |
throw new RuntimeException(failuresStr); | |
} | |
} | |
ArrayList<String> results = new ArrayList<String>(); | |
if (response != null) { | |
for (SearchHit hit : response.hits()) { | |
String sourceStr = hit.sourceAsString(); | |
results.add(sourceStr); | |
} | |
} | |
return results; | |
} | |
/** | |
* Create a document as a parent and index it. | |
* Load a file and index it as a child. | |
*/ | |
public String indexDoc() throws IOException { | |
String id = UUID.randomUUID().toString(); | |
Map<String, Object> objectMap = new HashMap<String, Object>(); | |
objectMap.put("title", "this is a document"); | |
Map<String, Object> objectMap2 = new HashMap<String, Object>(); | |
objectMap2.put("description", "child test"); | |
this.indexParent(id, objectMap); | |
this.indexChild(id, objectMap2); | |
return id; | |
} | |
/** | |
* Perform the match_all query. | |
*/ | |
public void searchMatchAll() throws InterruptedException { | |
String dslString = | |
"{\"query\":{\"match_all\":{}}}"; | |
executeSearch(dslString); | |
} | |
public static void main(String[] args) { | |
ElasticTestParentChild2 elasticTest = new ElasticTestParentChild2(); | |
try { | |
// loop a bunch of times - usually fails before the count is done. | |
int NUM_LOOPS = 10000; | |
System.out.println(); | |
System.out.println("Looping [" + NUM_LOOPS + "] times:"); | |
System.out.println(); | |
for (int i = 1; i < NUM_LOOPS; i++) { | |
elasticTest.indexDoc(); | |
elasticTest.searchMatchAll(); | |
System.out.println(" Success: " + i); | |
} | |
elasticTest.shutdown(); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} finally { | |
elasticTest.shutdown(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment