1.在pyg_parent父模块下新建了3个与搜索相关的子模块
满足了项目分布式的开发
pyg_search_web模块的pom文件中:
<packaging>war</packaging>
<dependencies>
<dependency>
<groupId>com.fighting</groupId>
<artifactId>pyg_search_interface</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<!--解决了日志警告问题,web层不能获取到pojo中的对象,因为pyg_search_interface层没有添加pojo
的坐标,而是在pgy_search_service中添加的,只能间接获取到,直接在下面添加pojo的坐标就不会出现警告了-->
<dependency>
<groupId>com.fighting</groupId>
<artifactId>pyg_pojo</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
<!-- 指定端口 -->
<port>9104</port>
<!-- 请求路径 -->
<path>/</path>
</configuration>
</plugin>
</plugins>
</build>
在resources的spring目录下导入springmvc.xml配置文件,并在webapp下引入静态資源
pyg_search_service的pom文件中:
<packaging>war</packaging>
<dependencies>
<dependency>
<groupId>com.fighting</groupId>
<artifactId>pyg_search_interface</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.fighting</groupId>
<artifactId>pyg_pojo</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
<!-- 指定端口 -->
<port>9004</port>
<!-- 请求路径 -->
<path>/</path>
</configuration>
</plugin>
</plugins>
</build>
在resources目录下的spring目录下导入配置文件applicationContext-service.xml和applicationContext-solr.xml
2.在本机的Tomcat上部署了solr
首先要将Tomcat的端口号改为9100
在本机的windows系统上的Tomcat上部署了solr-4.10.3.war,然后改名为solr.war
将这些jar包到tomcat中的solr的\WEB-INF\lib目录下
在E盘上创建solrhome(solr的数据存储位置,相当于solr的数据库)文件夹
注意,以上配置改完之后,需要重启Tomcat服务器
在浏览器上访问http://localhost:9100/solr,若成功,则出现一下界面:
3.中文分析器IK Analyzer的安装
将中文分词器IKAnalyzer.jar的jar包拷贝到\WEB-INF\lib目录下
\WEB-INF创建classes目录,并拷贝mydict.dic、IKAnalyzer.cfg.xml、ext_stopword.dic
在solrhome\collection1\conf修改schema.xml(在配置文件的最下方)加入配置
<fieldType name="text_ik" class="solr.TextField">
<analyzer class="org.wltea.analyzer.lucene.IKAnalyzer"/>
</fieldType>
测试http://localhost:9090/solr/#/collection1/analysis将FieldType改成text_ik查看效果
4.域配置
在本地solrhome目录的collection1的conf下的schema.xml中增加3种域
普通域:这种域一般用来去solr库中存储数据
//name:字段名称 type:字段类型 stored:是否存储分词前内容(复制域选择false) required:是否必填 indexed:索引(是否进行查询)
<field name="item_goodsid" type="long" indexed="true" stored="true"/>
<field name="item_title" type="text_ik" indexed="true" stored="true"/>
<field name="item_price" type="double" indexed="true" stored="true"/>
<field name="item_image" type="string" indexed="false" stored="true" />
<field name="item_category" type="string" indexed="true" stored="true" />
<field name="item_seller" type="text_ik" indexed="true" stored="true" />
<field name="item_brand" type="string" indexed="true" stored="true" />
复制域:不占用存储空间,只是逻辑上等于其他几个域的总和(复制域一般用来做搜索查询)
//multiValued 是否有多值 stored="false" 不需要存储
<field name="item_keywords" type="text_ik" indexed="true" stored="false" multiValued="true"/>
<copyField source="item_title" dest="item_keywords"/>
<copyField source="item_category" dest="item_keywords"/>
<copyField source="item_seller" dest="item_keywords"/>
<copyField source="item_brand" dest="item_keywords"/>
动态域:
//因为数据中的item_spec_后面的内容不固定所以用*号代替
<dynamicField name="item_spec_*" type="string" indexed="true" stored="true" />
//配置好各种域信息,需要重新启动tomcat
5.向solr种批量导入数据
在pyg_parent父模块下新建一个子模块pyg_solr_util
pyg_solr_util的pom文件:
<dependencies>
<dependency>
<groupId>com.fighting</groupId>
<artifactId>pyg_dao</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
applicationContext-solr.xml中配置:
<context:component-scan base-package="com.pinyougou.solrutil"></context:component-scan>
<!-- solr服务器地址 -->
<solr:solr-server id="solrServer" url="http://127.0.0.1:9100/solr" />
<!-- solr模板,使用solr模板可对索引库进行CRUD的操作 -->
<bean id="solrTemplate" class="org.springframework.data.solr.core.SolrTemplate">
<constructor-arg ref="solrServer" />
</bean>
SolrUtil.java工具类:
@Component
public class SolrUtil {
@Autowired
private SolrTemplate solrTemplate;
@Autowired
private TbItemMapper itemMapper;
@Test
public void importItemData(){
//获取所有SKU列表的信息
List<TbItem> itemList = itemMapper.selectByExample(null);
for (TbItem item : itemList) {
System.out.println(item.getTitle());
}
//将所有的SKU列表信息保存到solr的域中
solrTemplate.saveBeans(itemList);
//提交
solrTemplate.commit();
}
public static void main(String[] args) {
//加载所有配置文件
ApplicationContext ac = new ClassPathXmlApplicationContext("classpath*:spring/applicationContext*.xml");
SolrUtil solrUtil = ac.getBean(SolrUtil.class);
solrUtil.importItemData();
}
}
运行main方法就可以将查询到的所有SKU表的数据信息存到solr的数据库中
6.完成pyg_search_web模块中的search.html页面的展示以及搜索
search.html页面效果展示
search.html中:
引入js文件
<!--引入js-->
<script src="plugins/angularjs/angular.min.js"></script>
<script src="js/base.js" ></script>
<script src="js/service/searchService.js" ></script>
<!--<script src="js/controller/baseController.js"></script>-->
<script src="js/controller/searchController.js" ></script>
body标签
<body ng-app="pinyougou" ng-controller="searchController">
搜索栏部分
<!--searchAutoComplete-->
<div class="input-append">
<input ng-model="searchMap.keyWords" type="text" id="autocomplete" type="text" class="input-error input-xxlarge" />
<button ng-click="itemSearch()" class="sui-btn btn-xlarge btn-danger" type="button">搜索</button>
</div>
SKU商品展示数据绑定
<li ng-repeat="item in itemList" class="yui3-u-1-5">
<div class="list-wrap">
<div class="p-img">
<a href="" target="_blank"><img src="{{item.image}}" /></a>
</div>
<div class="price">
<strong>
<em>¥</em>
<i>{{item.price}}</i>
</strong>
</div>
<div class="attr">
<em>{{item.title}}</em>
</div>
<div class="cu">
<em><!--<span>促</span>满一件可参加超值换购--></em>
</div>
<div class="commit">
<i class="command">已有2000人评价</i>
</div>
<div class="operate">
<a href="success-cart.html" target="_blank" class="sui-btn btn-bordered btn-danger">加入购物车</a>
<a href="javascript:void(0);" class="sui-btn btn-bordered">对比</a>
<a href="javascript:void(0);" class="sui-btn btn-bordered">关注</a>
</div>
</div>
</li>
searchController.js中
//初始化searchMap
$scope.searchMap={keyWords:'三星'};
//查询SKU商品
$scope.itemSearch=function () {
searchService.itemSearch($scope.searchMap).success(
function (response) {
$scope.itemList=response.content;
}
)
}
searchService.js中
//查询SKU商品
this.itemSearch=function (searchMap) {
return $http.post('itemSearch/search.do',searchMap);
}
ItemSearchController.java中
@RestController
@RequestMapping("/itemSearch")
public class ItemSearchController {
@Reference
private ItemSearchService itemSearchService;
//因为要把页面传递过来的json格式的转成对象,用RequestBody
@RequestMapping("/search")
public Map search(@RequestBody Map searchMap){
return itemSearchService.itemSearch(searchMap);
}
}
ItemSearchServiceImpl.java中
@Service//注意这个Service是dubbo这个jar里的
public class ItemSearchServiceImpl implements ItemSearchService {
@Autowired
private SolrTemplate solrTemplate;
@Override
public Map itemSearch(Map searchMap) {
//先接收页面穿过来的关键字
String keyWords = (String) searchMap.get("keyWords");
System.out.println(keyWords);
Map map=new HashMap();
//如果页面的关键字不为空,就去solr库中进行查询
if (keyWords != null) {
SimpleHighlightQuery simpleHighlightQuery = new SimpleHighlightQuery();
//参数需要传域的名字(从solr库中查的时候需要从复制域中查)
Criteria criteria = new Criteria("item_keywords");
//前面需要加上criteria
criteria=criteria.contains(keyWords);
simpleHighlightQuery.addCriteria(criteria);
//高亮查询
HighlightPage<TbItem> highlightPage = solrTemplate.queryForHighlightPage(simpleHighlightQuery,TbItem.class);
List<TbItem> itemList = highlightPage.getContent();
//content需要和searchController.js中的返回值相对应
map.put("content",itemList);
}
return map;
}
}