One, foreword

1.1 an overview of the

  • Elaticsearch, referred to as es, is an open source and highly extended distributed full-text search engine, which can store and retrieve data in near real time. Its scalability is very good, can be extended to hundreds of servers, processing PB level of data. Es is also developed in Java and uses Lucene as its core to implement all indexing and searching functions, but it aims to hide the complexity of Lucene with a simple RESTful API to make full-text searching simple.

1.2 Application Scenarios

1, Wikipedia, similar to Baidu Encyclopedia, full text search, highlighting, search recommendation /2 (weight, Baidu!)

2, The Guardian (foreign news websites), is similar to sohu news, user behavior log (click browse, collection, comments) + social network data (think of xyz news related), data analysis, to The author of each news articles, let him know that his article of The public feedback (good, bad, hot, trash, despise, worship)

3, Stack Overflow (foreign program exception discussion forum), IT problems, program error, submit, someone will discuss with you and answer, full text search, search related questions and answers, program error, will report the error information pasted into the inside, search for the corresponding answer

GitHub, search hundreds of billions of lines of code

5, e-commerce website, search goods

6. Log data analysis, LogStash log collection, ES for complex data analysis, ELK technology, ElasticSearch + Logstash + Kibana

7. On the commodity price monitoring website, users set the price threshold of a certain commodity, and when it is lower than the threshold, they will send notification messages to users. For example, for the monitoring of toothpaste subscription, if the family set of Colgate toothpaste is lower than 50 yuan, they will inform me and I will buy it.

BI system, Business Intelligence, Business Intelligence. For example, there is a large shopping mall group, BI, analyze the trend of user consumption amount and the composition of user group in certain area in recent 3 years, output several statements related to ** area, in recent 3 years, the annual consumption amount presents 100% growth, and 85% of the user group is senior white collars, opening a new shopping mall. ES performs data analysis and mining, and Kibana performs data visualization

9. Domestic: site search (e-commerce, recruitment, portal, etc.), IT system search (OA, CRM, ERP, etc.), data analysis (a popular use scenario of ES)

Install ElasticSearch

Docker installation:

Download ElasticSearch and Kibaba

Docker Pull Kibana :7.6.2Copy the code

2, configuration,

Can be accessed remotely

mkdir -p /mydata/elasticsearch/config mkdir -p /mydata/elasticsearch/data echo "http.host: 0.0.0.0 "> / mydata/elasticsearch/config/elasticsearch yml chmod -r/mydata/elasticsearch / 777Copy the code

3. Start Elasticsearch

docker run --name elasticsearch -p 9200:9200 -p 9300:9300 \ -e "discovery.type=single-node" \ -e ES_JAVA_OPTS="-Xms64m -Xmx512m" \ -v /mydata/elasticsearch/config/elasticsearch.yml:/usr/share/elasticsearch/config/elasticsearch.yml \ -v /mydata/elasticsearch/data:/usr/share/elasticsearch/data \ -v / mydata/elasticsearch/plugins: / usr/share/elasticsearch/plugins \ - d elasticsearch: 7.6.2Copy the code

Set elasticSearch to start at boot

docker update elasticsearch --restart=always
Copy the code

4. Start Kibana

Docker run - name kibana ELASTICSEARCH_HOSTS = http://192.168.44.104:9200 - p - e 5601:5601 - d kibana: 7.6.2Copy the code

Set automatic startup upon startup

docker update kibana  --restart=always
Copy the code

5, test,

Check the elasticsearch version information: http://192.168.44.104:9200/

{
  "name" : "a0fdde086775",
  "cluster_name" : "elasticsearch",
  "cluster_uuid" : "a-nWkqxuRAaMnE80yD34Mw",
  "version" : {
    "number" : "7.4.2",
    "build_flavor" : "default",
    "build_type" : "docker",
    "build_hash" : "2f90bbf7b93631e52bafb59b3b049cb44ec25e96",
    "build_date" : "2019-10-28T20:40:44.881551Z",
    "build_snapshot" : false,
    "lucene_version" : "8.2.0",
    "minimum_wire_compatibility_version" : "6.8.0",
    "minimum_index_compatibility_version" : "6.0.0-beta1"
  },
  "tagline" : "You Know, for Search"
}
Copy the code

