全文检索sorl

索引和搜索流程图

 创建文档对象

获取原始内容的目的是为了索引,在索引前需要将原始内容创建成文档(Document), 文档中包括一个一个的域(Field),域中存储内容.
注意:每个Document可以有多个Field,不同的Document可以有不同的Filed, 同一个Document可以有相同的Field(域名和值都相同).每个文档都有唯一的编号id.

lucene代码测试

导入lucence相关jar包
创建索引
    // 创建索引
    @Test
    public void testIndex() throws Exception {
        // 第一步:创建一个java工程,并导入jar包。
        // 第二步:创建一个indexwriter对象。
        Directory directory = FSDirectory.open(new File("D:\\temp\\index"));
        // Directory directory = new RAMDirectory();//保存索引到内存中 (内存索引库)
        //  Analyzer analyzer = new StandardAnalyzer();// 官方推荐
        Analyzer analyzer = new IKAnalyzer();// 官方推荐
        IndexWriterConfig config = new IndexWriterConfig(Version.LATEST, analyzer);
        IndexWriter indexWriter = new IndexWriter(directory, config);
        // 1)指定索引库的存放位置Directory对象
        // 2)指定一个分析器,对文档内容进行分析。
        // 第三步:创建field对象,将field添加到document对象中。
        File f = new File("D:\\Lucene&solr\\searchsource");
        File[] listFiles = f.listFiles();
        for (File file : listFiles) {
            // 第三步:创建document对象。
            Document document = new Document();
            // 文件名称
            String file_name = file.getName();
            Field fileNameField = new TextField("fileName", file_name, Store.YES);
            // 文件大小
            long file_size = FileUtils.sizeOf(file);
            Field fileSizeField = new LongField("fileSize", file_size, Store.YES);
            // 文件路径
            String file_path = file.getPath();
            Field filePathField = new StoredField("filePath", file_path);
            // 文件内容
            String file_content = FileUtils.readFileToString(file);
            Field fileContentField = new TextField("fileContent", file_content, Store.NO);
            document.add(fileNameField);
            document.add(fileSizeField);
            document.add(filePathField);
            document.add(fileContentField);
            // 第四步:使用indexwriter对象将document对象写入索引库,此过程进行索引创建。并将索引和document对象写入索引库。
            indexWriter.addDocument(document);
        }
        // 第五步:关闭IndexWriter对象。
        indexWriter.close();
    }


是否存储的标准:是否要将内容展示给用户
File类
数据类型
Analyzed
是否分析
indexed
是否索引
Stored
是否存储
说明
StringField(FieldName,FieldValue,Store.YES)
字符串
N
Y
Y或N
这个Field用来构建一个字符串Field,但是不会进行分析,会将整个串存储在索引中,是否存储在文档中用(Store.YES或Store.NO决定)
LongField(FieldName,
FieldValue,Store.YES)
Long型
Y
Y
Y或N
这个Field用来构建一个Long数字符型Field,进行分析和索引, 是否存储在文档中用(Store.YES或Store.NO决定)
StoredField(FieldName,FieldValue)
重载方法,支持多种类型
N
N
Y
这个Field用来构建不同类型Field,不分析,不索引,但要Field存储在文档中.
TextField(FieldName,FieldValue,Store.NO)或
TextField(FieldName,reader)
字符串,
Y
Y
Y或N
如果是一个Reader,lucene猜测内容比较多,会采用Unstored的策略

搜索索引
    // 搜索索引
    @Test
    public void testSearch() throws Exception {
        // 第一步:创建一个Directory对象,也就是索引库存放的位置。
        Directory directory = FSDirectory.open(new File("D:\\temp\\index"));// 磁盘
        // 第二步:创建一个indexReader对象,需要指定Directory对象。
        IndexReader indexReader = DirectoryReader.open(directory);
        // 第三步:创建一个indexsearcher对象,需要指定IndexReader对象
        IndexSearcher indexSearcher = new IndexSearcher(indexReader);
        // 第四步:创建一个TermQuery对象,指定查询的域和查询的关键词。
        Query query = new TermQuery(new Term("fileName", "lucene"));
        // 第五步:执行查询。
        TopDocs topDocs = indexSearcher.search(query, 10);
        // 第六步:返回查询结果。遍历查询结果并输出。
        ScoreDoc[] scoreDocs = topDocs.scoreDocs;
        for (ScoreDoc scoreDoc : scoreDocs) {
            int doc = scoreDoc.doc;
            Document document = indexSearcher.doc(doc);
            // 文件名称
            String fileName = document.get("fileName");
            System.out.println(fileName);
            // 文件内容
            String fileContent = document.get("fileContent");
            System.out.println(fileContent);
            // 文件大小
            String fileSize = document.get("fileSize");
            System.out.println(fileSize);
            // 文件路径
            String filePath = document.get("filePath");
            System.out.println(filePath);
            System.out.println("------------");
        }
        // 第七步:关闭IndexReader对象
        indexReader.close();
    }

