solr笔记

1.搭建solr服务器

新建一个solr目录,将tomcat解压;从http://lucene.apache.org/solr/下载solr(这里使用solr4.10.3.zip)同样解压,将solr\solr-4.10.3\example\webapps目录下的solr.war包复制到solr\apache-tomcat-7.0.68\webapps,解压并删除war包(不删除tomcat启动会自动创建一个solr项目并覆盖),将solr\solr-4.10.3\example\lib\ext包下的jar包复制到solr\apache-tomcat-7.0.68\webapps\solr\WEB-INF\lib下;新建一个solrhome文件夹,复制solr\solr-4.10.3\example\solr下的全部到solrhome,打开solr\apache-tomcat-7.0.68\webapps\solr\WEB-INF下的web.xml文件配置solrhome路径

   <env-entry>
       <env-entry-name>solr/home</env-entry-name>
       <env-entry-value>E:\solr\solrhome</env-entry-value>
       <env-entry-type>java.lang.String</env-entry-type>

    </env-entry>

启动Tomcat访问http://localhost:8080/solr/

2.打开solr\solrhome\collection1\conf下的schema.xml配置IKAnalyzer 中文分析器

<!-- IKAnalyzer -->
     <fieldType name="text_ik" class="solr.TextField">
        <analyzer class="org.wltea.analyzer.lucene.IKAnalyzer"/>
     </fieldType>
     
     <field name="title_ik" type="text_ik" indexed="true" stored="true" />

     <field name="content_ik" type="text_ik" indexed="true" stored="false" multiValued="true" />

<!--product-->
     <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="true" stored="true" />
     
     <field name="product_keywords" type="text_ik" indexed="true" stored="false" />
     <copeField source="product_name" dest="product_keywords"/>
     <copeField source="product_description" dest="product_keywords"/>

solrconfig.xml

 <luceneMatchVersion>4.10.3</luceneMatchVersion>

 <requestHandler name="/dataimport" class="org.apache.solr.handler.dataimport.DataImportHandler">
     <lst name="defaults">
       <str name="config">data-config.xml</str>
     </lst>

     </requestHandler>

data-config.xml(新建)

<?xml version="1.0" encoding="UTF-8"?>
<dataConfig>
    <dataSource name="jdbcDataSource" type="JdbcDataSource" 
    driver="com.mysql.jdbc.Driver"
    url="jdbc:mysql://localhost:3306/lucene" 
    user="root" password="root"/>
    <document>
        <entity dataSource="jdbcDataSource" name="product"  
        query="select * from products" >
            <field column="pid" name="id"></field>
            <field column="name" name="product_name"></field> 
            <field column="catalog_name" name="product_catalog_name"></field> 
            <field column="price" name="product_price"></field>
            <field column="description" name="product_description"></field>
            <field column="picture" name="product_picture"></field>
        </entity>
    </document>
  </dataConfig>

3.复制solr\solr-4.10.3\dist下的solr-dataimporthandler-4.10.3.jar,solr-dataimporthandler-extras-4.10.3.jar到solr\solrhome\collection1\lib下(新建lib包)在copy一个MySQL数据库驱动包。

4.solr后台crud操作

public class SolrJManager {
// 添加
@Test
public void testadd() throws Exception {
String baseURL = "http://localhost:8080/solr/collection1";
// String baseURL = "http://localhost:8080/solr/collection2";
// 单机版
SolrServer solrServer = new HttpSolrServer(baseURL);
SolrInputDocument doc = new SolrInputDocument();
doc.setField("id", 4);
doc.setField("name", "范冰冰");
solrServer.add(doc);
solrServer.commit();
}


// 删除
@Test
public void testdelete() throws Exception {
String baseURL = "http://localhost:8080/solr/collection1";
// String baseURL = "http://localhost:8080/solr/collection2";
// 单机版
SolrServer solrServer = new HttpSolrServer(baseURL);
solrServer.deleteByQuery("id:4", 1000);
solrServer.commit();
}


// 修改
@Test
public void testupdate() throws Exception {
String baseURL = "http://localhost:8080/solr/collection1";
// String baseURL = "http://localhost:8080/solr/collection2";
// 单机版
SolrServer solrServer = new HttpSolrServer(baseURL);
// 与添加相同只要ID相同就是添加不同就是更新
}


// 查询
@Test
public void testquery() throws Exception {
String baseURL = "http://localhost:8080/solr/collection1";
// String baseURL = "http://localhost:8080/solr/collection2";
// 单机版
SolrServer solrServer = new HttpSolrServer(baseURL);
// 查询 关键词 饼干
// 过滤条件“product_catalog_name:零食”,“product_price:9.9”价格排序,分页,每页条数
SolrQuery solrQuery = new SolrQuery();
solrQuery.set("q", "product_name:饼干");
// solrQuery.setQuery("product_name:饼干");
solrQuery.set("fq", "product_catalog_name:零食");
solrQuery.set("fq", "product_price:[* TO 10]");
solrQuery.addSort("product_price", ORDER.desc);
// 分页
solrQuery.setStart(0);
solrQuery.setRows(1);
// 高亮
// 1打开开关
solrQuery.setHighlight(true);
// 2指定高亮部分
solrQuery.addHighlightField("product_name");
// 3前缀
solrQuery.setHighlightSimplePre("<span style='color:red'>");
// 4后缀
solrQuery.setHighlightSimplePost("</span>");
// 执行查询
QueryResponse response = solrServer.query(solrQuery);
// 文档结果集
SolrDocumentList docs = response.getResults();
Map<String, Map<String, List<String>>> highlighting = response.getHighlighting();
//Map <K id:V map>
//Map <K 域名:V list>
//List list.get(0);
// 总条数
long numFound = docs.getNumFound();
System.out.println(numFound);
/**
* "product_catalog_name": "零食", "product_price": 9.9, "product_name":
* "小熊饼干", "id": "1", "product_picture": "1234.jpg",
*/
for (SolrDocument doc : docs) {
System.out.println(doc.get("id"));
System.out.println(doc.get("product_name"));
System.out.println(doc.get("product_catalog_name"));
System.out.println(doc.get("product_price"));
System.out.println(doc.get("product_picture"));
System.out.println("-----------");
Map<String, List<String>> map = highlighting.get(doc.get("id"));
List<String> list = map.get("product_name");
System.out.println(list.get(0));
}
}

}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
提供的源码资源涵盖了安卓应用、小程序、Python应用和Java应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值