Solr全文搜索服务器
示意图
Solr和Lucene的区别
Solr下载与安装
下载地址:http://archive.apache.org/dist/lucene/solr/4.10.3/
单个solr.war是可以独立运行的,但我们想在tomcat里运行,因此解压Solr是在tomcat的webapps下解压的(注意)
主页:
点击collection1,点击documents,往里填加内容,然后Submit Doucment。
提交结果:
点击侧边栏的Query,点击页面的Execute Query查询添加的结果
java操作Solr服务器HelloWorld
添加数据(修改数据)
//添加数据,修改数据的话,可以根据id直接自动覆盖掉旧的内容信息
public void testAdd() throws Exception{
String URL = "192.168.179.200:8080/solr";
//实例化solr对象
SolrServer solrServer = new HttpSolrServer(URL);
//实例化添加数据
SolrInputDocument doc1 = newSolrInputDocument();
doc1.setFiled("id", "1001");
doc1.setFiled("name", "iphone6s手机");
doc1.setFiled("price", "6000");
doc1.setFiled("url", "/images/001.jpg");
SolrInputDocument doc2 = newSolrInputDocument();
doc2.setFiled("id", "1002");
doc2.setFiled("name", "三星s6手机");
doc2.setFiled("price", "5000");
doc2.setFiled("url", "/images/002.jpg");
//设置服务器保存信息并提交
solrServer.add(doc1);
solrServer.add(doc2);
solrServer.commit();
}
执行查询
//查詢
public void testSearch() throws Exception{
String URL = "192.168.179.200:8080/solr";
//实例化solr对象
SolrServer solrServer = new HttpSolrServer(URL);
//查詢类
SolrQuery solrQuery = new SolrQuery();
//查询关键词
solrQuery.set("q", "name:手机");
//查询数据
QueryResponse response = solrServer.query(solrQuery);
//取数据
SolrDocumentList solrList = response.getResults();
long num = solrList.getNumFound();
System.out.println("条数:" + num);
for (SolrDocument sd : solrList) {
String id = (String)sd.get("id");
String name = (String)sd.get("name");
String url = (String)sd.get("url");
Float price = (Float)sd.get("price");
System.out.println("id:" + id);
}
}
删除
public void testDel() throws Exception {
SolrServer solrServer = new HttpSolrServer(URL);
//deleteById形式
solrServer.deleteById("1");
//deleteByQuery形式
solrServer.deleteByQuery("id:1001 id:1002");
solrServer.commit();
}
全文检索基础
IK的下载地址:https://code.google.com/archive/p/ik-analyzer/downloads
添加的配合:
<fieldType name="text_ik" class="solr.TextField">
<!--index add IKAnalyzer-->
<analyzer type="index" isMaxWordLength="false" class="org.wltea.analyzer.lucene.IKAnalyzer"/>
<!--search add IKAnalyzer-->
<analyzer type="query" isMaxWordLength="true" class="org.wltea.analyzer.lucene.IKAnalyzer"/>
</fieldType>
重启tomcat,然后测试
Solr的基础
Solr的索引操作
Solr搜索
Solr添加对象
java对象加注解
直接server添加bean
类的转换
Solr的查询操作
public void queryCase() throws Exception {
SolrServer solrServer = new HttpSolrServer(URL);
SolrQuery params = new SolrQuery();
//AND、OR、NOT条件
params.set("q", "name:手 AND price:9");
params.set("q", "name:手 OR price:9");
params.set("q", "name:手机 NOT price:9");
//TO条件: 6 <= price <= 9
params.set("q", "name:手机 AND price:[6 TO 9]");
//TO条件:6 < price < 9 添加过滤器提高查询效率
params.set("q", "price:{6 TO 9}");
params.addFilterQuery("name:电脑");
QueryResponse response = solrServer.query(params);
//取数据
SolrDocumentList solrList = response.getResults();
//显示设置
}
public void testSearchMulti() throws Exception {
ModifiableSolrParams params = new ModifiableSolrParams();
//查询关键词,*:* 代表所有属性,所有值,即所有index
params.set("q", "*:*");
params.set("start", 0);
params.set("rows", 20);
params.set("sort", "price desc");
QueryResponse response = server.query(params);
SolrDocumentList solrList = response.getResults();
}
Solr管理员命令
Solr集群搭建
案例实战说明