1.D:\solrHome\collection1\conf 的solrHome 中的 schema.xml中 最后一行添加以下信息
<!--IKAnalyzer Field-->
<field name="title_ik" type="text_ik" indexed="true" stored="true" />
<field name="content_ik" type="text_ik" indexed="true" stored="false" multiValued="true"/>
<!-- IKAnalyzer-->
<fieldType name="text_ik" class="solr.TextField">
<analyzer class="org.wltea.analyzer.lucene.IKAnalyzer"/>
</fieldType>
配置自定义 filed 与这个field 所指向的 中文分词器
2. 把IKAnalyzer的jar文件放到 tomacat中webapp 中的 solr项目/WEB-INF/lib目录下
配置文件放到\solr项目\WEB-INF\classes下,classes如果没有,就创建
3.在 浏览器图形化界面就可以使用 自定义 filed 与中文分析器了
从数据库获取源文件
1.准备数据
CREATE TABLE `products` (
`pid` int(11) NOT NULL AUTO_INCREMENT COMMENT '商品编号',
`name` varchar(255) DEFAULT NULL COMMENT '商品名称',
`catalog` int(11) DEFAULT NULL COMMENT '商品分类ID',
`catalog_name` varchar(50) DEFAULT NULL COMMENT '商品分类名称',
`price` double DEFAULT NULL COMMENT '价格',
`number` int(11) DEFAULT NULL COMMENT '数量',
`description` longtext COMMENT '商品描述',
`picture` varchar(255) DEFAULT NULL COMMENT '图片名称',
`release_time` datetime DEFAULT NULL COMMENT '上架时间',
PRIMARY KEY (`pid`)
) ENGINE=InnoDB AUTO_INCREMENT=6126 DEFAULT CHARSET=utf8;
2.在schema.xml中配置 数据库中的Field
<!--自定义field-->
<field name="title_ik" type="text_ik" indexed="true" stored="true" />
<field name="content_ik" type="text_ik" indexed="true" stored="false" multiValued="true"/>
<!--类型IKAnalyzer-->
<fieldType name="text_ik" class="solr.TextField">
<analyzer class="org.wltea.analyzer.lucene.IKAnalyzer"/>
</fieldType>
<!-- products Field -->
<field name="product_name" type="text_ik" indexed="true" stored="true" />
<field name="product_catalog_name" type="string" indexed="true" stored="true" />
<field name="product_price" type="float" indexed="true" stored="true" />
<field name="product_description" type="text_ik" indexed="true" stored="false" />
<field name="product_picture" type="string" indexed="false" stored="true" />
<field name="product_release_time" type="date" indexed="true" stored="true" />
<!-- products copyField -->
<field name="product_keywords" type="text_ik" indexed="true" stored="false" multiValued="true"/>
<copyField source="product_name" dest="product_keywords"/>
<copyField source="product_description" dest="product_keywords"/>
使用自定义的分析器 其中 keywordsField 就是将 商品的名称 与描述 复制到这个域 到时候查询只需要在这个域就可以了
二、导入数据
定义dataimport-handler步骤:
1、打开collection1/config/solrconfig.xml
2、导入jar ,放到 solrHome\collection1\lib下 如果没有lib文件夹就自己创建
solr-4.10.3\dist\solr-dataimporthandler-extras-4.10.3.jar
solr-4.10.3\dist\ solr-dataimporthandle-4.10.3.jar
mysql-connector-java-8.0.16-bin.jar
3、在solrconfig.xml中添加如下配置文件
<requestHandler name="/dataimport"
class="org.apache.solr.handler.dataimport.DataImportHandler">
<lst name="defaults">
<str name="config">data-config.xml</str>
</lst>
</requestHandler>
4、创建配置文件data-config.xml放到和solrconfig.xml同目录 data-config.xml内容如下: 配置从mysql 查询的对应信息
<?xml version="1.0" encoding="UTF-8" ?>
<dataConfig>
<dataSource type="JdbcDataSource"
driver="com.mysql.cj.jdbc.Driver"
url="jdbc:mysql://localhost:3306/test"
user="root"
password="123"/>
<document>
<entity name="product" query="SELECT pid,name,catalog_name,price,description,picture FROM products ">
<field column="pid" name="id"/>
<field column="name" name="product_name"/>
<field column="catalog_name" name="product_catalog_name"/>
<field column="price" name="product_price"/>
<field column="description" name="product_description"/>
<field column="picture" name="product_picture"/>
</entity>
</document>
</dataConfig>
5.重启tomcat 点击dataimport 点击execute 再点击refresh 多刷新几次
查询出数据库的数据
1.df默认域 就可以使用前面配置的 product_keywords 这里面存放了 商品名称与描述 就是一个or 的查询了
2.fq 过滤在原有查询基础上过滤 比如 product_price :[10 TO 100] 查询价格10到100的 语法 {不包括临界 TO必须大写
3.sort 排序 product_price asc 升序 降序 product_price desc start row 分页
4.hl 高亮 高亮q关键字里的字 列如q 为花儿朵朵 hl 为要高亮的域 product_name
就会高亮 product_name 里含有 花儿朵朵的字段
java代码实现复杂查询
@Test
public void searchIndex() throws Exception {
SolrServer solrServer = new HttpSolrServer("http://localhost:8080/solr/collection1");
SolrQuery solrQuery = new SolrQuery();
solrQuery.setQuery("花儿朵朵"); 设置q
//solrQuery.set("q","花儿朵朵");
solrQuery.set("fq", "product_catalog_name:时尚卫浴"); 设置fq
solrQuery.set("fq", "product_price:[0 TO 200]");
//solrQuery.setFilterQueries(product_catalog_name:时尚卫浴)
solrQuery.setStart(0); 设置分页
solrQuery.setRows(10);
solrQuery.set("df", "product_keywords"); 设置默认查询域
solrQuery.setHighlight(true); 开启高亮
solrQuery.setHighlightSimplePre("<span color=\"red\">"); 设置前缀 为红色
solrQuery.setHighlightSimplePost("</span>"); 设置标签后缀
solrQuery.addHighlightField("product_name"); 设置要高亮的域
solrQuery.setSort("product_price", SolrQuery.ORDER.asc); 设置排序
QueryResponse queryResponse = solrServer.query(solrQuery); 执行查询
SolrDocumentList results = queryResponse.getResults(); 获得结果
System.out.println("总条数:" + results.getNumFound());
Iterator var6 = results.iterator();
while(var6.hasNext()) {
SolrDocument solrDocument = (SolrDocument)var6.next();
System.out.println(solrDocument.get("id"));
System.out.println(solrDocument.get("product_name"));
System.out.println(solrDocument.get("product_catalog_name"));
System.out.println(solrDocument.get("product_price"));
System.out.println(solrDocument.get("product_description"));
System.out.println(solrDocument.get("product_picture"));
System.out.println("-----------------------------");
}
}