Skip to content

Commit

Permalink
solr查询功能
Browse files Browse the repository at this point in the history
  • Loading branch information
sdksdk0 committed Oct 8, 2016
1 parent ac5fb8e commit 3f9b7e5
Show file tree
Hide file tree
Showing 20 changed files with 335 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/META-INF
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#Generated by Maven Integration for Eclipse
#Fri Oct 07 11:58:25 CST 2016
#Sat Oct 08 10:04:24 CST 2016
version=0.0.1-SNAPSHOT
groupId=cn.tf.taotao
m2e.projectName=taotao-manager-web
Expand Down
1 change: 1 addition & 0 deletions taotao-portal/src/main/webapp/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/META-INF
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#Generated by Maven Integration for Eclipse
#Fri Oct 07 11:58:25 CST 2016
#Sat Oct 08 10:04:31 CST 2016
version=0.0.1-SNAPSHOT
groupId=cn.tf.taotao
m2e.projectName=taotao-portal
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#Generated by Maven Integration for Eclipse
#Fri Oct 07 20:48:19 CST 2016
#Sat Oct 08 10:46:11 CST 2016
version=0.0.1-SNAPSHOT
groupId=cn.tf.taotao
m2e.projectName=taotao-rest
Expand Down
26 changes: 26 additions & 0 deletions taotao-rest/src/test/java/cn/tf/taotao/SolrJTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@

import java.io.IOException;

import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServer;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.HttpSolrServer;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrDocumentList;
import org.apache.solr.common.SolrInputDocument;
import org.junit.Test;

Expand Down Expand Up @@ -36,6 +40,28 @@ public void delete() throws SolrServerException, IOException{
solrServer.commit();
}

@Test
public void queryDocument() throws SolrServerException {
SolrServer solrServer=new HttpSolrServer("http://115.28.16.234:8090/solr");
SolrQuery query=new SolrQuery();
query.setQuery("*:*");

query.setStart(20);
query.setRows(50);

QueryResponse response=solrServer.query(query);
SolrDocumentList solrDocumentList=response.getResults();

System.out.println("共查询到记录数:"+solrDocumentList.getNumFound());
for (SolrDocument solrDocument : solrDocumentList) {
System.out.println(solrDocument.get("id"));
System.out.println(solrDocument.get("item_title"));
System.out.println(solrDocument.get("item_price"));
}



}



Expand Down
22 changes: 22 additions & 0 deletions taotao-search/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -82,5 +82,27 @@
</configuration>
</plugin>
</plugins>


<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>


</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package cn.tf.taotao.search.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.tf.taotao.common.utils.ExceptionUtil;
import cn.tf.taotao.common.utils.TaotaoResult;
import cn.tf.taotao.search.service.ItemService;