3. Preliminary retrieval

3.1 _cat

  • GET /_cat/nodes: Views all nodes
Request path: http://192.168.44.104:9200/_cat/nodes request results: 127.0.0.1 61, 91, 11, 0.08 0.49 0.87 dilm * 0 adeb7852e00Copy the code
  • GET /_cat/health: Checks the health status of es
Request path: http://192.168.44.104:9200/_cat/health return results: 1611885437 01:57:17 elasticsearch green 1 1 3 3 0 0 0 0-100.0%Copy the code
  • GET /_cat/master: View the primary node
Request path: http://192.168.44.104:9200/_cat/master return results: LrZMyPXvRhWcoLBpYWh09w 127.0.0.1 127.0.0.1 a0fdde086775Copy the code
  • GET /_cat/indicies: displays all indexes, which is equivalent to show databases of the mysql database
Request path: http://192.168.44.104:9200/_cat/indices return results: Kibana_task_manager_1 mReiL6afT12fT -cGRH7N8Q 1 0 2 0 12.5 KB 12.5 KB Green open. apM-agent-configuration Pngy7t61qlqehsm-qr4jpg 1 0 0 283b 283b green open. kibanA_1 ixGtl_9VQBKGqTCuRkYwzw 1 0 50 18.8 KB 18.8 KBCopy the code

3.2 Indexing a document

PUT Customer /external/1; PUT customer/external/1; Save data 1 in external type under customer index as”

PUT customer/external/1 {"name":" name"}Copy the code
{
    "_index": "customer",
    "_type": "external",
    "_id": "1",
    "_version": 1,
    "result": "created",
    "_shards": {
        "total": 2,
        "successful": 1,
        "failed": 0
    },
    "_seq_no": 0,
    "_primary_term": 1
}
Copy the code
The meaning of these returned JSON strings; These, which begin with an underscore, are called metadata and reflect current basic information. _index: Customer indicates the database in which the data is stored. _type: External indicates the type of the data. _id: 1 indicates the ID of the data to be saved. "_version" : 1, indicates the version of the data to be saved. "result" : "created" Indicates that a data is created. If a data is put again, the status changes to updated and the version number changes.Copy the code

Both PUT and POST are available. Post is new. If no ID is specified, the system automatically generates the ID. Specifying an ID modifies this data and adds a version number; Put can be added or modified. PUT must specify an ID; Because the ID of PUT needs to be specified, it is generally used for modification operations. If the ID is not specified, an error will be reported.

3.3 Viewing Documents

GET /customer/external/1

Request path http://192.168.44.104:9200/customer/external/1 returns the {" _index ":" the customer ", "_type" : "external", "_id" : "1", "_version" : 1, "_seq_no" : 0, "_primary_term" : 1, "found" : true, "_source" : {" name ":" ray "}}Copy the code

3.4 Updating Documents

Two ways:

