Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Java to realize paging, sorting and aggregation related to index database

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "how to use Java to achieve index database related paging, sorting and aggregation", the content of the article is simple and clear, easy to learn and understand, now please follow the editor's ideas slowly in depth, together to study and learn "how to use Java to achieve index database related paging, sorting and aggregation" bar!

Learn to plan and use Java code to implement various operations on the index library:

Through the custom method to achieve matching query, range query.

How is the native query code written?

Finally, there is the code related to aggregation.

I. Custom method

I learned the most basic methods of adding, deleting, changing and searching yesterday, but the query is obviously not so simple in practical application.

For example, if users want to match a query, we learned a few days ago that there are two common ways of match matching and term matching.

What are the ways to correspond to these in Java?

There are native methods that can be used, but they are troublesome. Here we use the custom methods provided by springdata.

1 self-defined method to implement multiple query methods

Yesterday I used the findAll () method, which is a built-in method. If you look at its semantics, you can know that you can actually query all the data.

But most of the time, it can not be solved by querying all the methods, so you need to customize the description yourself.

① queryItemsByTitleMatchs

The method name must be in its format, which is easy to understand as soon as you see it, and the query is matched according to the Title field.

It can also be followed by "And" and "Or" these logical operations to achieve Boolean query.

② queryItemsByPriceBetween

By the same token, range queries have their own format, that is, the use of the word between.

2 the use of custom methods

① matches according to Title entry

Directly call the custom match matching method, and state in the parameters that the keyword to be searched is "Xiaomi mobile phone", so that match can match the data related to "Xiaomi mobile phone".

② queries based on price range

Call the custom range query method directly, and indicate that the price range is 2000-4000 in the parameters, so that the relevant data of this price range can be queried.

3 running method and observation result

Matching result of ① "Xiaomi Mobile phone"

This was specifically explained a few days ago, because the field type of title is text, which requires word segmentation, so "Xiaomi mobile phone" is particified into "Xiaomi" and "mobile phone".

As long as the data related to the above participle will be queried, so the "nut phone" has also been queried.

Data for ② prices in the 2000-4000 range

The result is obvious, as shown in the figure above.

2. Native query

Although the custom method is very simple, sometimes it does not meet the business requirements well, such as when there are too many query conditions.

At this time, we need to combine the native query provided by the government.

① creates the constructor of the query

NativeSearchQueryBuilder means native search query constructor, through which various query conditions can be added.

② add query condition

WithQuery (): QueryBuilders description query

In the example, there is a match matching query, so in Java, that is, the matchQuery method, the parameter: title is the field, and "Xiaomi phone" is the data corresponding to that field.

WithPageable (): PageRequest implements paging

The of () method enables paging, where the number of pages starts at 0, and the page size specified in the above figure is 2.

WithSort (): SortBuilders implements sorting

The fieldSort () method describes the fields that need to be sorted.

The order () method describes how it is sorted.

It can be chain programming to constantly add query conditions.

③ then calls the search method to complete the query

Call the search () method using a custom itemRepository interface, while explaining the query condition, that is, the native query only describes the next query condition.

Query result test

Match matches "Xiaomi mobile phone", and there are 3 query data.

When paging, it shows that each page displays 2 pieces of data, so there are a total of 2 pages of data.

The current display is the home page, that is, page 0.

Java is an object-oriented programming language, which not only absorbs the advantages of C++ language, but also abandons the concepts of multi-inheritance and pointers which are difficult to understand in C++, so Java language has two characteristics: powerful and easy to use. Https://www.yuananren.com Java language, as the representative of static object-oriented programming language, perfectly implements the object-oriented theory and allows programmers to carry out complex programming in an elegant way of thinking. Java has the characteristics of simplicity, object-oriented, distributed, robust, security, platform independence and portability, multithreading, dynamic and so on. Java can write desktop applications, Web applications, distributed systems, embedded system applications and so on.

In short: after customizing the method, we do not need to write how to query, we can use it directly, but the method name needs to follow the naming convention, if it can not be used normally, it is very likely that the method name is in the wrong format.

III. Primary polymerization

Let's start with a brief review of aggregation:

In fact, it is nothing more than the core four steps:

Aggs indicates that this is an aggregation.

Customize a name for the aggregation.

Explain the aggregation type: buckets have their own corresponding types, and metrics have their own corresponding types. In the above example, it is the terms type in the bucket.

Description aggregation field: brand. That is, it is divided into multiple buckets according to the brand field.

Well, this is used in Elasticsearch, so how do you manipulate it with Java code?

① creates a constructor

This is the same as the native query, using this constructor.

② add aggregation

This code clarifies the three elements of aggregation.

What's the name of the aggregation? His name is popularBrand.

What is the type of aggregation? The terms () method is the terms type, and different types correspond to different methods in Java.

Which is the aggregate field? The field description is the brand field,

The aggregation result is obtained by ③.

Using elasticsearchTemplate query aggregation, the aggregate result is returned.

Analysis of ④ results

This code is a little complicated, let's make a comparison.

Using kibana to respond to a json data, this code in Java is actually the parsing of json data.

GetAggregations (), aggregates can be nested, but only one aggregate is written in the example.

Get (): finds the desired aggregation based on the aggregate name.

GetBuckets (): find the corresponding bucket data. There is some other data in json data, and what we need naturally is buckets data.

Get the corresponding data in the bucket.

To put it bluntly, the parsing of aggregation by Java is actually the parsing process of json data. Even if it is not aggregation, the parsing ideas of other json data are the same.

Thank you for your reading, the above is "how to use Java to achieve index database-related paging, sorting and aggregation" content, after the study of this article, I believe you on how to use Java to achieve index database-related paging, sorting and aggregation of this problem has a more profound experience, the specific use also needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report