Mixed query
# What is a mixed query?
A simple understanding is that half of it adopts the syntax of EE, and half of it adopts the syntax of RestHighLevelClient, which is similar to "Hybrid". I believe you will fall in love with this "Hybrid" mode, because it combines the advantages of both modes!
# Why have hybrid queries?
Because EE has not yet achieved 100% coverage of the functions of RestHighLevelClient, at the initial stage of open source, only about 90% of the functions of RestHighLevelClient and 99% of the core high-frequency use functions are covered. EE cannot meet a certain special requirement. At this time, the secondary development of the EE framework or the requirement is directly submitted to the EE author, which cannot meet the developer's needs in terms of time. Some requirements may be tighter than the product manager. , you can solve the dilemma by mixing queries.
# What are the advantages of using hybrid query vs using native query directly?
The biggest advantage of hybrid vehicles compared to pure oil vehicles is fuel saving. In fact, the same is true for hybrid queries. It can save a lot of code than using native queries directly, and is simpler than using native queries directly. It combines the flexibility of native syntax and EE's Low-code advantage.
# How to use hybrid query?
When I did not provide this document, although I provided an API and a brief introduction to the mixed query, many people still do not know this function, let alone how to use it, so here I will use a specific case to demonstrate to you How to use mixed query is for your reference, please don't worry about the space, it's actually very, very simple, just the details of my tutorial.
background
User "Xiangyang" WeChat feedback to me that currently EE does not support query sorting according to the position from the given point from near to far. In actual development, this scenario can be applied to "the passenger places an order and asks to give priority to the driver closest to me within 3 kilometers" when taking a taxi, and then the passenger is a beautiful woman, worried about her own safety, and added a few more requirements. , For example, the driver must be a female, the driving experience is more than 3 years, and the commercial vehicle, etc...
Taking the taxi-hailing scenario above as an example, let's see how to use EE to query? The above query can be divided into two parts
- General query supported by EE: if the driver is female within 3 kilometers, and the driving age is >= 3 years...
- Unconventional queries not supported by EE: sorting by complex collation (not supported at the time of writing this document, but it is now supported, but this is not important, this article only demonstrates the use of hybrid queries)
For the supported part, we can directly call EE, and EE builds a SearchSourceBuilder first.
// Assume the latitude and longitude of the passenger's location is 31.256224D, 121.462311D
LambdaEsQueryWrapper<Driver> wrapper = new LambdaEsQueryWrapper<>();
wrapper.geoDistance(Driver::getLocation, 3.0, DistanceUnit.KILOMETERS, new GeoPoint(31.256224D, 121.462311D))
.eq(Driver::getGender, "female")
.ge(Driver::getDriverAge,3)
.eq(Driver::getCarModel,"Business Car");
SearchSourceBuilder searchSourceBuilder = driverMapper.getSearchSourceBuilder(wrapper);
2
3
4
5
6
7
For unsupported statements, you can continue to encapsulate them with the syntax of RestHighLevelClient. After encapsulation, you can directly call the native query interface provided by EE to complete the entire query.
SearchRequest searchRequest = new SearchRequest("index name");
// The searchSourceBuilder here is constructed from the above EE, and we continue to append sorting parameters to it
searchSourceBuilder.sort(
new GeoDistanceSortBuilder("location", 31.256224D, 121.462311D)
.order(SortOrder.DESC)
.unit(DistanceUnit.KILOMETERS)
.geoDistance(GeoDistance.ARC)
);
searchRequest.source(searchSourceBuilder);
SearchResponse searchResponse = driverMapper.search(searchRequest, RequestOptions.DEFAULT);
2
3
4
5
6
7
8
9
10
In this way, you can not only enjoy the basic query that EE helps you to generate, but also complete the functions that EE does not currently support. You only need a lot of code (compared to direct RestHighLevelClient, it can still save a lot of code) to achieve your The goal is similar to a compromise solution under the condition that pure electric vehicles have not yet fully developed, namely gasoline-electric hybrid.
Of course, if you are not used to using this mode, you can still use the native query directly, so you can use EE without any worries. We have already figured out various solutions and fallbacks for you, and after-sales is worry-free! If you also agree with this model, you may wish to give the author a thumbs up. In order to make EE users happy, the author's bad old man can be described as painstaking!
Regarding the hybrid query, there is another usage, which also saves you a lot of code. In some special scenarios, the API provided by us cannot meet your query needs. In this case, you can construct the SearchSourceBuilder yourself, and then call the EE API to query, which can help you save the code related to data parsing.
Example of use:
@Test
public void testSetSearchSourceBuilder(){
// Another way to test hybrid queries
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
searchSourceBuilder.query(QueryBuilders.matchQuery(FieldUtils.val(Document::getCreator),"Old man"));
LambdaEsQueryWrapper<Document> wrapper=new LambdaEsQueryWrapper<>();
wrapper.setSearchSourceBuilder(searchSourceBuilder);
List<Document> documents = documentMapper.selectList(wrapper);
System.out.println(documents);
}
2
3
4
5
6
7
8
9
10
# Conclusion
Because the functions supported by RestHighLevel officially provided by ES are too numerous, although I am still integrating various new functions non-stop, repairing user feedback problems, and optimizing the performance of existing code, there will inevitably be some The function can not meet your current needs, please forgive me, my lords, EE has only been born for three months, it is impossible to be perfect, please give us a little time, these so-called shortcomings will be solved, just like new energy vehicles will eventually be in the future. Gradually replace the fuel car, those so-called problems will not be a problem in the future, Ullah!