增删改查
/**
* 索引维护
* 添加  入门程序
* 删除
* 修改
* 查询  入门程序 精准查询
* @author lx
*
*/
public class LuceneManager {
    //
    public IndexWriter getIndexWriter() throws Exception{
        // 第一步:创建一个java工程,并导入jar包。
        // 第二步:创建一个indexwriter对象。
        Directory directory = FSDirectory.open(new File("D:\\temp\\index"));
        // Directory directory = new RAMDirectory();//保存索引到内存中 (内存索引库)
        Analyzer analyzer = new StandardAnalyzer();// 官方推荐
        IndexWriterConfig config = new IndexWriterConfig(Version.LATEST, analyzer);
        return new IndexWriter(directory, config);
    }
    //全删除
    @Test
    public void testAllDelete() throws Exception {
        IndexWriter indexWriter = getIndexWriter();
        indexWriter.deleteAll();
        indexWriter.close();
    }
    //根据条件删除
    @Test
    public void testDelete() throws Exception {
        IndexWriter indexWriter = getIndexWriter();
        Query query = new TermQuery(new Term("fileName","apache"));
        indexWriter.deleteDocuments(query);
        indexWriter.close();
    }
    //修改
    @Test
    public void testUpdate() throws Exception {
        IndexWriter indexWriter = getIndexWriter();
        Document doc = new Document();
        doc.add(new TextField("fileN", "测试文件名",Store.YES));
        doc.add(new TextField("fileC", "测试文件内容",Store.YES));
        indexWriter.updateDocument(new Term("fileName","lucene"), doc, new IKAnalyzer());
        indexWriter.close();
    }
    //IndexReader  IndexSearcher
    public IndexSearcher getIndexSearcher() throws Exception{
        // 第一步:创建一个Directory对象,也就是索引库存放的位置。
        Directory directory = FSDirectory.open(new File("D:\\temp\\index"));// 磁盘
        // 第二步:创建一个indexReader对象,需要指定Directory对象。
        IndexReader indexReader = DirectoryReader.open(directory);
        // 第三步:创建一个indexsearcher对象,需要指定IndexReader对象
        return new IndexSearcher(indexReader);
    }
    //执行查询的结果
    public void printResult(IndexSearcher indexSearcher,Query query)throws Exception{
        // 第五步:执行查询。
        TopDocs topDocs = indexSearcher.search(query, 10);
        // 第六步:返回查询结果。遍历查询结果并输出。
        ScoreDoc[] scoreDocs = topDocs.scoreDocs;
        for (ScoreDoc scoreDoc : scoreDocs) {
            int doc = scoreDoc.doc;
            Document document = indexSearcher.doc(doc);
            // 文件名称
            String fileName = document.get("fileName");
            System.out.println(fileName);
            // 文件内容
            String fileContent = document.get("fileContent");
            System.out.println(fileContent);
            // 文件大小
            String fileSize = document.get("fileSize");
            System.out.println(fileSize);
            // 文件路径
            String filePath = document.get("filePath");
            System.out.println(filePath);
            System.out.println("------------");
        }
    }
    //查询所有
    @Test
    public void testMatchAllDocsQuery() throws Exception {
        IndexSearcher indexSearcher = getIndexSearcher();
        Query query = new MatchAllDocsQuery();
        System.out.println(query);
        printResult(indexSearcher, query);
        //关闭资源
        indexSearcher.getIndexReader().close();
    }
    //根据数值范围查询
    @Test
    public void testNumericRangeQuery() throws Exception {
        IndexSearcher indexSearcher = getIndexSearcher();
        
        Query query = NumericRangeQuery.newLongRange("fileSize", 47L, 200L, false, true);
        System.out.println(query);
        printResult(indexSearcher, query);
        //关闭资源
        indexSearcher.getIndexReader().close();
    }
    //可以组合查询条件
    @Test
    public void testBooleanQuery() throws Exception {
        IndexSearcher indexSearcher = getIndexSearcher();
        
        BooleanQuery booleanQuery = new BooleanQuery();
        
        Query query1 = new TermQuery(new Term("fileName","apache"));
        Query query2 = new TermQuery(new Term("fileName","lucene"));
        //  select * from user where id =1 or name = 'safdsa'
        booleanQuery.add(query1, Occur.MUST);
        booleanQuery.add(query2, Occur.SHOULD);
        System.out.println(booleanQuery);
        printResult(indexSearcher, booleanQuery);
        //关闭资源
        indexSearcher.getIndexReader().close();
    }
    //条件解释的对象查询
    @Test
    public void testQueryParser() throws Exception {
        IndexSearcher indexSearcher = getIndexSearcher();
        //参数1: 默认查询的域  
        //参数2:采用的分析器
        QueryParser queryParser = new QueryParser("fileName",new IKAnalyzer());
        // *:*   域:值
        Query query = queryParser.parse("fileName:lucene is apache OR fileContent:lucene is apache");
        
        printResult(indexSearcher, query);
        //关闭资源
        indexSearcher.getIndexReader().close();
    }
    //条件解析的对象查询   多个默念域
    @Test
    public void testMultiFieldQueryParser() throws Exception {
        IndexSearcher indexSearcher = getIndexSearcher();
        
        String[] fields = {"fileName","fileContent"};
        //参数1: 默认查询的域  
        //参数2:采用的分析器
        MultiFieldQueryParser queryParser = new MultiFieldQueryParser(fields,new IKAnalyzer());
        // *:*   域:值
        Query query = queryParser.parse("lucene is apache");
        
        printResult(indexSearcher, query);
        //关闭资源
        indexSearcher.getIndexReader().close();
    }   
}


什么是solr

Solr是Apache下的一个顶级开源项目,采用Java开发,它基于Lucene的全文搜索服务器.
Sorl提供了比Lucene更为丰富的查询语言,同时实现了可配置丶可扩展,并对索引丶搜索性能进行了优化.


Solr与Lucene的区别

Lucene是一个开放源代码的全文检索引擎工具包,它不是一个完整的全文检索引擎,
Lucene提供了完整的查询引擎和搜索引擎,目的是为软件开发人员提供一个简单易用的工具包,
以方便的在目标系统中实现全文检索的功能,或者以Lucene为基础构建全文检索引擎.
Sorl的目标是打造一款企业级的搜索引擎系统,它是一个搜索引擎服务,可以独立运行,
通过Sorl可以非常快速的构建企业的搜索引擎,通过Solr也可以高效的完成站内搜索功能.


Sorl服务器搭建步骤
第一步:把 solr   的压缩包上传到 Linux 系统
第二步:解压 solr
第三步:安装 Tomcat ,解压缩即可。
第四步:把 solr 部署到 Tomcat 下。
第五步:解压缩 war 包。启动 Tomcat 解压。
第六步:把 /root/solr-4.10.3/example/lib/ext 目录下的所有的 jar 包,添加到 solr 工程中。
[root@localhost ext]# pwd
/root/solr-4.10.3/example/lib/ext
[root@localhost ext]# cp * /usr/local/solr/tomcat/webapps/solr/WEB-INF/lib/
第七步:创建一个 solrhome /example/solr 目录就是一个 solrhome 。复制此目录到 /usr/local/solr/solrhome
[root@localhost example]# pwd
/root/solr-4.10.3/example
[root@localhost example]# cp -r solr /usr/local/solr/solrhome
[root@localhost example]#
第八步:关联 solr solrhome 。需要修改 solr 工程的 web.xml 文件。
第九步:启动 Tomcat


配置业务域

创建步骤:
第一步:把中文分析器添加到工程中。
  1. IKAnalyzer2012FF_u1.jar添加到solr工程的lib目录下
  2. 把扩展词典、配置文件放到solr工程的WEB-INF/classes目录下。

第二步:配置一个FieldType,制定使用IKAnalyzer
修改 schema.xml 文件
修改 Solr schema.xml 文件,添加 FieldType
<fieldType name="text_ik" class="solr.TextField">
    <analyzer class="org.wltea.analyzer.lucene.IKAnalyzer"/>
</fieldType>

第三步:配置业务域, type 制定使用自定义的 FieldType

设置业务系统Field
<field name="item_title" type="text_ik" indexed="true" stored="true"/>
<field name="item_sell_point" type="text_ik" indexed="true" stored="true"/>
<field name="item_price"     type="long" indexed="true" stored="true"/>
<field name="item_image" type="string" indexed="false" stored="true" />
<field name="item_category_name" type="string" indexed="true" stored="true" />
 
<field name="item_keywords" type="text_ik" indexed="true" stored="false" multiValued="true"/>
<copyField source="item_title" dest="item_keywords"/>
<copyField source="item_sell_point" dest="item_keywords"/>
<copyField source="item_category_name" dest="item_keywords"/>

第四步:重启 tomcat


什么是solrJ

Sorlj是访问Sorl服务的java客户端,提供索引和搜索的请求方法,Sorl通常在嵌入在业务系统中,通过Sorlj的API服务.


使用sorlj管理索引库

添加文档
创建步骤:
第一步:把 solrJ jar 包添加到工程中。
第二步:创建一个 SolrServer ,使用 HttpSolrServer 创建对象。
第三步:创建一个文档对象 SolrInputDocument 对象。
第四步:向文档中添加域。必须有 id 域,域的名称必须在 schema.xml 中定义。
第五步:把文档添加到索引库中。
第六步:提交。

测试:
@Test
       public   void   addDocument ()   throws   Exception {
              //   第一步:把 solrJ jar 包添加到工程中。
              //   第二步:创建一个 SolrServer ,使用 HttpSolrServer 创建对象。
              SolrServer   solrServer   =   new   HttpSolrServer( " http://192.168.25.154:8080/solr" ; );
              //   第三步:创建一个文档对象 SolrInputDocument 对象。
              SolrInputDocument   document   =   new   SolrInputDocument();
              //   第四步:向文档中添加域。必须有 id 域,域的名称必须在 schema.xml 中定义。
              document .addField( "id" ,   "test001" );
              document .addField( "item_title" ,   " 测试商品 " );
              document .addField( "item_price" ,   "199" );
              //   第五步:把文档添加到索引库中。
              solrServer .add( document );
              //   第六步:提交。
              solrServer .commit();
       }


删除文档
创建步骤:
第一步:创建一个 SolrServer 对象。
第二步:调用 SolrServer 对象的根据 id 删除或查询条件删除的方法。
第三步:提交。

测试:(根据id删除)
@Test
       public   void   deleteDocumentById ()   throws   Exception {
              //   第一步:创建一个 SolrServer 对象。
              SolrServer   solrServer   =   new   HttpSolrServer( " http://192.168.25.154:8080/solr" ; );
              //   第二步:调用 SolrServer 对象的根据 id 删除的方法。
              solrServer .deleteById( "1" );
              //   第三步:提交。
              solrServer .commit();
       }

测试:(根据查询删除)
@Test
       public   void   deleteDocumentByQuery()   throws   Exception {
              SolrServer   solrServer   =   new   HttpSolrServer( " http://192.168.25.154:8080/solr" ; );
              solrServer .deleteByQuery( "title:change.me" );
              solrServer .commit();
       }

修改文档
和添加文档相似

查询索引库
创建步骤:
查询步骤:
第一步:创建一个 SolrServer 对象
第二步:创建一个 SolrQuery 对象。
第三步:向 SolrQuery 中添加查询条件、过滤条件。。。
第四步:执行查询。得到一个 Response 对象。
第五步:取查询结果。
第六步:遍历结果并打印。

测试:(简单查询)
@Test
       public   void   queryDocument()   throws   Exception {
              //   第一步:创建一个 SolrServer 对象
              SolrServer   solrServer   =   new   HttpSolrServer( " http://192.168.25.154:8080/solr" ; );
              //   第二步:创建一个 SolrQuery 对象。
              SolrQuery   query   =   new   SolrQuery();
              //   第三步:向 SolrQuery 中添加查询条件、过滤条件。。。
              query .setQuery( "*:*" );
              //   第四步:执行查询。得到一个 Response 对象。
              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" ));
              }
       }

测试:(带高亮显示)
@Test
       public   void   queryDocumentWithHighLighting ()   throws   Exception {
              //   第一步:创建一个 SolrServer 对象
              SolrServer   solrServer   =   new   HttpSolrServer( " http://192.168.25.154:8080/solr" ; );
              //   第二步:创建一个 SolrQuery 对象。
              SolrQuery   query   =   new   SolrQuery();
              //   第三步:向 SolrQuery 中添加查询条件、过滤条件。。。
              query .setQuery( " 测试 " );
              // 指定默认搜索域
              query .set( "df" ,   "item_keywords" );
         //设置分页条件
           query.setStart(1);
             query .setRows(2);
              // 开启高亮显示
              query .setHighlight( true );
              // 高亮显示的域
              query .addHighlightField( "item_title" );
              query .setHighlightSimplePre( "<em>" );
              query .setHighlightSimplePost( "</em>" );
              //   第四步:执行查询。得到一个 Response 对象。
              QueryResponse   response   =   solrServer .query( query );
              //   第五步:取查询结果。
              SolrDocumentList   solrDocumentList   =   response .getResults();
              System. out .println( " 查询结果的总记录数: "   +   solrDocumentList .getNumFound());
              //   第六步:遍历结果并打印。
              for   (SolrDocument   solrDocument   :   solrDocumentList ) {
                     System. out .println( solrDocument .get( "id" ));
                     // 取高亮显示
                     Map<String, Map<String, List<String>>>   highlighting   =   response .getHighlighting();
                     List<String>   list   =   highlighting .get( solrDocument .get( "id" )).get( "item_title" );
                     String   itemTitle   =   null ;
                     if   ( list   !=   null   &&   list .size() > 0) {
                            itemTitle   =   list .get(0);
                     }   else   {
                            itemTitle   = (String)   solrDocument .get( "item_title" );
                     }
                     System. out .println( itemTitle );
                     System. out .println( solrDocument .get( "item_price" ));
              }
       }



什么是SolrCloud
SolrCloud是Solr提供的分布式搜索方案,当你需要大规模,容错,分布式索引和检索能力时使用SolrCloud.


搭建solr集群需要先搭建zookeeper集群
zookeeper集群的搭建
第一步:需要安装 jdk 环境。
第二步:把 zookeeper 的压缩包上传到服务器。
第三步:解压缩。
第四步:把 zookeeper 复制三份。
[root@localhost ~]# mkdir /usr/local/solr-cloud
[root@localhost ~]# cp -r zookeeper-3.4.6 /usr/local/solr-cloud/zookeeper01
[root@localhost ~]# cp -r zookeeper-3.4.6 /usr/local/solr-cloud/zookeeper02
[root@localhost ~]# cp -r zookeeper-3.4.6 /usr/local/solr-cloud/zookeeper03
第五步:在每个 zookeeper 目录下创建一个 data 目录。
第六步:在 data 目录下创建一个 myid 文件,文件名就叫做“ myid ”。内容就是每个实例的 id 。例如 1 2 3
[root@localhost data]# echo 1 >> myid
[root@localhost data]# ll
total 4
-rw-r--r--. 1 root root 2 Apr  7 18:23 myid
[root@localhost data]# cat myid
1
第七步:修改配置文件。把 conf 目录下的 zoo_sample.cfg 文件改名为 zoo.cfg

server.1=192.168.25.154:2881:3881
server.2=192.168.25.154:2882:3882
server.3=192.168.25.154:2883:3883

第八步:启动每个 zookeeper 实例。
启动 bin/zkServer.sh start

或者使用p处理

vim start-all.sh

cd zookeeper01/bin
./zkServer.sh start
cd ../../
cd zookeeper02/bin
./zkServer.sh start
cd ../../
cd zookeeper03/bin
./zkServer.sh start
cd ../../

chmod u+x start-all.sh

查看 zookeeper 的状态:
bin/zkServer.sh status


Solr集群的搭建

第一步:创建四个tomcat实例。每个tomcat运行在不同的端口。8180828083808480
第二步:部署 solr war 包。把单机版的 solr 工程复制到集群中的 tomcat 中。
第三步:为每个 solr 实例创建一个对应的 solrhome 。使用单机版的 solrhome 复制四份。
第四步:需要修改 solr web.xml 文件。把 solrhome 关联起来。
第五步:配置 solrCloud 相关的配置。每个 solrhome 下都有一个 solr.xml ,把其中的 ip 及端口号配置好。
第六步:让 zookeeper 统一管理配置文件。需要把 solrhome/collection1/conf 目录上传到 zookeeper 。上传任意 solrhome 中的配置文件即可。
使用工具上传配置文件: /root/solr-4.10.3/example/scripts/cloud-scripts/zkcli.sh

./zkcli.sh -zkhost 192.168.25.154:2181,192.168.25.154:2182,192.168.25.154:2183 -cmd upconfig -confdir /usr/local/solr-cloud/solrhome01/collection1/conf -confname myconf

查看 zookeeper 上的配置文件:
使用 zookeeper 目录下的 bin/zkCli.sh 命令查看 zookeeper 上的配置文件:
./zkCli.sh -server ip:端口号
[root@localhost bin]#  ./zkCli.sh
[zk: localhost:2181(CONNECTED) 0]  ls /
[configs, zookeeper]
[zk: localhost:2181(CONNECTED) 1]  ls /configs
[myconf]
[zk: localhost:2181(CONNECTED) 2]  ls /configs/myconf
[admin-extra.menu-top.html, currency.xml, protwords.txt, mapping-FoldToASCII.txt, _schema_analysis_synonyms_english.json, _rest_managed.json,  solrconfig.xml , _schema_analysis_stopwords_english.json, stopwords.txt, lang, spellings.txt, mapping-ISOLatin1Accent.txt, admin-extra.html, xslt, synonyms.txt, scripts.conf, update-script.js, velocity, elevate.xml, admin-extra.menu-bottom.html, clustering,  schema.xml
[zk: localhost:2181(CONNECTED) 3]
退出:
[zk: localhost:2181(CONNECTED) 3] quit

使用以下命令连接指定的 zookeeper 服务:
./zkCli.sh -server 192.168.25.154:2183

第七步:修改 tomcat/bin 目录下的 catalina.sh  文件,关联 solr zookeeper
把此配置添加到配置文件中:

JAVA_OPTS="-DzkHost=192.168.25.154:2181,192.168.25.154:2182,192.168.25.154:2183"

第八步:启动每个 tomcat 实例。要包装 zookeeper 集群是启动状态。

第九步:访问集群


第十步:创建新的Collection 进行分片处理。
http://192.168.25.154:8180/solr/admin/collections?action=CREATE&name=collection2&numShards=2&replicationFactor=2


第十一步:删除不用的Collection


使用Solrj管理集群

添加文档

第一步:把solrJ 相关的jar 包添加到工程中。
第二步:创建一个SolrServer对象,需要使用CloudSolrServer子类。构造方法的参数是zookeeper的地址列表。
第三步:需要设置DefaultCollection 属性。
第四步:创建一SolrInputDocument 对象。
第五步:向文档对象中添加域
第六步:把文档对象写入索引库。
第七步:提交。

测试:
@Test
     public void testSolrCloudAddDocument() throws Exception {
          // 第一步:把solrJ相关的jar包添加到工程中。
          // 第二步:创建一个SolrServer对象,需要使用CloudSolrServer子类。构造方法的参数是zookeeper的地址列表。
          //参数是zookeeper的地址列表,使用逗号分隔
          CloudSolrServer solrServer = new CloudSolrServer("192.168.25.154:2181,192.168.25.154:2182,192.168.25.154:2183");
          // 第三步:需要设置DefaultCollection属性。
          solrServer.setDefaultCollection("collection2");
          // 第四步:创建一SolrInputDocument对象。
          SolrInputDocument document = new SolrInputDocument();
          // 第五步:向文档对象中添加域
          document.addField("item_title", "测试商品");
          document.addField("item_price", "100");
          document.addField("id", "test001");
          // 第六步:把文档对象写入索引库。
          solrServer.add(document);
          // 第七步:提交。
          solrServer.commit();
     }


​编写applicationContext-sorl.xml
<?xml version="1.0" encoding="UTF-8"?>
 
     <!-- 单机版solr服务配置 -->
     <!-- <bean id="httpSolrServer" class="org.apache.solr.client.solrj.impl.HttpSolrServer">
          <constructor-arg name="baseURL" value="http://192.168.25.154:8080/solr"></constructor-arg>;
     </bean> -->
     <!-- 集群版solr服务 -->
     <bean id="cloudSolrServer" class="org.apache.solr.client.solrj.impl.CloudSolrServer">
          <constructor-arg name="zkHost" value="192.168.25.154:2181,192.168.25.154:2182,192.168.25.154:2183"></constructor-arg>    
          <property name="defaultCollection" value="collection2"></property>
     </bean>
</beans>




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值