@Controller
@RequestMapping("/manager")
public class ItemController {

@Autowired
private ItemService itemService;

/**
* 导入商品数据库到索引库
*/
@RequestMapping("/importAll")
@ResponseBody
public TaotaoResult importAllItems() {
TaotaoResult result = itemService.importAllItems();
return result;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package cn.tf.taotao.search.controller;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.tf.taotao.common.utils.ExceptionUtil;
import cn.tf.taotao.common.utils.TaotaoResult;
import cn.tf.taotao.search.pojo.SearchResult;
import cn.tf.taotao.search.service.SearchService;


//商品查询
@Controller
public class SearchController {

@Autowired
private SearchService searchService;

@RequestMapping(value="/query",method=RequestMethod.GET)
@ResponseBody
public TaotaoResult search(@RequestParam("q") String queryString,
@RequestParam(defaultValue="1")Integer page,
@RequestParam(defaultValue="40")Integer rows){

if(StringUtils.isBlank(queryString)){
return TaotaoResult.build(400, "查询条件不能为空");
}

SearchResult searchResult=null;
try {
queryString=new String(queryString.getBytes("iso8859-1"),"utf-8");

searchResult = searchService.search(queryString, page, rows);
} catch (Exception e) {
e.printStackTrace();
return TaotaoResult.build(500, ExceptionUtil.getStackTrace(e));
}
return TaotaoResult.ok(searchResult);
}

}
12 changes: 12 additions & 0 deletions taotao-search/src/main/java/cn/tf/taotao/search/dao/SearchDao.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package cn.tf.taotao.search.dao;

import org.apache.solr.client.solrj.SolrQuery;

import cn.tf.taotao.search.pojo.SearchResult;

public interface SearchDao {

SearchResult searchItem(SolrQuery solrQuery) throws Exception;


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package cn.tf.taotao.search.dao.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServer;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrDocumentList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import cn.tf.taotao.search.dao.SearchDao;
import cn.tf.taotao.search.pojo.Item;
import cn.tf.taotao.search.pojo.SearchResult;

//商品搜索dao
@Repository
public class SearchDaoImpl implements SearchDao {


@Autowired
private SolrServer solrServer;


@Override
public SearchResult searchItem(SolrQuery query) throws Exception {

SearchResult result=new SearchResult();
//根据查询条件查询索引库
QueryResponse queryResponse=solrServer.query(query);
SolrDocumentList solrDocumentList=queryResponse.getResults();
result.setRecordCount(solrDocumentList.getNumFound());
List<Item> itemList=new ArrayList<>();

//高亮显示
Map<String, Map<String,List<String>>> highlight=queryResponse.getHighlighting();

for (SolrDocument solrDocument : solrDocumentList) {
Item item=new Item();
item.setId((String) solrDocument.get("id"));

List<String>list = highlight.get(solrDocument.get("id")).get("item_title");
String title = "";
if (null != list&& !list.isEmpty()) {
title = list.get(0);
} else {
title = (String) solrDocument.get("item_title");
}
item.setTitle(title);

item.setPrice((long) solrDocument.get("item_price"));
item.setSell_point((String) solrDocument.get("item_sell_point"));
item.setImage((String) solrDocument.get("item_image"));
item.setCategory_name((String) solrDocument.get("item_category_name"));

itemList.add(item);

}
result.setItemList(itemList);
return result;

}



}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@

public interface ItemMapper {

List<Item> searchItemList();
List<Item> getItemList();

}
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="cn.tf.taotao.search.mapper.ItemMapper">
<select id="searchItemList" resultType="cn.tf.taotao.search.pojo.Item">
SELECT
<select id="getItemList" resultType="cn.tf.taotao.search.pojo.Item">
SELECT
a.id,
a.title,
a.sell_point,
a.price,
a.image,
b.name category_name,
c.item_desc
FROM tb_item a LEFT JOIN tb_item_cat b ON a.cid=b.id LEFT JOIN tb_item_desc c ON a.id=c.item_id
FROM tb_item a
LEFT JOIN tb_item_cat b ON a.cid=b.id
LEFT JOIN tb_item_desc c ON a.id=c.item_id

</select>
</mapper>
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package cn.tf.taotao.search.pojo;

import java.util.List;

public class SearchResult {

private Long recordCount; //总记录数
private List<Item>itemList; //商品列表
private long pageCount; //分页总数
private long curPage; //当前页
public Long getRecordCount() {
return recordCount;
}
public void setRecordCount(Long recordCount) {
this.recordCount = recordCount;
}
public List<Item> getItemList() {
return itemList;
}
public void setItemList(List<Item> itemList) {
this.itemList = itemList;
}
public long getPageCount() {
return pageCount;
}
public void setPageCount(long pageCount) {
this.pageCount = pageCount;
}
public long getCurPage() {
return curPage;
}
public void setCurPage(long curPage) {
this.curPage = curPage;
}




}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@

public interface ItemService {

TaotaoResult importAllItems() throws Exception;
TaotaoResult importAllItems();

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package cn.tf.taotao.search.service;

import org.apache.solr.client.solrj.SolrQuery;

import cn.tf.taotao.search.pojo.SearchResult;

public interface SearchService {

SearchResult search(String queryString,int page,int rows) throws Exception;
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package cn.tf.taotao.search.service;
package cn.tf.taotao.search.service.impl;

import java.io.IOException;
import java.util.List;
Expand All @@ -7,12 +7,15 @@
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.common.SolrInputDocument;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.tf.taotao.common.utils.ExceptionUtil;
import cn.tf.taotao.common.utils.TaotaoResult;
import cn.tf.taotao.search.mapper.ItemMapper;
import cn.tf.taotao.search.pojo.Item;
import cn.tf.taotao.search.service.ItemService;

@Service
public class ItemServiceImpl implements ItemService{

@Autowired
Expand All @@ -22,10 +25,11 @@ public class ItemServiceImpl implements ItemService{
private SolrServer solrServer;

@Override
public TaotaoResult importAllItems() throws SolrServerException, IOException {
List<Item> list=itemMapper.searchItemList();
public TaotaoResult importAllItems() {

try {
List<Item> list=itemMapper.getItemList();

for (Item item : list) {
SolrInputDocument document=new SolrInputDocument();
document.setField("id", item.getId());
Expand Down
Loading

0 comments on commit 3f9b7e5

Please sign in to comment.