Highlight
The key words entered by the user are displayed in red font in the title, which is the highlight commonly used in search.
Back end code:
private Map searchList(Map searchMap){//Created condition, used for query. Map map=new HashMap();//Create a Map object to return highlighted data HighlightQuery query=new SimpleHighlightQuery(); //Set the highlighted field, and multiple fields can be added. HighlightOptions highlightOptions=new HighlightOptions().addField("item_title"); highlightOptions.setSimplePrefix("<em style='color:red'>");//Highlight prefix highlightOptions.setSimplePostfix("</em>");//Highlight suffix query.setHighlightOptions(highlightOptions);//Set highlight options //Query by keyword Criteria criteria=new Criteria("item_keywords").is(searchMap.get("keywords")); query.addCriteria(criteria);//Add query criteria HighlightPage<TbItem> page = solrTemplate.queryForHighlightPage(query, TbItem.class);//query for(HighlightEntry<TbItem> h: page.getHighlighted()){//Cycle highlight entry set TbItem item = h.getEntity();//Get the original entity class without highlighting if(h.getHighlights().size()>0 && h.getHighlights().get(0).getSnipplets().size()>0){ item.setTitle(h.getHighlights().get(0).getSnipplets().get(0));//Set highlighted results } } map.put("rows",page.getContent()); return map; }
Note:
1.h.getHighlights() is the reason for the collection: there may be multiple domains
new HighlightOptions().addField("item_title").addField("item_price")...;
2. H. gethighlights(). Get (0). Getsniplets() is the reason for the collection: there may be multiple values, such as searching Samsung mobile phones, and Samsung and mobile phones represent two words to search from the title.
3. The back end passes the html tag to the front-end page, but the front-end page does not parse, but displays it as a string. Because the security mechanism of angularJS solves the following problems in order to avoid html injection attack:
Add filter and $sce service to base.js of front end
app.filter('trustHtml',['$sce',function($sce){//Write a filter named trustHtml to introduce services return function(data){ return $sce.trustAsHtml(data); } }]);
In the page: ng bind html is used to display the content of HTML
<div class="attr" ng-bind-html="item.title | trustHtml"></div>//item.title is the data in the filter
Group query
private List searchCategoryList(Map searchMap){ List<String> list=new ArrayList(); Query query=new SimpleQuery(); //Query by keyword Criteria criteria=new Criteria("item_keywords").is(searchMap.get("keywords")); query.addCriteria(criteria); //Set grouping options GroupOptions groupOptions=new GroupOptions().addGroupByField("item_category"); query.setGroupOptions(groupOptions); //Get group page GroupPage<TbItem> page = solrTemplate.queryForGroupPage(query, TbItem.class); //Group result set by column GroupResult<TbItem> groupResult = page.getGroupResult("item_category"); //Get group result entry page Page<GroupEntry<TbItem>> groupEntries = groupResult.getGroupEntries(); //Get group entry set List<GroupEntry<TbItem>> content = groupEntries.getContent(); for(GroupEntry<TbItem> entry:content){ list.add(entry.getGroupValue());//Encapsulate the name of the grouping result into the return value } return list; }
Filtering queries
... Criteria filterCriteria=new Criteria("item_category").is(searchMap.get("category")); FilterQuery filterQuery=new SimpleFilterQuery(filterCriteria); query.addFilterQuery(filterQuery); ...