Primary knowledge elasticsearch_2 (query and Integration Springboot)

Source: Internet
Author: User

Initialization

Firstly, the JSON file downloaded by the flag net is put into ES, using the following command:

curl -H "Content-Type: application/json" -XPOST ‘localhost:9200/bank/account/_bulk?pretty&refresh‘ --data-binary "@accounts.json"curl ‘localhost:9200/_cat/indices?v‘
Search API

Then you can start querying. You can query in 2 ways, either by placing it in RESTAPI or by placing it in the RESTAPI request body. Obviously the form of the request is more representative and more readable.
Looking at the RESTAPI, the following statement queries all the documents for the bank index.

GET /bank/_search?q=*&sort=account_number:asc&pretty

The argument list represents the q=* query all, SORT=ACCOUNT_NUMBER:ASC, representing the results in ascending order of Account_number, and the pretty representative returns the results in the form of JSON output.
You can look at the return value, and the return value description is written in the comment:

 {"Took": 63,//whether the delay "timed_out": false,//the current search for how many shards "_shards": {"Total": 5, "successful": 5, "skipped": 0, "failed": 0},//Search Result "hits": {//number of bars matching search results "Total": +, "Max_score": null, Array of results, default display of the first 10 "hits": [{"_index": "Bank", "_type": "Account", "_id": "0",//Sort field "so RT ": [0]," _score ": null," _source ": {" Account_number ": 0," balance ": 16623," FirstName ":" Bradshaw "," LastName ":" Mc Kenzie "," Age ":" Gender ":" F "," Address ":" 244 Columbus Place "," Employer ":" Euron "," email ":" [email protected] " , ' City ': ' Hobucken ', ' state ': ' CO '}}, {"_index": "Bank", "_type": "Account", "_id": "1", "Sort": [1], "_score": null, "_source": {"Account_number": 1, "balance": 39225, "FirstName": "Amber", "LastName": "Duke", "age ": +," gender ":" M "," Address ":" 880 Holmes Lane "," Employer ":" Pyrami "," email ":" [email protected] "," City ":" Brogan "," state ":" IL "}}, ...] }}

Requests can be made in the form of the request body:

GET /bank/_search{  "query": { "match_all": {} },  "sort": [    { "account_number": "asc" }  ]}

The returned result is the same.
By adding parameters, you can control the number of result bars returned:

// 展示一条GET /bank/_search{  "query": { "match_all": {} },  "size": 1}// 第10条~第20条GET /bank/_search{  "query": { "match_all": {} },  "from": 10,  "size": 10}

The following is a reverse order according to balance

GET /bank/_search{  "query": { "match_all": {} },  "sort": { "balance": { "order": "desc" } }}

By default, the returned source is the one that contains all the data structures, and if we do not want to return all of the document structure, you can use the following statement:

GET /bank/_search{  "query": { "match_all": {} },  "_source": ["account_number", "balance"]}

You can look at the return value:

{    "took": 11,    "timed_out": false,    "_shards": {        "total": 5,        "successful": 5,        "failed": 0    },    "hits": {        "total": 999,        "max_score": 1,        "hits": [            {                "_index": "bank",                "_type": "account",                "_id": "25",                "_score": 1,                "_source": {                    "account_number": 25,                    "balance": 40540                }            }        ]    }}

Next you can look at the filter according to the field, the following filter the order of Account_number 20

GET /bank/_search{  "query": { "match": { "account_number": 20 } }}

The following filters out the results of the address values containing Mill,lane

GET /bank/_search{  "query": { "match": { "address": "mill lane" } }}

If you want to filter the containing phrase Mill Lane:

GET /bank/_search{  "query": { "match_phrase": { "address": "mill lane" } }}

Then look at the bool query.
The following bool query is the same as the above query, and the query contains the phrase containing the phrase Mill Lane:

GET /bank/_search{  "query": {    "bool": {      "must": [        { "match": { "address": "mill" } },        { "match": { "address": "lane" } }      ]    }  }}

Must represents that all queries must return TRUE. Take a look at the following statement:

GET /bank/_search{  "query": {    "bool": {      "should": [        { "match": { "address": "mill" } },        { "match": { "address": "lane" } }      ]    }  }}

Should represents one of these queries, which must return true.
The following statement, on behalf of the address, cannot contain either mill or lane:

GET /bank/_search{  "query": {    "bool": {      "must_not": [        { "match": { "address": "mill" } },        { "match": { "address": "lane" } }      ]    }  }}

Must_not requires query results to be unsatisfied for all queries
Each condition can be combined with one another, as follows:

GET /bank/_search{  "query": {    "bool": {      "must": [        { "match": { "age": "40" } }      ],      "must_not": [        { "match": { "state": "ID" } }      ]    }  }}

We can search for banalance from 20000 to 30000 through a filter.

GET /bank/_search{  "query": {    "bool": {      "must": { "match_all": {} },      "filter": {        "range": {          "balance": {            "gte": 20000,            "lte": 30000          }        }      }    }  }}

Note that the "match" in must does not support GTE and LTE.
Group, note that ES can return an array of aggressions in addition to the parameter description to group the returned array. As shown below:

GET /bank/_search{  "size": 0,  "aggs": {    "group_by_state": {      "terms": {        "field": "state.keyword"      }    }  }}

The above statement is probably equivalent to the following SQL:

SELECTCOUNTFROMGROUPBYORDERBYCOUNTDESC

The following statement calculates the average value of the balance after the state is classified as

GET /bank/_search{  "size": 0,  "aggs": {    "group_by_state": {      "terms": {        "field": "state.keyword"      },      "aggs": {        "average_balance": {          "avg": {            "field": "balance"          }        }      }    }  }}

Note that we used two times Aggs, and note that when we need to manipulate the results, we can use Aggs nesting to extract the required data from the return value.
Here is an example of a demo Aggs nesting:

GET /bank/_search{  "size": 0,  "aggs": {    "group_by_age": {      "range": {        "field": "age",        "ranges": [          {            "from": 20,            "to": 30          },          {            "from": 30,            "to": 40          },          {            "from": 40,            "to": 50          }        ]      },      "aggs": {        "group_by_gender": {          "terms": {            "field": "gender.keyword"          },          "aggs": {            "average_balance": {              "avg": {                "field": "balance"              }            }          }        }      }    }  }}

The purpose of this line of statement is to first group according to the age segment, in accordance with the gender group, and finally take the average of balance. The return value is as follows:

{"Took": 8, "timed_out": false, "_shards": {"Total": 5, "successful": 5, "failed": 0}, "hits": {"Tota          L ": 999," Max_score ": 0," hits ": []}," aggregations ": {" Group_by_age ": {" buckets ": [{            "Key": "20.0-30.0", "from": +, "to": "," Doc_count ":", "Group_by_gender": {                "Doc_count_error_upper_bound": 0, "Sum_other_doc_count": 0, "buckets": [{ "Key": "M", "Doc_count": 231, "average_balance": {"value": 27400.                982683982686}}, {"Key": "F", "Doc_count": 219,            "Average_balance": {"value": 25341.260273972603}} }}, {"Key": "30.0-40.0", "from": +, "to": +, "Doc_count" : 504, "group_bY_gender ": {" Doc_count_error_upper_bound ": 0," Sum_other_doc_count ": 0," buckets ": [                  {"Key": "F", "Doc_count": 253, "average_balance": { "Value": 25670.869565217392}}, {"Key": "M", "              Doc_count ": 251," average_balance ": {" value ": 24288.239043824702}          }]}}, {"Key": "40.0-50.0", "from": +, "to": 50, "Doc_count": "Group_by_gender": {"Doc_count_error_upper_bound": 0, "sum_other_d                Oc_count ": 0," buckets ": [{" Key ":" M "," Doc_count ": 24,                "Average_balance": {"value": 26474.958333333332}}, { "Key": "F",               "Doc_count": +, "average_balance": {"value": 27992.571428571428 }              }            ]          }        }      ]    }  }}
Springboot Integrated Elasticsearch

Since Springboot is using Spring-data-elasticsearch, the current maximum version of the ES version does not reach 5, so we tested it with a lower ES version. The ES version used is 2.3.2, The corresponding Spring-data-elasticsearch version is 2.1.0,spring-boot version with 1.5.1,springboot-starter-elasticsearch version of 1.5.1.RELEASE

    • Pom.xml
        <dependency>            <groupId>org.springframework.boot</groupId>            <artifactId>spring-boot-starter-data-elasticsearch</artifactId>            <version>1.5.1.RELEASE</version>        </dependency>
    • Application.properties
# ESspring.data.elasticsearch.repositories.enabled = truespring.data.elasticsearch.cluster-nodes = 127.0.0.1:9300
    • Entity Class (account)

It is important to note that Indexname,type cannot have uppercase. Otherwise it will be an error.

@Document(indexName = "bank",type = "account")public class Account implements Serializable{    @Id    private Long id;    private Integer account_number;    private Long balance;    private String firstname;    private String lastname;    private Integer age;    private String gender;    private String address;    private String employer;    private String email;    private String city;    private String state;        //  get&set}
    • Operation of the Repository es

Very simple only need to inherit.

publicinterfaceextends ElasticsearchRepository<Account,Long> {}
    • Service

It is important to note that when saving, ES is created manually for us when the index of the document does not match, and the ID is manually specified when the document is saved, otherwise ES will be null as the document ID.

@Service Public classAccountserviceesimpl {@AutowiredAccountrepository accountrepository;/*** Save Account     */     PublicLongSave(Account account) {Account acountsaved = accountrepository.Save(account);returnAcountsaved.getId(); }/*** Base site value filtering* @return     */     PublicList<account>querybyaddress() {//Filter by address valuepageable page =New pagerequest(0,Ten); Boolquerybuilder QueryBuilder = querybuilders.Boolquery(); QueryBuilder.must(Querybuilders.Matchquery("Address","Beijing")); SearchQuery query =New Nativesearchquerybuilder().Withquery(QueryBuilder).withpageable(page).Build(); page<account> pages = accountrepository.Search(query);returnPages.getcontent(); }}

Primary knowledge elasticsearch_2 (query and Integration Springboot)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.