Principles and use of Solr

 

1.Solr Introduction
Solr is a standalone enterprise search application servers, which provide external similar to the Web-service API interface. Users can http request, submit the XML file format to a certain search engine server generates an index; can also be made to find a request by Http Get operation, and get back the results in XML format.

 

 

2. The principle of
solr is based Lucence development of enterprise-class search engine technology, and lucence principle is inverted index. So what is the inverted index it? Next we will introduce lucence inverted index principle.
Suppose there are two articles 1 and 2:
the article content is 1: old super job in Kazimen, me too.
2 content of the article is: a small super job at the Drum Tower.
Since lucence keyword index is based on a query, then we must first get keyword two articles. If we take the article as a string, we need to get all the words in a string that word. When word, ignore "the", "and" no sense of prepositions and the like, can be filtered and punctuation.
We use the Chinese word Ik Analyzer to achieve results after the word as follows:
Article 1:

Article 2:

Next, with the key words, we can build the inverted index. The above correspondence is: "Article number" of "all of the words in the article." Inverted index to reverse this relationship, becomes: "keyword" on the "all articles No. owns the keywords."

Usually know only keywords in the article which appeared not enough, we also need to know the number of occurrences of keywords and location appear in the article, there are usually two positions:
. A character position, that is, the word record is the first of several articles characters (keyword highlighted the advantage of fast positioning);
. keyword position B,
Plus the frequency and location message appears, our index structure becomes:

实现时,lucene将上面三列分别作为词典文件(Term Dictionary)、频率文件(frequencies)、位置文件 (positions)保存。其中词典文件不仅保存有每个关键词,还保留了指向频率文件和位置文件的指针,通过指针可以找到该关键字的频率信息和位置信息。

 

3.使用SolrJ管理索引库
使用SolrJ可以实现索引库的增删改查操作。
3.1 添加文档
第一步:把solrJ的jar包添加到工程中。
第二步:创建一个SolrServer,使用HttpSolrServer创建对象。
第三步:创建一个文档对象SolrInputDocument对象。
第四步:向文档中添加域。必须有id域,域的名称必须在schema.xml中定义。
第五步:把文档添加到索引库中。
第六步:提交。

public voidtestSolrJAdd() throws SolrServerException, IOException {
// 创建一个SolrServer对象。创建一个HttpSolrServer对象
// 需要指定solr服务的url
SolrServer solrServer = newHttpSolrServer( "http://101.132.69.111:8080/solr/collection1");
// 创建一个文档对象SolrInputDocument
SolrInputDocument document= newSolrInputDocument();
// 向文档中添加域,必须有id域,域的名称必须在schema.xml中定义
document.addField( "id", "123");
document.addField( "item_title", "红米手机");
document.addField( "item_price", 1000);
// 把文档对象写入索引库
solrServer.add( document);
// 提交
solrServer.commit();
}

3.2 删除文档
3.2.1 根据id删除
第一步:创建一个SolrServer对象。
第二步:调用SolrServer对象的根据id删除的方法。
第三步:提交。

publicvoiddeleteDocumentById()throwsException {
SolrServer solrServer = newHttpSolrServer( "http://101.132.69.111:8080/solr/collection1");
solrServer.deleteById( "123");
// 提交
solrServer.commit();
}

3.2.2 根据查询删除

publicvoiddeleteDocumentByQuery()throwsException {
SolrServer solrServer = newHttpSolrServer( "http://101.132.69.111:8080/solr/collection1");
//这边会根据分词去删
solrServer.deleteByQuery( "item_title:红米手机");
solrServer.commit();
}

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

3.3.1 简单查询

publicvoidqueryDocument() throws Exception {
// 第一步:创建一个SolrServer对象
SolrServer solrServer = newHttpSolrServer( "http://101.132.69.111:8080/solr/collection1");
// 第二步:创建一个SolrQuery对象。
SolrQuery query = newSolrQuery();
// 第三步:向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"));
}
}

3.3.2 带高亮显示

public voidsearchDocumet() throws Exception {
// 创建一个SolrServer对象
SolrServer solrServer = newHttpSolrServer( "http://101.132.69.111:8080/solr/collection1");
// 创建一个SolrQuery对象
SolrQuery query = newSolrQuery();
// 设置查询条件、过滤条件、分页条件、排序条件、高亮
// query.set("q", "*:*");
query.setQuery( "手机");
// 分页条件
query.setStart( 0);
query.setRows( 30);
// 设置默认搜索域
query. set( "df", "item_keywords");
// 设置高亮
query.setHighlight( true);
// 高亮显示的域
query.addHighlightField( "item_title");
query.setHighlightSimplePre( "<div>");
query.setHighlightSimplePost( "</div>");
// 执行查询,得到一个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");
StringitemTitle = "";
if(list != null&& list.size() > 0) {
itemTitle = list. get( 0);
} else{
36itemTitle = ( String) solrDocument. get( "item_title");
}
System.out.println(itemTitle);
System.out.println(solrDocument. get( "item_sell_point"));
System.out.println(solrDocument. get( "item_price"));
System.out.println(solrDocument. get( "item_image"));
System.out.println(solrDocument. get( "item_category_name"));
}

 

4.Solr服务器中的后台数据处理
这个其实是通过图形界面操作,只需手动填写查询条件,不需要进行代码处理。但是实际项目开发中,还是需要进行代码编写的。

solr的基础语法
1.q 查询的关键字,此参数最为重要,例如,q= id: 1,默认为q=*:*,
2.fq (filter query)过虑查询,提供一个可选的筛选器查询。返回在q查询符合结果中同时符合的fq条件的查询结果
3.sort 排序方式,例如 iddesc 表示按照 “ id” 降序
4.start 返回结果的第几条记录开始,一般分页用,默认 0开始
5.rows 指定返回结果最多有多少条记录,默认值为 10,配合start实现分页
6.fl 指定返回哪些字段,用逗号或空格分隔,注意:字段区分大小写,例如fl= id,title,sort
7.df 默认的查询字段,一般默认指定
8.wt (writer type)指定输出格式,有 xml, json, php等
9.indent 返回的结果是否缩进,默认关闭0
10.hl 高亮
10.1.hl.fl 设定高亮显示的字段
10.2.hl.requireFieldMatch 如果置为 true,除非用hl.fl指定了该字段,查询结果才会被高亮。它的默认值是 false。
10.3.hl.usePhraseHighlighter 如果一个查询中含有短语(引号框起来的)那么会保证一定要完全匹配短语的才会被高亮。
10.4.hl.highlightMultiTerm如果使用通配符和模糊搜索,那么会确保与通配符匹配的term会高亮。默认为 false,同时hl.usePhraseHighlighter要为 true。
10.5.hl.fragsize 返回的最大字符数。默认是 100.如果为 0,那么该字段不会被fragmented且整个字段的值会被返回。

Guess you like

Origin www.cnblogs.com/ericz2j/p/11118454.html