Chapter Content
Article Content Search Ideas
Search for content participle
Search query Statements
Filter criteria
pagination, sorting criteria
Summary
First, the article content search ideas
The previous article on how to integrate ES 5 on the Spring Boot 2.0, this article talk about specific combat. Simply talk about how to achieve the article, question and answer these content search concrete implementation. The idea of implementation is simple:
Set the minimum matching weight value based on phrase match
Where to get the phrase, using the IK word breaker participle
Implementing filtering based on Fiter
Pagination sorting based on pageable
It is easy to search for something that is not satisfactory, if you call it directly. Because content search is concerned with the connectivity of content. So here the processing method is relatively low, hope that more exchanges together to achieve a better search method. is to get a lot of phrases through word segmentation, and then use phrases to accurately match phrases.
ES Installing the IK word breaker plugin is simple. The first step is to download the corresponding version of Https://github.com/medcl/elasticsearch-analysis-ik/releases. The second step, in the Elasticsearch-5.5.3/plugins directory, create a new folder IK, the elasticsearch-analysis-ik-5.5.3.zip extracted files copied to elasticsearch-5.1.1 The/plugins/ik directory. Finally, restart ES.
Second, the search content participle
Install IK, how do I call it?
The first step, I search this side of the content will be joined by commas. So we'll split the comma first.
The second step is to add yourself to the search term, because some words are gone after the IK participle ... This is a bug.
Third, use the Analyzerequestbuilder object to get a list of return value objects after IK participle
The fourth step, the optimization of the word segmentation results, such as all words, then retain all the words have words, then retain the words;
The core implementation code is as follows:
/*** Search for content participle*/ protectedList<string>handlingsearchcontent (String searchcontent) {List<String> searchtermresultlist =NewArraylist<>(); //split by comma to get a list of search termsList<string> searchtermlist =arrays.aslist (Searchcontent.split (searchconstant.string_token_split)); //If the search term is greater than 1 words, the word-breaker result list is obtained through the IK word breakerSearchtermlist.foreach (Searchterm, { //the search term TAG itself adds a list of search terms and solves the question of wouldSearchtermresultlist.add (searchterm); //get the search term IK participle listSearchtermresultlist.addall (Getikanalyzesearchterms (searchterm)); }); returnsearchtermresultlist; } /*** Call ES for results after IK participle*/ protectedList<string>getikanalyzesearchterms (String searchcontent) {analyzerequestbuilder ikrequest=NewAnalyzerequestbuilder (Elasticsearchtemplate.getclient (), Analyzeaction.instance, searchconstant.i Ndex_name, searchcontent); Ikrequest.settokenizer (Searchconstant.tokenizer_ik_max); List<AnalyzeResponse.AnalyzeToken> iktokenlist =Ikrequest.execute (). Actionget (). Gettokens (); //Cyclic AssignmentList<string> searchtermlist =NewArraylist<>(); Iktokenlist.foreach (Iktoken-{Searchtermlist.add (Iktoken.getterm ()); }); returnhandlingikresultterms (searchtermlist); } /*** If Word breaker results: Shampoo (hair wash, fa shui, wash, hair, water) *-all words, reserved *-word + word, reserved words only *-all words, reserved words*/ PrivateList<string> Handlingikresultterms (list<string>searchtermlist) {Boolean Isphrase=false; Boolean Isword=false; for(String term:searchtermlist) {if(Term.length () >searchconstant.search_term_length) {Isphrase=true; } Else{Isword=true; } } if(Isword &isphrase) {List<String> phraselist =NewArraylist<>(); Searchtermlist.foreach ( term- { if(Term.length () >searchconstant.search_term_length) {Phraselist.add (term); } }); returnphraselist; } returnsearchtermlist; }
Third, search query statements
Constructs the Content enumeration object, lists the fields which need to search, the Contentsearchtermenum code is as follows:
ImportLombok. Allargsconstructor; @AllArgsConstructor Public enumContentsearchtermenum {//titleTitle ("title"), //contentContent ("Content"); /*** Search Fields*/ PrivateString name; PublicString GetName () {returnname; } Public voidsetName (String name) { This. Name =name; } }
Loop through the "phrase Search match" search field, and then set the minimum weight value to 1. The core code is as follows:
/** * Construct query conditions * /privatevoid buildmatchquery (boolquerybuilder QueryBuilder, list<string> searchtermlist) { for (String searchterm: searchtermlist) { for (contentsearchtermenum searchtermenum: Contentsearchtermenum.values ()) { querybuilder.should (querybuilders.matchphrasequery ( Searchtermenum.getname (), searchterm)); } } Querybuilder.minimumshouldmatch (Searchconstant.minimum_should_match); }
Iv.. Screening conditions
Search for things, and sometimes the need is like this. Need to search under a category, such as e-commerce needs to search for products under a certain brand. Then you need to construct some fitler to filter. The OR and and two statements that correspond to the where of the SQL statement. Use the filter method in ES to add a filter. The code is as follows:
/*** Build Filter Criteria*/ Private voidbuildfilterquery (Boolquerybuilder boolquerybuilder, Integer type, String category) {//Content Type Filtering if(Type! =NULL) {Boolquerybuilder Typefilterbuilder=Querybuilders.boolquery (); Typefilterbuilder.should (Querybuilders.matchquery (Searchconstant.type_name, TYPE). Lenient (true)); Boolquerybuilder.filter (Typefilterbuilder); } //Content Category Filtering if(!stringutils.isempty (category)) {Boolquerybuilder Categoryfilterbuilder=Querybuilders.boolquery (); Categoryfilterbuilder.should (Querybuilders.matchquery (Searchconstant.category_name, CATEGORY). Lenient (true)); Boolquerybuilder.filter (Categoryfilterbuilder); } }
Type is a large class, category is a small class, so you can support size class filtering. But what if you need to search for type = 1 or type = 2? The implementation code is simple:
Typefilterbuilder should (Querybuilders.matchquery (Searchconstant.type_name, 1) . Should (Querybuilders.matchquery (Searchconstant.type_name, 2) . Lenient(true));
With a chain expression, two should implementations or, that is, the SQL corresponding or statement. Implemented with two Boolquerybuilder, that is, the SQL corresponding and statement.
Five, pagination, sorting conditions
The pagination sort code is simple:
@Override PublicPagebean searchcontent (Contentsearchbean contentsearchbean) {Integer pagenumber=Contentsearchbean.getpagenumber (); Integer pageSize=contentsearchbean.getpagesize (); Pagebean<ContentEntity> Resultpagebean =NewPagebean<>(); Resultpagebean.setpagenumber (pagenumber); Resultpagebean.setpagesize (pageSize); //build a search phraseString searchcontent =contentsearchbean.getsearchcontent (); List<String> searchtermlist =handlingsearchcontent (searchcontent); //Build Query CriteriaBoolquerybuilder Boolquerybuilder =Querybuilders.boolquery (); Buildmatchquery (Boolquerybuilder, searchtermlist); //Build Filter Criteriabuildfilterquery (Boolquerybuilder, Contentsearchbean.gettype (), contentsearchbean.getcategory ()); //building pagination, sorting criteriapageable pageable =Pagerequest.of (pagenumber, pageSize); if(!Stringutils.isempty (Contentsearchbean.getordername ())) {pageable=Pagerequest.of (pagenumber, PageSize, Sort.Direction.DESC, Contentsearchbean.getordername ()); } SearchQuery SearchQuery=NewNativesearchquerybuilder (). withpageable (pageable). Withquery (Boolquerybuilder). build (); //SearchLogger.info ("\ n contentserviceimpl.searchcontent () [" +searchcontent+ "] \ n DSL = \ n" +searchquery.getquery (). toString ()); Page<ContentEntity> Contentpage =Contentrepository.search (searchQuery); Resultpagebean.setresult (Contentpage.getcontent ()); Resultpagebean.settotalcount ((int) contentpage.gettotalelements ()); Resultpagebean.settotalpage ((int) contentpage.gettotalelements ()/resultpagebean.getpagesize () + 1); returnResultpagebean; }
The Pageable object is used to construct paging parameters and specify corresponding sort fields, sort order (DESC ASC).
Article Source: http://mp.weixin.qq.com/s/ZoJzF9VpynUBSQWlJJjmEw
Springboot Video Tutorial: http://www.roncoo.com/course/list.html?courseName=spring+boot
Spring Boot 2.0 Integrated ES 5 article content Search combat