POST customer/external/1/_update {"doc":{"name":"xiaolei"}} {"name" : "xiaolei"} or PUT customer/external/1 {Copy the code
  • The difference between POST and PUT: Post compares the original document data. If the document version is the same, no operation is performed. Put always saves the data and increases the version. If the metadata with _update is the same, no operation is performed. Look at the scenario: For large concurrent updates, no UPDATE.

(1) POST updates the document with _update, then the original will be compared, if the same version number will not change

(2) Put cannot be used with _update, and their versions are automatically increased if _update is not used by default.

3.5 Deleting a Document or Index

Grammar:

DELETE customer/external/1
DELETE customer
Copy the code

Note: ElasticSearch does not provide delete type operations, only index and document deletion operations.

Example: Delete the data whose ID is 1 and continue to query.

{
    "_index": "customer",
    "_type": "external",
    "_id": "1",
    "found": false
}
Copy the code

3.6 EleasticSearch specifies the bulk operation

Syntax format:

{action:{metadata}}\n
{request body  }\n

{action:{metadata}}\n
{request body  }\n
Copy the code

In batch operations, when one execution fails, the rest of the data can continue to execute, that is, independent of each other.

Examples, tested in Kibana:

POST /customer/external/_bulk
{"index":{"_id":"1"}}
{"name":"John Doe"}
{"index":{"_id":"2"}}
{"name":"John Doe"}
Copy the code

Results:

#! Deprecation: [types removal] Specifying types in bulk requests is deprecated. { "took" : 5, "errors" : false, "items" : [ { "index" : { "_index" : "customer", "_type" : "external", "_id" : "1", "_version" : 1, "result" : "created", "_shards" : { "total" : 2, "successful" : 1, "failed" : 0 }, "_seq_no" : 10, "_primary_term" : 1, "status" : 201 } }, { "index" : { "_index" : "customer", "_type" : "external", "_id" : "2", "_version" : 1, "result" : "created", "_shards" : { "total" : 2, "successful" : 1, "failed" : 0 }, "_seq_no" : 11, "_primary_term" : 1, "status" : 201}}]}Copy the code

Test data link:

Github.com/elastic/ela… , import test data,

POST bank/account/_bulk

Fourth, advanced search

4.1 SearchAPI

ES supports two basic methods of retrieval:

  • One is to send search parameters (HRI + retrieval parameters) by using REST Request URIs
  • The other is to send them using the REST Request body (URI + request body)

4.2 Query DSL

Elasticsearch provides a JSON-style DSL for performing queries. This is called the Query DSL, and the Query language is very comprehensive.

4.2.1 Basic Syntax Format

Typical structure of a query statement:

QUERY_NAME:{
   ARGUMENT:VALUE,
   ARGUMENT:VALUE,...
}
Copy the code

If it is for a field, it has the following structure:

{ QUERY_NAME:{ FIELD_NAME:{ ARGUMENT:VALUE, ARGUMENT:VALUE,... }}}Copy the code
GET bank/_search
{
  "query": {
    "match_all": {}
  },
  "from": 0,
  "size": 5,
  "sort": [
    {
      "account_number": {
        "order": "desc"
      }
    }
  ]
}
Copy the code

Query defines how to query:

  • Match_all query type [stands for query of all], ES can combine many query types in query to complete complex query;
  • In addition to the query argument, other arguments can be passed, such as sort, size;
  • Form +size completes pagination
  • Sort, multi-field sort, will sort the following fields internally if the preceding fields are equal.
4.2.2 Returning Some Fields
GET bank/_search
{
  "query": {
    "match_all": {}
  },
  "from": 0,
  "size": 5,
  "sort": [
    {
      "account_number": {
        "order": "desc"
      }
    }
  ],
  "_source": ["balance","firstname"]
  
}
Copy the code
4.2.3 Match Match Query
  • Basic type (non-string), precise control, similar to WHERE in SQL
GET bank/_search
{
  "query": {
    "match": {
      "account_number": "20"
    }
  }
}
Copy the code
4.2.4 match_phrase

Match_phrase matches a phrase as long as the text contains matching conditions. And match is an exact match.

The value to be matched is retrieved as a rectification word (regardless of the word), and the correlation score is given

GET bank/_search
{
  "query": {
    "match_phrase": {
      "address": "mill road"
    }
  }
}
Copy the code
4.2.5 multi_math[Multi-field matching]

State or address contains mill and the score is computed

GET bank/_search
{
  "query": {
    "multi_match": {
      "query": "mill",
      "fields": [
        "state",
        "address"
      ]
    }
  }
}
Copy the code
4.2.6 bool Used for compound query

Compound statements can be merged with any other query statement, including compound statements. Compound statements can nest with each other.

Must: All the conditions listed in must must be fulfilled

GET bank/_search
{
   "query":{
        "bool":{
             "must":[
              {"match":{"address":"mill"}},
              {"match":{"gender":"M"}}
             ]
         }
    }
Copy the code

Must_not, must not match all conditions listed by must_NOT.

Should, should satisfy the conditions listed in should.

4.2.7 Filter [Result filtering]

Not all queries need to generate scores, especially documents that are used only for filtering. To avoid counting scores, Elastic Search automatically checks scenarios and optimizes query execution.

4.2.8 term

Same as match. Matches the value of an attribute. Match is used for full-text retrieval fields, and term is used for other non-text fields

4.2.9 Aggregation

Aggregation provides the ability to group and extract data from data. The simplest aggregation method is roughly equivalent to SQL GROUP BY and SQL aggregation functions. In elasticsearch, perform a search to return to this (hits), and return to aggregate the results at the same time, put in response to all of the hits (hits) separated the forehead ability, this is a very powerful and effective, you can perform a query and multiple polymerization, and get their return results in a single use, avoid the network traffic.

Size =0: no search data is displayed

Aggregate syntax:

"Aggs_name ":{"aggs_name ":{"AGG_TYPE Type of aggregation (AVg,term,terms)":{}}},Copy the code

4.3 the Mapping

Maping is used to define a document and how its contained attributes (fields) are stored and indexed. For example, use maping to define:

  • Which string attributes should be treated as full text fields;

  • Which attributes contain numbers, dates, or geographic locations;

  • Whether all attributes in the document are not indexed (all configuration);

  • Date format;

  • Custom mapping rules to perform dynamic adding properties;

  • Viewing mapping Information

Create the index and specify the mapping

PUT /my_index
{
  "mappings": {
    "properties": {
      "age": {
        "type": "integer"
      },
      "email": {
        "type": "keyword"
      },
      "name": {
        "type": "text"
      }
    }
  }
}
Copy the code

Add a new field map:

PUT /my_index/_mapping
{
  "properties": {
    "employee-id": {
      "type": "keyword",
      "index": false
    }
  }
}
Copy the code

The “index” : false indicates that the new field cannot be retrieved and is redundant.

Update mapping:

An existing field map cannot be updated externally. Updates must create new indexes and migrate data.

Data migration:

4.4 word

A Tokenizer takes a stream of characters, splits it into separate tokens, and then outputs the tokens stream. For example, whitespace Tokenizer splits text when it encounters whitespace characters. It breaks the text “Quick Brown Fox! Divided into [Quick, brown, fox!] .

The tokenizer is also responsible for recording the order of terms or position (used in phrase and word proximity query). And the character offsets of the start and end of the original word that term represents (used to highlight the search).

Elasticsearch comes with a number of built-in word dividers that you can use to build custom analyzers.

About participles: www.elastic.co/guide/en/el…

POST _analyze
{
  "analyzer": "standard",
  "text": "The 2 QUICK Brown-Foxes jumped over the lazy dog's bone."
}
Copy the code

Execution Result:

{
  "tokens" : [
    {
      "token" : "the",
      "start_offset" : 0,
      "end_offset" : 3,
      "type" : "<ALPHANUM>",
      "position" : 0
    },
    {
      "token" : "2",
      "start_offset" : 4,
      "end_offset" : 5,
      "type" : "<NUM>",
      "position" : 1
    },
    {
      "token" : "quick",
      "start_offset" : 6,
      "end_offset" : 11,
      "type" : "<ALPHANUM>",
      "position" : 2
    },
    {
      "token" : "brown",
      "start_offset" : 12,
      "end_offset" : 17,
      "type" : "<ALPHANUM>",
      "position" : 3
    },
    {
      "token" : "foxes",
      "start_offset" : 18,
      "end_offset" : 23,
      "type" : "<ALPHANUM>",
      "position" : 4
    },
    {
      "token" : "jumped",
      "start_offset" : 24,
      "end_offset" : 30,
      "type" : "<ALPHANUM>",
      "position" : 5
    },
    {
      "token" : "over",
      "start_offset" : 31,
      "end_offset" : 35,
      "type" : "<ALPHANUM>",
      "position" : 6
    },
    {
      "token" : "the",
      "start_offset" : 36,
      "end_offset" : 39,
      "type" : "<ALPHANUM>",
      "position" : 7
    },
    {
      "token" : "lazy",
      "start_offset" : 40,
      "end_offset" : 44,
      "type" : "<ALPHANUM>",
      "position" : 8
    },
    {
      "token" : "dog's",
      "start_offset" : 45,
      "end_offset" : 50,
      "type" : "<ALPHANUM>",
      "position" : 9
    },
    {
      "token" : "bone",
      "start_offset" : 51,
      "end_offset" : 55,
      "type" : "<ALPHANUM>",
      "position" : 10
    }
  ]
}
Copy the code

Go to Linux and install the Chinese IK word divider

[root@hadoop104 plugins]# wget https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v7.4.2/elasticsearch-analysis-ik-7.4.2.zip [root @ hadoop104 plugins] # ls elasticsearch - analysis - ik - 7.4.2. ZipCopy the code

Unzip the downloaded file

Case study:

POST _analyze {" Analyzer ":" IK_smart ", "text":" Become a scientist in Berkeley army "} POST _analyze {"token" : "Childhood", "start_offset" : 0, "end_offset" : 2, "type" : "CN_WORD", "position" : 0}, {" token ":" without father ", "start_offset" : 2, "end_offset" : 4, "type" : "CN_WORD", "position" : 1}, {" token ":" no ", "start_offset" : 4, "end_offset" : 5, "type" : "CN_CHAR", "position" : 2}, {" token ":" mother, "" start_offset" : 5, "end_offset" : 6, "type" : "CN_CHAR", "position" : 3}, {" token ":" ", "start_offset" : 6, "end_offset" : 7, "type" : "CN_CHAR", "position" : 4}, {" token ":" the orphan "and" start_offset ": 7," end_offset ": 9," type ":" CN_WORD ", "position" : 5}, {" token ": "Into", "start_offset" : 10, "end_offset" : 11, "type" : "CN_CHAR", "position" : 6}, {" token ":" years ", "start_offset" : 11, "end_offset" : 13, "type" : "CN_WORD", "position" : 7}, {"token" : "become ", "start_offset" : 13, "end_offset" : 13 15, "type" : "CN_WORD", "position" : 8}, {" token ":" the United States ", "start_offset" : 15, "end_offset" : 17, "type" : "CN_WORD", "position" : 9}, {"token" : "Berkeley ", "start_offset" : 17, "end_offset" : 20, "type" : CN_WORD, "position" : 10}, {"token" : "military ", "start_offset" : 20, "end_offset" : 22, "type" : "CN_WORD", "position" : 11}, {" token ":" ", "start_offset" : 22, "end_offset" : 23, "type" : "CN_CHAR", "position" : 12}, {" token ":" a ", "start_offset" : 23, "end_offset" : 25, "type" : "CN_WORD", "position" : 13}, {" token ": "Start_offset" : 25, "end_offset" : 28, "type" : "CN_WORD", "position" : 14}]Copy the code
GET _analyze {"analyze ":" ik_max_word", "text":" I am Chinese "} GET _analyze {"analyze ":" IK_max_word ", "TEXT ":" I am Chinese"} 0, "end_offset" : 1, "type" : "CN_CHAR", "position" : 0}, {" token ":" ", "start_offset" : 1, "end_offset" : 2, "type" : "CN_CHAR", "position" : 1}, {" token ":" Chinese ", "start_offset" : 2, "end_offset" : 5, "type" : "CN_WORD", "position" : 2}, {" token ":" Chinese ", "start_offset" : 2, "end_offset" : 4, "type" : "CN_WORD", "position" : 3}, {" token ":" the people ", "start_offset" : 3, "end_offset" : 5, "type" : "CN_WORD", "position" : 4}]}Copy the code

