The Elasticsearch JAVA API is roughly divided into the following four categories:
1.TransportCustomer
Step 1: Get the client
Step 2: Perform the corresponding add, delete, modify, check operations
/** * Query Search * @param index * @param type * @param term * @param queryString */ private static void querySearch(String index, String type,String term,String queryString){ Client client = createTransportClient(); SearchResponse response = client.prepareSearch(index) .setTypes(type) // set query type // 1.SearchType.DFS_QUERY_THEN_FETCH = exact query // 2.SearchType.SCAN = scan query, unordered // 3.SearchType.COUNT = If you don't set it, this is the default value, and you can try it yourself .setSearchType(SearchType.DFS_QUERY_THEN_FETCH) // set query keywords .setQuery(QueryBuilders.matchQuery(term, queryString)) .addHighlightedField(term) .setHighlighterPreTags("<em>") .setHighlighterPostTags("</em>") // Set the location of the query data, used for paging .setFrom(0) // Set the maximum number of query result sets .setSize(60) // Set whether to sort by query matching degree .setExplain(true) // The last is to return the search response information .execute() .actionGet(); SearchHits searchHits = response.getHits(); System.out.println("-----------------Search keywords in ["+term+"]["+queryString+"]--------- ------------"); System.out.println("Total matches: "+searchHits.getTotalHits()+" records!"); SearchHit[] hits = searchHits.getHits(); for (SearchHit searchHit : hits) { //Get the highlighted field Map<String, HighlightField> highlightFields = searchHit.getHighlightFields(); HighlightField highlightField = highlightFields.get(term); System.out.println("Highlight field:"+highlightField.getName()+"\nHighlight content:"+highlightField.getFragments()[0].string()); Map<String, Object> sourceAsMap = searchHit.sourceAsMap(); Set<String> keySet = sourceAsMap.keySet(); for (String string : keySet) { //key value value correspondence System.out.println(string+":"+sourceAsMap.get(string)); } System.out.println(); } }
2.RestClient
ES5.0 introduces a new client RestClient, which uses the HTTP API elasticsearch instead of the internal protocol, which requires less dependencies and does not need to pay attention to so many versions.
HttpEntity entity = new NStringEntity( "{ \"query\": { \"match_all\": {}}}", ContentType.APPLICATION_JSON); // alternative: performRequestAsync Response response = restClient.performRequest("POST", "/_search", emptyMap(), entity); String json = toString(response.getEntity()); // ...
3.It is
Jest is a Java HTTP Rest client for ES
/** * @brief: full-text search entry * @throws Exception */ private static void fullTextQuery(String queryString) throws Exception { JestClient jestClient = JestExample.getJestClient(); //Factory pattern implementation SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder(); searchSourceBuilder.query(QueryBuilders.queryStringQuery(queryString)); Search search = new Search.Builder(searchSourceBuilder.toString()) // .addIndex("article") .build(); SearchResult result = jestClient.execute(search); System.out.println(result.getJsonString()); }
4.Spring Data Elasticsearch
...
1), API comparison: http://blog.florian-hopf.de/2016/11/java-clients-elasticsearch.html 2), official website API address: https://www.elastic.co/guide/en/ elasticsearch/client/java-api/2.3/index.html 3), Jest address: https://github.com/searchbox-io/Jest 4), code example: https://github.com/ameizi/elasticsearch- jest-example