Elasticsearch java API example package com.etonenet.msisdn;import com.codahale.metrics.MetricRegistry;import com.codahale.metrics.Slf4jReporter;import com.codahale.metrics.Timer;import com.fasterxml.jackson.core.JsonProcessingException;impo
package com.etonenet.msisdn; import com.codahale.metrics.MetricRegistry; import com.codahale.metrics.Slf4jReporter; import com.codahale.metrics.Timer; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; import example.springdata.elasticsearch.conference.Conference; import org.elasticsearch.action.get.GetResponse; import org.elasticsearch.action.index.IndexResponse; import org.elasticsearch.action.search.SearchRequest; import org.elasticsearch.action.update.UpdateRequest; import org.elasticsearch.action.update.UpdateResponse; import org.elasticsearch.client.transport.TransportClient; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.transport.InetSocketTransportAddress; import org.elasticsearch.common.xcontent.XContentType; import org.elasticsearch.index.query.BoolQueryBuilder; import org.elasticsearch.index.query.MatchPhraseQueryBuilder; import org.elasticsearch.index.query.MatchQueryBuilder; import org.elasticsearch.index.query.Operator; import org.elasticsearch.index.search.MatchQuery; import org.elasticsearch.search.SearchHit; import org.elasticsearch.search.builder.SearchSourceBuilder; import org.elasticsearch.search.fetch.subphase.FetchSourceContext; import org.elasticsearch.transport.client.PreBuiltTransportClient; import org.junit.After; import org.junit.Before; import org.junit.Test; import org.slf4j.LoggerFactory; import java.net.InetAddress; import java.util.*; import java.util.concurrent.TimeUnit; import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder; /** * Created by wxu on 2017/8/24. */ public class ApplicationTest { public static final String HOST = "10.8.11.23"; public static final String INDEX = "twitter"; public static final String TYPE = "tweet"; private TransportClient client; private Slf4jReporter reporter; static final MetricRegistry metrics = new MetricRegistry(); @Before public void setUp() throws Exception { // on startup byte[] as = new byte[]{10, 8, 11, 23}; client = new PreBuiltTransportClient(Settings.EMPTY) // .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByAddress(as), 9300)); .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(HOST), 9300)); reporter = Slf4jReporter.forRegistry(metrics) .outputTo(LoggerFactory.getLogger("com.etonenet.epay.metrics.MetricService")) .convertRatesTo(TimeUnit.SECONDS) .convertDurationsTo(TimeUnit.MILLISECONDS) .build(); reporter.start(1, TimeUnit.SECONDS); } @After public void tearDown() throws Exception { // on shutdown client.close(); } @Test public void index() throws Exception { Mapjson = new HashMap (); json.put("user", "kimchy"); json.put("postDate", new Date()); json.put("message", "trying out Elasticsearch"); IndexResponse response = client.prepareIndex("twitter", "tweet", "1") .setSource(json) .get(); System.out.println(response); } @Test public void get() throws Exception { GetResponse response = client.prepareGet("twitter", "tweet", "1").get(); Map source = response.getSource(); System.out.println(source); } @Test public void update() throws Exception { UpdateRequest updateRequest = new UpdateRequest(); updateRequest.index("twitter"); updateRequest.type("tweet"); updateRequest.id("1"); updateRequest.doc(jsonBuilder() .startObject() .field("gender", "male") .field("user", "update") .endObject()); UpdateResponse updateResponse = client.update(updateRequest).get(); System.out.println(updateResponse); } @Test public void stressTest() throws JsonProcessingException { long start = System.currentTimeMillis(); ObjectMapper mapper = new ObjectMapper(); Conference build = Conference.builder().date("2014-11-06").name("Spring eXchange 2014 - London") .keywords(Arrays.asList("java", "spring")).build(); for (int i = 0; i < 50_000; i++) { String id = String.valueOf(i); build.setId(id); build.setName(UUID.randomUUID().toString()); IndexResponse response = client.prepareIndex(INDEX, TYPE, id) .setSource(mapper.writeValueAsBytes(build), XContentType.JSON) .get(); } System.out.println(TimeUnit.MILLISECONDS.toSeconds(System.currentTimeMillis() - start)); } @Test public void testGet() throws Exception { Timer timer = metrics.timer("twitter_get"); Random random = new Random(); for (int i = 0; i < 5_000_000; i++) { Timer.Context context = timer.time(); client.prepareGet("twitter", "tweet", String.valueOf(random.nextInt(50_000))); context.stop(); } } @Test public void testSearch() throws Exception { SearchRequest request = new SearchRequest(); SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder(); searchSourceBuilder.from(0); searchSourceBuilder.size(4); String[] fields = {"name", "date"}; // 自定义字段 FetchSourceContext sourceContext = new FetchSourceContext(true, fields, null); searchSourceBuilder.fetchSource(sourceContext); BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder(); boolQueryBuilder.should( // new MatchQueryBuilder("hostIP", HOST) new MatchQueryBuilder("id", "73") // .operator(Operator.AND) // .type(MatchQuery.Type.PHRASE) ); searchSourceBuilder.query(boolQueryBuilder); request.source(searchSourceBuilder); request.indices(INDEX); request.types(TYPE); for (SearchHit hit : client.search(request).actionGet().getHits()) { System.out.println(hit.getSourceAsString()); } } }