You can see that there are some differences between these two participles.

Fifth, Elasticsearch – Rest – Client

Elasticsearch-rest-client: an official RestClient that encapsulates ES operations and is easy to use.

5.1 Importing Dependencies

The version here matches the ELK version you installed

<dependency> <groupId>org.elasticsearch.client</groupId> <artifactId>elasticsearch-rest-high-level-client</artifactId> The < version > 7.6.2 < / version > < / dependency >Copy the code

ELK version of a

< elasticsearch version > 7.6.2 < / elasticsearch version >Copy the code

5.2 the test class

  1. API test data
@SpringBootTest class ElasticsearchStudyApplicationTests { @Autowired private RestHighLevelClient restHighLevelClient; @test void createIndex() throws IOException {// 1. Create an index request CreateIndexRequest request = new CreateIndexRequest("lei_index"); // 2. Execute request IndicesClient. Get the response after the request CreateIndexResponse CreateIndexResponse = restHighLevelClient. Indices (). The create (request, RequestOptions. DEFAULT); System.out.println(createIndexResponse); @test void testExistIndex() throws IOException{GetIndexRequest Request =new GetIndexRequest("lei_index");} @test void testExistIndex() throws IOException{GetIndexRequest request =new GetIndexRequest("lei_index"); boolean exists=restHighLevelClient.indices().exists(request,RequestOptions.DEFAULT); System.out.println(exists); @test void testDelete() throws IOException {DeleteIndexRequest Request =new DeleteIndexRequest("lei");} @test void testDelete() throws IOException {DeleteIndexRequest request =new DeleteIndexRequest(" LEI "); AcknowledgedResponse delete = restHighLevelClient.indices().delete(request, RequestOptions.DEFAULT); System.out.println(delete.isAcknowledged()); } / / = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = / / second, documentation, Test / / 2.1 Test add documents @ Test void testAddDocument ()  throws IOException { User user = new User("xiaolei", 24); IndexRequest request =new IndexRequest("lei_index"); / / rules request. Id (" 1 "); request.timeout(TimeValue.timeValueSeconds(1)); request.timeout("1s"); // Put our data into the request request.source(json.tojsonString (user), xcontentType.json); / / the client sends a request, get the results returned IndexResponse index = restHighLevelClient. Index (request, RequestOptions. DEFAULT); System.out.println(index.toString()); System.out.println(index.status()); } //2.2 Testing whether a document exists get /index/doc/1 @test void testIsExists() throws IOException {GetRequest Request = new GetRequest("lei_index", "1"); The context of the / / don't get returned _source request. FetchSourceContext (new fetchSourceContext (false)); request.storedFields("_none_"); boolean exists = restHighLevelClient.exists(request, RequestOptions.DEFAULT); System.out.println(exists); @test void testInfo() throws IOException {GetRequest Request =new GetRequest("lei_index","1"); GetResponse documentFields = restHighLevelClient.get(request, RequestOptions.DEFAULT); System.out.println(documentFields.getSourceAsString()); // Print the document contents system.out.println (documentFields); @test void testUpdate() throws IOException {UpdateRequest request = new UpdateRequest("lei_index", "1"); request.timeout("1s"); User user = new User("xxxxxx", 3); request.doc(JSON.toJSONString(user),XContentType.JSON); UpdateResponse update = restHighLevelClient.update(request, RequestOptions.DEFAULT); System.out.println(update.status()); @test void testDeleteDocument() throws IOException {DeleteRequest request = new DeleteRequest("lei_index", "1"); request.timeout("1s"); DeleteResponse delete = restHighLevelClient.delete(request, RequestOptions.DEFAULT); System.out.println(delete.status()); } //2.6 Inserting Data in batches @test void testBulkRequest() throws IOException {BulkRequest BulkRequest = new BulkRequest(); bulkRequest.timeout("10s"); ArrayList<User> list = new ArrayList<>(); list.add(new User("xiaolei2",23)); list.add(new User("xiaolei3",23)); list.add(new User("xiaolei4",23)); list.add(new User("xiaolei25",23)); list.add(new User("xiaolei26",23)); list.add(new User("xiaolei27",23)); for (int i = 0; i < list.size(); i++) { bulkRequest.add(new IndexRequest("lei_index") .id(""+(1+i)).source(JSON.toJSONString(list.get(i)),XContentType.JSON)); } BulkResponse bulk = restHighLevelClient.bulk(bulkRequest, RequestOptions.DEFAULT); System.out.println(bulk.status()); System.out.println(bulk.hasFailures()); } // 2.7 query // SearchRequest SearchRequest // SearchSourceBuilder conditional construct // QueryBuilders. TermQuery precision // HightlightBuilder @test void testSearch() throws IOException {SearchRequest Request = new SearchRequest("lei_index"); SearchSourceBuilder sourceBuilder = new SearchSourceBuilder(); // Query condition, We can use QueryBuilders tools to implement / / QueryBuilders termQuery accurate / / QueryBuilders matchAllQuery match all TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery("name", "xiaolei27"); MatchAllQueryBuilder matchAllQueryBuilder = QueryBuilders.matchAllQuery(); sourceBuilder.timeout(new TimeValue(60, TimeUnit.SECONDS)); sourceBuilder.query(termQueryBuilder); request.source(sourceBuilder); SearchResponse search = restHighLevelClient.search(request, RequestOptions.DEFAULT); System.out.println(JSON.toJSONString(search.getHits())); System.out.println("=============================="); for (SearchHit hit : search.getHits().getHits()) { System.out.println(hit.getSourceAsMap()); }}}Copy the code

2) a Java implementation

/** * select * from bank where address contains mill's age distribution and average age, * @throws IOException */ @test public void searchData() throws IOException {//1. SearchRequest = new SearchRequest(); Searchrequest.indices ("bank"); Soursourcebuilder = new SearchSourceBuilder(); sourceBuilder.query(QueryBuilders.matchQuery("address","Mill")); / / 1.2.1) shall be carried out in accordance with the age distribution of polymerization TermsAggregationBuilder ageAgg = AggregationBuilders. Terms (" ageAgg "). The field (" age "). The size (10); sourceBuilder.aggregation(ageAgg); AvgAggregationBuilder ageAvg = AggregationBuilders. Avg ("ageAvg").field("age"); sourceBuilder.aggregation(ageAvg); AvgAggregationBuilder balanceAvg = AggregationBuilders. Avg ("balanceAvg"). Field ("balance"); sourceBuilder.aggregation(balanceAvg); System.out.println(" search criteria: "+sourceBuilder); searchRequest.source(sourceBuilder); SearchResponse SearchResponse = client.search(searchRequest, requestOptions.default); System.out.println(" search result: "+searchResponse); SearchHits = searchResponse.gethits (); SearchHit[] searchHits = hits.getHits(); for (SearchHit searchHit : searchHits) { String sourceAsString = searchHit.getSourceAsString(); Account account = JSON.parseObject(sourceAsString, Account.class); System.out.println(account); } / / 4. The aggregation information Aggregations Aggregations = the searchResponse. The getAggregations (); Terms ageAgg1 = aggregations.get("ageAgg"); for (Terms.Bucket bucket : ageAgg1.getBuckets()) { String keyAsString = bucket.getKeyAsString(); System.out.println(" age: "+keyAsString+" ==> "+bucket.getDocCount()); } Avg ageAvg1 = aggregations.get("ageAvg"); System.out.println(" average age: "+ ageavg1.getValue ()); Avg balanceAvg1 = aggregations.get("balanceAvg"); System.out.println(" average salary: "+ balanceavg1.getValue ()); }Copy the code