package com.baidu.search.service.impl;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.solr.core.SolrTemplate;
import org.springframework.data.solr.core.query.Criteria;
import org.springframework.data.solr.core.query.FilterQuery;
import org.springframework.data.solr.core.query.GroupOptions;
import org.springframework.data.solr.core.query.HighlightOptions;
import org.springframework.data.solr.core.query.HighlightQuery;
import org.springframework.data.solr.core.query.Query;
import org.springframework.data.solr.core.query.SimpleFilterQuery;
import org.springframework.data.solr.core.query.SimpleHighlightQuery;
import org.springframework.data.solr.core.query.SimpleQuery;
import org.springframework.data.solr.core.query.result.GroupEntry;
import org.springframework.data.solr.core.query.result.GroupPage;
import org.springframework.data.solr.core.query.result.GroupResult;
import org.springframework.data.solr.core.query.result.HighlightEntry;
import org.springframework.data.solr.core.query.result.HighlightEntry.Highlight;
import org.springframework.data.solr.core.query.result.HighlightPage;
import org.springframework.data.solr.core.query.result.ScoredPage;
import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.fastjson.JSON;
import com.baidu.pojo.TbItem;
import com.baidu.pojo.TbSpecificationOption;
import com.baidu.search.service.ItemSearchService;
@Service
public class ItemSearchServiceImpl implements ItemSearchService {
@Autowired
private SolrTemplate solrTemplate;
@Autowired
private RedisTemplate redisTemplate;
private Long totalElements;
private int totalPages;
@Override
public Map searchMap(Map<String, String> map) {
if (map != null && map.get("keywords") != null && !map.get("keywords").trim().equals("")) {
Map resultMap = new HashMap();
List highlightList = searchHighlight(map);
resultMap.put("rows", highlightList);
List categoryList = searchCategory(map);
resultMap.put("categoryList", categoryList);
if (categoryList != null && categoryList.size() > 0) {
Long typeId = (Long) redisTemplate.boundHashOps("itemCatList").get(categoryList.get(0));
List brandList = searchBrandList(typeId);
resultMap.put("brandList", brandList);
List specList = searchSpecList(typeId);
resultMap.put("specList", specList);
resultMap.put("totalElements", totalElements);
resultMap.put("totalPages", totalPages);
System.out.println("totalElements" + totalElements);
System.out.println("totalPages" + totalPages);
}
return resultMap;
}
return null;
}
private List searchCategory(Map<String, String> map) {
List list = new ArrayList();
Query query = new SimpleQuery("*:*");
FilterQuery filterQuery = addFilterQuery(map);
query.addFilterQuery(filterQuery);
Criteria criteria = new Criteria("item_keywords").contains(map.get("keywords"));
GroupOptions options = new GroupOptions().addGroupByField("item_category");
query.setGroupOptions(options);
GroupPage<TbItem> groupPage = solrTemplate.queryForGroupPage(query, TbItem.class);
GroupResult<TbItem> groupResult = groupPage.getGroupResult("item_category");
Page<GroupEntry<TbItem>> groupEntries = groupResult.getGroupEntries();
for (GroupEntry<TbItem> entry : groupEntries) {
String groupValue = entry.getGroupValue();
if (groupValue != null && !groupValue.equals("")) {
list.add(groupValue);
}
}
return list;
}
private List searchHighlight(Map<String, String> map) {
Map highlightMap = new HashMap();
HighlightQuery query = new SimpleHighlightQuery();
query = addScoredPage(query, map);
query = addSort(query, map);
FilterQuery filterQuery = addFilterQuery(map);
query.addFilterQuery(filterQuery);
HighlightOptions options = new HighlightOptions().addField("item_title");
options.setSimplePrefix("");
options.setSimplePostfix("");
query.setHighlightOptions(options);
Criteria criteria = new Criteria("item_keywords").contains(map.get("keywords"));
query.addCriteria(criteria);
HighlightPage<TbItem> highlightPage = solrTemplate.queryForHighlightPage(query, TbItem.class);
List<HighlightEntry<TbItem>> highlighted = highlightPage.getHighlighted();
for (HighlightEntry<TbItem> highlightEntry : highlighted) {
TbItem entity = highlightEntry.getEntity();
List<Highlight> highlights = highlightEntry.getHighlights();
if (highlights != null && highlights.size() > 0) {
Highlight highlight = highlights.get(0);
if (highlight.getSnipplets() != null && highlight.getSnipplets().size() > 0) {
String title = highlight.getSnipplets().get(0);
System.out.println(title);
entity.setTitle(title);
}
}
}
totalElements = highlightPage.getTotalElements();
totalPages = highlightPage.getTotalPages();
return highlightPage.getContent();
}
private FilterQuery addFilterQuery(Map map) {
FilterQuery query = new SimpleQuery();
String category = (String) map.get("category");
if (category != null && category.trim().length() > 0) {
Criteria criteria = new Criteria("item_category").contains(category);
query.addCriteria(criteria);
}
String brand = (String) map.get("brand");
if (brand != null && brand.trim().length() > 0) {
Criteria criteria = new Criteria("item_brand").contains(brand);
query.addCriteria(criteria);
}
Map<String, String> specMap = (Map) map.get("spec");
if (specMap != null && specMap.size() > 0) {
for (String key : specMap.keySet()) {
Criteria criteria = new Criteria("item_spec_" + key).contains(specMap.get(key));
query.addCriteria(criteria);
}
}
String price_str = (String) map.get("price");
if (price_str != null && price_str.trim().length() > 0) {
String[] strs = price_str.split("-");
if (!strs[0].equals("0")) {
Criteria criteria = new Criteria("item_price").greaterThan(strs[0]);
query.addCriteria(criteria);
}
if (!strs[1].equals("*")) {
Criteria criteria = new Criteria("item_price").lessThan(strs[1]);
query.addCriteria(criteria);
}
}
return query;
}
private <T> T addScoredPage(T t, Map map) {
Integer currentPage = Integer.parseInt((String) map.get("currentPage"));
Integer size = Integer.parseInt((String) map.get("size"));
if (t instanceof SimpleQuery) {
SimpleQuery query = (SimpleQuery) t;
if (currentPage == -1) {
currentPage = 1;
}
if (size == -1) {
size = 10;
}
query.setOffset((currentPage - 1) * size);
query.setRows(size);
}
if (t instanceof SimpleHighlightQuery) {
SimpleHighlightQuery query = (SimpleHighlightQuery) t;
if (currentPage == -1) {
currentPage = 1;
}
if (size == -1) {
size = 10;
}
query.setOffset((currentPage - 1) * size);
query.setRows(size);
}
return t;
}
private <T> T addSort(T t, Map<String, String> map) {
String sortField = map.get("sortField");
String sortType = map.get("sortType");
Direction asc = Sort.Direction.DESC;
if ("ASC".equals(sortType)) {
asc = Sort.Direction.ASC;
}
if (t instanceof SimpleQuery) {
SimpleQuery query = (SimpleQuery) t;
Sort sort = new Sort(asc, "item_" + sortField);
query.addSort(sort);
}
if (t instanceof SimpleHighlightQuery) {
SimpleHighlightQuery query = (SimpleHighlightQuery) t;
Sort sort = new Sort(asc, "item_" + sortField);
query.addSort(sort);
}
return t;
}
private List searchBrandList(Long templateId) {
List<Map> brandList = (List<Map>) redisTemplate.boundHashOps("brandList").get(templateId);
return brandList;
}
private List searchSpecList(Long templateId) {
List specList = (List) redisTemplate.boundHashOps("specList").get(templateId);
System.out.println(specList);
return specList;
}
}