In actual combat

Now that you know the basics of ES, it’s time to actually do it.

The installation

Environment: JDK 1.8 ElasticSearch 6.4.0 Window7

Steps:

  1. Unzip elasticSearch-6.4.0.zip
  2. Go to the /bin directory and run elasticSearch. bat in the terminal window. The following startup page is displayed.

The default JVM memory parameters set during startup are large. You can lower -xms and -xmx in the jvm.options directory below the config directory

  1. Install the HEAD plug-in (to view data and health information in the ES cluster)
git clone git://github.com/mobz/elasticsearch-head.git
cd elasticsearch-head
npm install
npm run start
Copy the code

Once started, port 9100 is accessible in a browser

Through the above steps, we have successfully built a testable ES environment, using the header plug-in can be more convenient to view the index, sharding and document information in ES.

Elasticsearch. Yml file in the config directory of elasticSearch due to cross-domain problems

http.cors.enabled: true
http.cors.allow-origin: "*"
Copy the code

Java client

ES is based on HTTP and JSON to interact with the client, so the official RESTClient also provides a client, so we also use this client to implement add, delete, change and check operations.

Rely on:

Org. Elasticsearch: elasticsearch: 5.4.2 - > ES platform version org. Elasticsearch. Client: rest: 5.4.2 - > restClient version IO. Searchbox :jest:5.3.3 -> A wrapper based on RestClientCopy the code
  1. Initialize the Client

When jEST is used, a JestClient of the global singleton is created and then used throughout the life of the application

JestClientFactory factory = new JestClientFactory();
List<String> urls = new ArrayList<>();
//        urls.add("http://10.37.2.142:9900");
urls.add("http://127.0.0.1:9200");
//        urls.add("http://10.37.2.144:9900"); Factory. SetHttpClientConfig (new HttpClientConfig. Builder (urls). / / all the nodes in the cluster address multiThreaded (true) / / whether or not to use multithreading. DefaultMaxTotalConnectionPerRoute (2) / / maximum number of connections. Each route maxTotalConnection (20) / / the whole application maximum number of connections .readTimeout(30000)// Timeout time.build ()); JestClient client = factory.getObject();Copy the code

In the application, you can manage the client through Spring. When the application is closed, you need to call client.shutdownClient(). Release resources

  1. Create index
Settings.Builder settingsBuilder = Settings.builder();
settingsBuilder.put("number_of_shards", 5); Settingsbuilder.put ("number_of_replicas", 1); Jestclient. execute(new CreateIndex.Builder()"index").settings(settingsBuilder.build().getAsMap()).build()); PutMapping PutMapping = new PutMapping.Builder("my_index"."my_type"."{ \"my_type\" : { \"properties\" : { \"message\" : {\"type\" : \"string\", \"store\" : \"yes\"} } } }"
).build();
client.execute(putMapping);
Copy the code

ES allows dynamic creation by default. Therefore, you do not need to create type mapping when creating index. Then, you can dynamically create mapping when saving Document and query all types of mapping under index

The GET 127.0.0.1:9200 / db1 _mapping

  1. Remove the index

    jestClient.execute(new DeleteIndex.Builder(index)
                    .build());
    Copy the code
  2. Add the document

    The document addition is relatively simple

    String source = "{\"user\":\"kimchy\"}"; // Use the pickup String Stringsource = jsonBuilder()
                    .startObject()
                    .field("user"."kimchy")
                    .field("postDate"."date")
                    .field("message"."trying out Elastic Search") .endObject().string(); // Use the constructed JSON Map<String, String>source= new LinkedHashMap<String,String>(); // use map source.put("user"."kimchy");
    Article source= new Article(); // Use the poJO object source.setauthor ("John Ronald Reuel Tolkien");
    source.setContent("The Lord of the Rings is an epic high fantasy novel");
    
    Index index = new Index.Builder(source)
                            .index("twitter"// Specify index. type("tweet"/ / specifiedtype
                            .id("1"Build (); // If id is specified, ES generates id.build (); jestClient.execute(index);Copy the code

    The above is a single increment, we can also use Bulk Bulk to insert ES:

    List<Index> indexList = new ArrayList<>();
    for (OrderDto t : datas) {
                Index indexDoc = new Index.Builder(t).index(index).type(type).build();
                indexList.add(indexDoc);
                Bulk.Builder bulkBuilder = new Bulk.Builder();
                bulkBuilder.addAction(indexList);
                BulkResult br = jestClient.execute(bulkBuilder.build());
    }
    
    Copy the code
  3. Delete the document

Each document is uniquely determined by index, type, and ID, so you can specify a document to delete:

jestClient.execute(new Delete.Builder("1")//id =1
        .index("index")//  index = index
        .type("type") / /type = type
        .build());
Copy the code

Similarly, we can delete it according to the conditions:

String query ="\"{\n"+
                    "\"query\": {\n"+
                "\"match_all\": {}\n"+
            "} \ ""; DeleteByQuery = new deletebyQuery.builder (query).build(); DeleteByQuery = new deletebyQuery.builder (query).build(); JestResult deleteResult = jestClient.execute(deleteByQuery);Copy the code

As above, delete all documents. The query conditions will be described later.

  1. retrieve

Retrieval is the most attractive feature of ES, so let’s take a quick look

    SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
    TermQueryBuilder memberTerm = QueryBuilders.termQuery("memberId", memberId); Integer[] Channels = {101, 106}; TermsQueryBuilder orderChannelTerm = QueryBuilders.termsQuery("orderChannel", channels); //inQuery Integer[] activeType = {0, 8, 9, 7}; TermsQueryBuilder activeTypeTerm = QueryBuilders.termsQuery("activeType", activeType);
    AggregationBuilder orderItemIdAgg =    .field("b2corditemid").size(Integer.MAX_VALUE); Date now = new Date(); Calendar calendar = Calendar.getInstance(); // Get calendar calendar.settime (now); // Assign the current time to calendar.add(calendar.MONTH, -3); Date dBefore = calendar.getTime(); RangeQueryBuilder payTimeRange = QueryBuilders. RangeQuery ("payTime").gt(dBefore).lt(now); / / range query searchSourceBuilder. The query (QueryBuilders boolQuery () must (memberTerm) / / and the query. The filter (orderChannelTerm) / / .filter(activeTypeTerm) .filter(payTimeRange) ); Sort placeTimeSort = new Sort("placeTime", Sort.Sorting.DESC);
    Sort idSort = new Sort("orderId", Sort.Sorting.ASC); searchSourceBuilder.aggregation(orderItemIdAgg); // group by group searchSourceBuilder. From (0).size(20); Search search = new Search.Builder(searchSourceBuilder.toString()) .addType(type).addIndex(index).addsort (placeTimeSort)// Result sort.addsort (idSort).build();Copy the code

The above query could be similar to SQL:

SELECT * FROM bm_client_order_detail o WHERE o.`payTime` >= DATE_SUB(NOW(), INTERVAL 3 MONTH) AND o.memberId = 101204389 AND o.orderChannel IN (101, 106) AND activeType IN (0, 8, 9, 17) GROUP BY o.b2corditemid ORDER BY o.lacetime DESC, o.id ASC LIMIT 0,20Copy the code

The key to review

  • Standalone version of ES installation
  • Install and use elasticSearch -header
  • The use of JestClient

    1. Initialize the
    2. The index created
    3. The index to delete
    4. Add the document
    5. Delete the document
    6. Retrieve the document

To be continued!!