Skip to content

Instantly share code, notes, and snippets.

@gustavobmaia
Created July 19, 2012 04:05
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 gustavobmaia/3140681 to your computer and use it in GitHub Desktop.
Save gustavobmaia/3140681 to your computer and use it in GitHub Desktop.
Problem Parents & Children limit, version 0.19.8
import java.util.Arrays;
import org.elasticsearch.action.admin.indices.create.CreateIndexRequestBuilder;
import org.elasticsearch.action.index.IndexRequestBuilder;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.search.SearchRequestBuilder;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.Client;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.ImmutableSettings;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.TopChildrenQueryBuilder;
import org.elasticsearch.node.Node;
import org.elasticsearch.node.NodeBuilder;
import org.elasticsearch.search.SearchHit;
/**
* <p>
* Goshme Soluções para a Internet LTDA<br />
* Projeto JusBrasil<br />
* <a href="http://www.jusbrasil.com.br/">http://www.jusbrasil.com.br/</a>
* </p>
*
* @since $Date$
* @version CVS $Revision$
* @author <a href="mailto:gustavo@jusbrasil.com.br>Gustavo Barreto Maia</a>
*/
public class ParentChildrenTest {
public static void main(String[] args) throws Exception {
long numResult = execute(10);
if(numResult == 1){
System.out.println("It's OK");
}
numResult = execute(100);
if(numResult == 0){
System.err.println("Have some problem when index one document with 100 children");
}
}
public static long execute(int numChild) throws Exception {
long numReturnResults = 0;
Node node = NodeBuilder.nodeBuilder().settings(ImmutableSettings.settingsBuilder().put("gateway.type", "local").put("cluster.name", "gustavo-index")).node();
String host1 = "localhost";
Settings b = ImmutableSettings.settingsBuilder().put("client.transport.sniff", false).put("cluster.name", "gustavo-index").put("index.number_of_shards", 5).put("index.number_of_replicas", 1).build();
Client client = new TransportClient(b).addTransportAddress(new InetSocketTransportAddress(host1, 9300));
String indexName = "indextest";
String indexMap = "diarioindexmap";
try {
client.admin().indices().prepareDelete(indexName).execute().actionGet();
} catch (Exception e) {
}
CreateIndexRequestBuilder builder = client.admin().indices().prepareCreate(indexName).setSettings("{\"analysis\":{\"analyzer\":{\"default\":{\"tokenizer\":\"whitespace\",\"filter\":[\"standard\",\"lowercase\"]}}},\"index.number_of_replicas\":1,\"index.number_of_shards\":10,\"index.store.compress.stored\":true}");
String map = "{\"diarioindexmap\":" +
" {" +
" \"properties\":{" +
" \"titulo\":{\"type\":\"string\",\"store\":\"yes\",\"index\":\"analyzed\"}," +
" \"id\":{\"type\":\"long\",\"store\":\"yes\",\"index\":\"analyzed\"}," +
" \"body\":{\"type\":\"string\",\"store\":\"yes\",\"index\":\"analyzed\"}" +
"}" +
"}" +
"}";
//_parent
String mapChild = "{\"diarioindexmapchild\":" +
"{\"_parent\":{\"type\":\"diarioindexmap\"}," +
" \"properties\":{" +
" \"titulo\":{\"type\":\"string\",\"store\":\"yes\",\"index\":\"analyzed\"}," +
" \"id\":{\"type\":\"long\",\"store\":\"yes\",\"index\":\"analyzed\"}," +
" \"body\":{\"type\":\"string\",\"store\":\"yes\",\"index\":\"analyzed\"}" +
"}" +
"}" +
"}";
builder.addMapping(indexMap, map);
builder.addMapping("diarioindexmapchild", mapChild);
builder.execute().actionGet();
StringBuilder corpo = new StringBuilder();
for (int j = 0; j < 1; j++) {
corpo.append(" gustavo GO lei sdfa ");
}
try {
XContentBuilder docBuilder = XContentFactory.jsonBuilder().startObject();
docBuilder.field("titulo", corpo.toString());
docBuilder.field("id", 1l);
docBuilder.field("corpo", corpo.toString());
docBuilder.endObject();
// feed previously created doc
IndexRequestBuilder irb = client.prepareIndex(indexName, indexMap, String.valueOf(1l)).setSource(docBuilder);
IndexResponse iresp = irb.execute().actionGet();
// System.out.println(iresp.getId());
} catch (Exception e) {
e.printStackTrace();
}
for (int i = 0; i < numChild; i++) {
try {
XContentBuilder docBuilder = XContentFactory.jsonBuilder().startObject();
docBuilder.field("titulo", "child : " + i +" " + corpo.toString());
docBuilder.field("id", i);
docBuilder.field("corpo", "child : " + i +" " + corpo.toString());
docBuilder.endObject();
// feed previously created doc
IndexRequestBuilder irb = client.prepareIndex(indexName, "diarioindexmapchild", String.valueOf(i)).setSource(docBuilder);
irb.setParent(String.valueOf(1l));
IndexResponse iresp = irb.execute().actionGet();
// System.out.println(iresp.getId());
} catch (Exception e) {
e.printStackTrace();
}
}
Thread.sleep(1000);
numReturnResults = doChildSearch(client,indexName);
client.close();
node.close();
return numReturnResults;
}
private static TopChildrenQueryBuilder buildChildSearch(String userQuery){
QueryBuilder query = QueryBuilders.termQuery("corpo", userQuery);
if (query != null) {
TopChildrenQueryBuilder topChildquery = QueryBuilders.topChildrenQuery("diarioindexmapchild", query);
topChildquery.score("max");
return topChildquery;
}
return null;
}
private static long doChildSearch(Client client, String indexName) {
// Search
String keyword = "lei";
SearchRequestBuilder builderSearch = client.prepareSearch(indexName);
TopChildrenQueryBuilder qb = buildChildSearch(keyword);// QueryBuilders.queryString(keyword);
builderSearch.setQuery(qb);
SearchResponse rsp = builderSearch.execute().actionGet();
if (rsp.failedShards() > 0) {
System.err.println("*** FAILED SHARDS ***");
System.err.println(Arrays.toString(rsp.shardFailures()));
}
SearchHit[] docs = rsp.getHits().getHits();
long numResults = rsp.getHits().getTotalHits();
for (SearchHit sd : docs) {
System.out.println("forID : " + sd.getId() + " " + sd.getScore());
if (sd.getHighlightFields().get("titulo") != null) {
String[] frags = sd.getHighlightFields().get("titulo").getFragments();
for (String value : frags) {
System.out.println(value + "...");
}
} else {
System.err.println("####################");
System.err.println("why don't have Highlighting values ?");
System.err.println("####################");
}
}
return numResults;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment