Skip to content

Instantly share code, notes, and snippets.

@kimchy
Created June 25, 2012 21:36
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 kimchy/2991425 to your computer and use it in GitHub Desktop.
Save kimchy/2991425 to your computer and use it in GitHub Desktop.
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.QueryBuilders;
import org.elasticsearch.index.query.QueryStringQueryBuilder;
import org.elasticsearch.node.Node;
import org.elasticsearch.node.NodeBuilder;
import org.elasticsearch.search.SearchHit;
import java.util.Arrays;
/**
* <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>
*
* @author <a href="mailto:gustavo@jusbrasil.com.br>Gustavo Barreto Maia</a>
* @version CVS $Revision$
* @since $Date$
*/
public class Test2 {
public static void main(String[] args) {
Node node = NodeBuilder.nodeBuilder().settings(ImmutableSettings.settingsBuilder()
.put("gateway.type", "none").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,\"index.store.type\":\"niofs\",\"index.fs.memory.enabled\":\"true\"}");
String map = "{\"diarioindexmap\":{\"_boost\":{\"name\":\"doc_boost\",\"null_value\":1},\"_all\":{\"enabled\":true},\"_source\":{\"enabled\":false,\"compress\":true},\"properties\":{\"pdf\":{\"type\":\"integer\",\"store\":\"yes\",\"index\":\"no\",\"include_in_all\":\"false\"},\"prioridade\":{\"type\":\"integer\",\"store\":\"yes\",\"index\":\"no\",\"include_in_all\":\"false\"},\"paginaOrdenacao\":{\"type\":\"integer\",\"store\":\"yes\",\"index\":\"no\",\"include_in_all\":\"false\"},\"pagina\":{\"type\":\"string\",\"store\":\"yes\",\"index\":\"no\",\"include_in_all\":\"false\"},\"descricao\":{\"type\":\"string\",\"store\":\"yes\",\"index\":\"no\",\"include_in_all\":\"false\"},\"url\":{\"type\":\"string\",\"store\":\"yes\",\"index\":\"no\"},\"destaque\":{\"type\":\"string\",\"store\":\"no\",\"index\":\"no\",\"boost\":2.0,\"include_in_all\":\"true\"},\"corpo\":{\"type\":\"string\",\"store\":\"yes\",\"index\":\"no\",\"include_in_all\":\"true\",\"analyzer\":\"default\"},\"titulo\":{\"type\":\"string\",\"store\":\"yes\",\"index\":\"no\",\"boost\":3.0,\"include_in_all\":\"true\",\"analyzer\":\"default\"},\"expand\":{\"type\":\"string\",\"store\":\"no\",\"index\":\"no\",\"include_in_all\":\"true\"},\"titulos-topicos\":{\"type\":\"string\",\"store\":\"no\",\"index\":\"no\",\"include_in_all\":\"true\"},\"sigla\":{\"type\":\"string\",\"store\":\"yes\",\"index\":\"not_analyzed\",\"include_in_all\":\"true\"},\"caderno\":{\"type\":\"string\",\"store\":\"yes\",\"index\":\"not_analyzed\",\"include_in_all\":\"true\"},\"artefato\":{\"type\":\"integer\",\"store\":\"yes\",\"index\":\"not_analyzed\"},\"id\":{\"type\":\"long\",\"store\":\"yes\",\"index\":\"not_analyzed\"},\"data\":{\"type\":\"date\",\"format\":\"basic_date_time\",\"store\":\"yes\",\"index\":\"not_analyzed\"},\"idPerfil\":{\"type\":\"long\",\"store\":\"yes\",\"index\":\"not_analyzed\"},\"idGrupo\":{\"type\":\"long\",\"store\":\"yes\",\"index\":\"not_analyzed\"},\"privado\":{\"type\":\"integer\",\"store\":\"yes\",\"index\":\"not_analyzed\"},\"jus-doc\":{\"type\":\"string\",\"store\":\"yes\",\"index\":\"not_analyzed\",\"include_in_all\":\"false\"},\"topicos_perfis_ids\":{\"type\":\"string\",\"store\":\"yes\",\"index\":\"analyzed\",\"include_in_all\":\"false\"}}}}";
builder.addMapping(indexMap, map);
builder.execute().actionGet();
StringBuilder corpo = new StringBuilder();
for (int j = 0; j < 500; j++) {
corpo.append(" gustavo GO lei sdfa ");
}
for (int i = 0; i < 50; i++) {
try {
XContentBuilder docBuilder = XContentFactory.jsonBuilder().startObject();
docBuilder.field("titulo", corpo.toString());
docBuilder.field("descricao", corpo.toString());
docBuilder.field("corpo", corpo.toString());
docBuilder.endObject();
// feed previously created doc
IndexRequestBuilder irb = client.prepareIndex(indexName, indexMap, String.valueOf(i)).setSource(docBuilder);
IndexResponse iresp = irb.execute().actionGet();
System.out.println(iresp.getId());
} catch (Exception e) {
e.printStackTrace();
}
}
//Search
String keyword = "lei";
SearchRequestBuilder builderSearch = client.prepareSearch(indexName);
QueryStringQueryBuilder qb = QueryBuilders.queryString(keyword);
qb.field("_all");
qb.analyzer("default");
// builderSearch.addFields("titulo", "descricao", "corpo");
// builder.addSort("dateval", SortOrder.ASC);
// builder.addFacet(FacetBuilders.termsFacet("cor").field("corval"));
// builderSearch.setFrom(0).setSize(10);
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();
for (SearchHit sd : docs) {
System.out.println("forID : " + sd.getId() + " " + sd.getScore());
}
client.close();
node.close();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment