全文检索sorl

全文检索sorl索引和搜索流程图 nbsp 创建文档对象获取原始内容的目的是为了索引 在索引前需要将原始内容创建成文档 Document 文档中包括一个一个的域 Field 域中存储内容 注意 每个 Document 可以有多个 Field 不同的 Document 可以有不同的 Filed 同一个 Document 可以有相同的 Field 域名和值都相同 每个文档都有唯一的编号 id l

索引和搜索流程图


全文检索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
文件。
全文检索sorl

第九步:启动
Tomcat




配置业务域


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

第二步:配置一个FieldType,制定使用IKAnalyzer
修改
schema.xml
文件
修改
Solr

schema.xml
文件,添加
FieldType

 
 



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


设置业务系统Field




 
 
type=”long” indexed=”true” stored=”true”/>


 






第四步:重启
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(

);

              query .setHighlightSimplePost(
);
    
 
    
 
//
 
第四步:执行查询。得到一个
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

>>

 
highlighting
 
=
 
response
.getHighlighting();
    
 
    
 
    
 
List

 
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
全文检索sorl


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
及端口号配置好。
全文检索sorl
第六步:让
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″



全文检索sorl
第八步:启动每个
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”?>
 
     
     
     
     <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>









版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/216296.html原文链接:https://javaforall.net

(0)
上一篇 2026年3月18日 下午12:09
下一篇 2026年3月18日 下午12:09


相关推荐

  • python基本语法结构

    python基本语法结构python 基本语法结构目录 1 0 变量 2 0 数据类型 3 0 类型转换 4 0 字符串结语目录 1 0 变量在 Python 中 当你给它赋值时就会创建变量 Python 没有用于声明变量的命令 变量在第一次为其赋值时创建 和 matlab 一样 2 0 数据类型 List 列表是一个有序且可变的集合 允许重复成员 turple 元组是一个有序且不可更改的集合 允许重复成员 Set 集合是一个无序且无索引的集合 没有重复的成员 dict 字典是一个有序 且可变的集合 没有重复的成员 从 Python

    2026年3月16日
    2
  • Django(59)验证和授权[通俗易懂]

    Django(59)验证和授权[通俗易懂]验证和授权概述Django有一个内置的授权系统。他用来处理用户、分组、权限以及基于cookie的会话系统。Django的授权系统包括验证和授权两个部分。验证是验证这个用户是否是他声称的人(比如用户名

    2022年8月7日
    5
  • javascript三目运算符的嵌套

    javascript三目运算符的嵌套普通的三目运算符比较简单,就不做介绍了,如(expr1)?(expr2):(expr3),之前在使用三目运算符嵌套的时候,我是这样用的(expr1)?(expr2)

    2022年6月16日
    92
  • 5分钟商学院之个人篇–演讲能力和沟通能力

    1.演讲能力1.1认知台阶影响力是成为领导的必要条件,而演讲是实现影响力最重要的方法之一经常有人说,全天下最难的两件事:一是把钱从别人的口袋里掏出来,二是把想法塞到别人的脑海中去。这个说法不

    2021年12月30日
    51
  • 可以对属性进行封装么_元器件封装类型

    可以对属性进行封装么_元器件封装类型1、RAII简介RAII(ResourceAcquisitionIsInitialization),也称为“资源获取就是初始化”,是C++语言的一种管理资源、避免泄漏的惯用法。C++标准保证任何情况下,已构造的对象最终会销毁,即它的析构函数最终会被调用。简单的说,RAII的做法是使用一个对象,在其构造时获取资源,在对象生命期控制对资源的访问使之始终保持有效,最后在对象析构的时候释放资源。2、RAII分类根据RAII对资源的所有权可分为常性类型和变性类型,代表者分别是std::shared_p

    2025年5月30日
    4
  • Jenkins(4)docker容器内部修改jenkins容器时间「建议收藏」

    Jenkins(4)docker容器内部修改jenkins容器时间「建议收藏」前言用docker搭建的Jenkins环境时间显示和我们本地时间相差8个小时,需修改容器内部的系统时间查看时间查看系统时间date-R进入docker容器内部,查看容器时间dockere

    2022年7月28日
    11

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号