0 0 0 0

Source: http://www. Cnblogs. com/zhuxiaojie/p/5764680. Html.

This tutorial is based on solr5. 5.

Preface preface

As for why you use solr5. 5, because the latest 6, 10, no chinese support, here's an ik splitter, just supported to 5, 5.

Ik download address: https://github. com/EugenePig/ik-analyzer-solr5 r3, after downloading, use the maven command, the mvn package to generate the jar file, or download thehttp://pan. Baidu. com/s/1o7P0846 I compiled.

Download address solr5. 5: http://apache. Fayea. com/lucene/solr/5. 5. 2/.

Download, download the address, tomcat8.

One: install configuration

After downloading, extract the solr file and extract tomcat.

1. 1 I talling solr on tomcat and building SolrCore.

  • Rename the server solr web app web app in the solr5. 5 directory to solr and place it in the directory of tomcat web app.
  • Open tomcat web app solr web inf web. Xml.
  • Create a new folder, don't use the chinese directory to do solrhome, which is where the SolrCore instance holds
  • Configuring solr 's address in tomcat web app solr web inf web. Xml.

In the tomcat web app solr web inf folder, build the classes directory

  • Copy solr5. 5/server/resource/log4j. Properties to the classes directory created by the previous step.
  • Copy all the jar files under the solr5. 5 server lib directory to the tomcat web app solr web inf lib, which is a jar package for some logs, or start the error.
  • At this time, you can enter 8080/solr/admin. Html to talk to 's control interface.
  • Next is to create SolrCore.
  • Now that the solrhome directory is empty, we create an empty folder core1 that's one of our instances and then copy the solr5. 5/server/solr/configsets/sample_techproducts_configs/conf/file to solrhome core1.
  • Copy solr5. 5/server/solr/solr. Xml to the solrhome directory.
  • In the management console interface of solr, add a core1
  • It creates a successful instance core1, yge. , please pay attention to the part of my code, and you need to perform the first step in step 11. Otherwise, you won't be able to create solr core, that's, there's an error message that's a bug in solr, but there's no fix to it now.

1. 2 I talling ik chinese.

  • Prepare the jar package for the ik, you can compile it, or download it for you. Then copy it to the tomcat web app solr web inf lib. ( Don't copy to tomcat lib so that you can't find the class of lucene )
  • Open the solrhome core1 conf managed schema file, at the bottom, append the following configuration
  • 复制代码
<fieldType name="text_ik" class="solr.TextField"><analyzer type="index" useSmart="false" class="org.wltea.analyzer.lucene.IKAnalyzer"/><analyzer type="query" useSmart="true" class="org.wltea.analyzer.lucene.IKAnalyzer"/></fieldType>
  • To start tomcat, you can see the text ik participle

1. The document inserted by 3 must match the domain.

Domain, and I also call it a field, and it has a specific meaning in solr, just like columns in a database table, and we're going to make an example.

As you can see, the document I inserted has id, and of course, in solr, each record must have a unique id, which is similar to the primary key in the database. The insertion of this record is successful.

But, however, If I change the title to title1, This isn't the same as the defined field, and it's an error, as shown in the following figure

As you can see here, here's an unknown field title1.

Definition field for 1. 4 domain.

Let's take a configuration. Check it out.

<field name="id" type="string" indexed="true" stored="true" required="true" multiValued="false"/>

Learn about these properties

Name: domain name

Type: the type of the domain, which must match the type; otherwise, the error is reported

Index: whether an index is taken

Stored: whether to store

Required: is required, general only id will be set

Multivalued: if there are multiple values, if set to multiple values, the values in the inside are stored in arrays, such as the image address ( larger, inside, small, and so on ).

1. 5 configuring dynamic domain dynamicField.

Again, take a look at it.

<dynamicField name="*_i" type="string" indexed="true" stored="true" multiValued="true" />

What's the dynamic domain? it's the name of the domain, which is composed of expressions, as long as the name meets the expression.

The same identification of these properties.

Name of name: domain name; the name of the field is specified by an expression, as long as it matches this rule, you can use this domain. For example, aa_i, bb_i, 13, and so on, just meet this expression

Type: the corresponding value type, the corresponding value must satisfy the type, otherwise the error is reported.

Index: whether to index

Stored: whether to store

. Other properties to a normal domain.

1. 6 primary key domain uniquekey.

Give a configuration


Specify a unique primary key, each of which should have a unique primary key, and this value shouldn't be changed

1. 7 replication domain copyfield.

Give a configuration

<copyField source="cat" dest="text"/>

Explain the corresponding attributes.

Source: domain domain

Dest: target domain

Copy domain to copy contents of source domain to destination domain

Note: the target domain must be, as follows, the nultivalued must be true, because more than one source domain might correspond to a target domain, so it needs to be stored in an array

<field name="text" type="string" indexed="true" stored="true" multiValued="true"/>

Type fieldtype of 1. 8 domain.

The same gives a bit of configuration, which is a bit more complicated.

<fieldType name="text_general" class="solr.TextField" positionIncrementGap="100"><analyzer type="index"><tokenizer class="solr.StandardTokenizerFactory"/><filter class="solr.StopFilterFactory" ignoreCase="true" words="stopwords.txt"/><!-- in this example, we will only use synonyms at query time
 <filter class="solr.SynonymFilterFactory" synonyms="index_synonyms.txt" ignoreCase="true" expand="false"/>
 --><filter class="solr.LowerCaseFilterFactory"/></analyzer><analyzer type="query"><tokenizer class="solr.StandardTokenizerFactory"/><filter class="solr.StopFilterFactory" ignoreCase="true" words="stopwords.txt"/><filter class="solr.SynonymFilterFactory" synonyms="synonyms.txt" ignoreCase="true" expand="true"/><filter class="solr.LowerCaseFilterFactory"/></analyzer></fieldType>

A description of the corresponding attributes

Name of name: domain

Class: specifies the type of solr

Analyzer: configuration of

Type: index ( index word ), query ( query ),

Tokenizer: configuring

Filter: filters

Actual configuration of 1. 9 business fields.

Through the above study, almost learned about some common configurations, now we use field to configure actual business fields, with attributes as follows

Of course, chinese word segmentation is still used because we've already configured a fieldtype chinese word segmentation in the previous 1.2 chapters, so we're all

A primary key id doesn't need to be configured, by default, the id is configured as a primary key, and the default configuration is as follows

<field name="id" type="string" indexed="true" stored="true" required="true" multiValued="false"/>

Trade name ( need to be stored, need to be stored )

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

Commodity classification ( no word segmentation required )

<field name="catalog" type="int" indexed="false" stored="true"/>

Category name ( need to be stored, need to be stored )

<field name="catalog_name" type="text_ik" indexed="true" stored="true"/>

(, )

<field name="price" type="double" indexed="false" stored="true"/>

Product description ( requires segmentation, no storage required )

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

Product picture ( no word segmentation required )

<field name="picture" type="string" indexed="false" stored="true"/>

Application of replication domain

We've seen the replication domain, but don't know its application scenario, and now we're considering the replication domain.

When the user searches for a search box, it's possible to enter a trade name, or you may enter a description of the commodity, and it's possible to enter a type of product

Define a target domain first

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

Copy domain, trade name, product description, item type name to above field

<copyField source="name" dest="keywords"/><copyField source="catalog_name" dest="keywords"/><copyField source="description" dest="keywords"/>

1. 10 dataimport import database data.

Solr defaults to not open the dataimport feature, so we've to configure it to open it.

  • First find solr5. 5/dist/solr-dataimporthandler-5. 5. 2. Jar, copy this file to tomcat web app solr web inf lib, and find the driver package for the appropriate database, also. I'm using the MySQL driver.
  • Find solr5. 5/example/example-DIH/solr/db/conf/db-data-config. Xml, copy it to solrhome core1 conf, and rename the data config. Xml.
  • Find the solrhome core1 conf solrconfig. Xml, and open it, add a piece of content inside it, as follows.
<requestHandler name="/dataimport" class="solr.DataImportHandler"><lst name="defaults"><str name="config">data-config.xml</str></lst></requestHandler>
  • Open and edit the data config. Xml, the complete profile as follows.
  • 复制代码
<dataConfig><!-- 这是mysql的配置,学会jdbc的都应该看得懂 --><dataSource driver="com.mysql.jdbc.Driver" url="jdbc:mysql://localhost:3306/solr/useUnicode=true&characterEncoding=utf-8" user="root" password="密码"/><document><!-- name属性,就代表着一个文档,可以随便命名 --><!-- query是一条sql,代表在数据库查找出来的数据 --><entity name="product" query="select * from products"><!-- 每一个field映射着数据库中列与文档中的域,column是数据库列,name是solr的域(必须是在managed-schema文件中配置过的域才行) --><field column="pid" name="id"/><field column="name" name="product_name"/><field column="catalog" name="product_catalog"/><field column="catalog_name" name="product_catalog_name"/><field column="price" name="product_price"/><field column="description" name="product_description"/><field column="picture" name="product_picture"/></entity></document></dataConfig>
  • Restart tomcat, and then see the following page
  • Click on the blue button to start importing, and the import process will vary depending on the amount of quantity you need, and you can click refresh status on the right to refresh the status of the current import.
  • Import succeeded as follows

Two: the use of solrj.

Code address https://github. com/zxj19951029/useSolrj.

The above section has been finished with solr 's installation and configuration, and now it's a java client that you can use solrj to maintain solr 's index and operatio

First introduction of maven 's dependencies, the solrj version number to correspond to solr 's version number


2. 1 increase and modify.

First of all, in solr, Adding and modifying is the same thing, and when this id doesn't exist, it's added when the id exists

The code is good to understand.

private String serverUrl ="";
 /** * 增加与修改<br>
 * 增加与修改其实是一回事,只要id不存在,则增加,如果id存在,则是修改
 * @throws IOException 
 * @throws SolrServerException 
 */ @Test
 publicvoid upadteIndex() throws SolrServerException, IOException{
 //HttpSolrServer server = new HttpSolrServer("");
 //创建 HttpSolrClient client = new HttpSolrClient(serverUrl);
 SolrInputDocument doc = new SolrInputDocument();

2. 2 delete index.

The deleted code is also given, and the comments inside the code can.

/** * 删除索引
 * @throws Exception
 */ @Test
 publicvoid deleteIndex()throws Exception{
 HttpSolrClient client = new HttpSolrClient(serverUrl);
 //1.删除一个 client.deleteById("zxj1");
 //2.删除多个 List<String> ids = new ArrayList<>();
 //3.根据查询条件删除数据,这里的条件只能有一个,不能以逗号相隔 client.deleteByQuery("id:zxj1");
 //4.删除全部,删除不可恢复 client.deleteByQuery("*:*");
 //一定要记得提交,否则不起作用 client.commit();

2. 3 queries.

The query is slightly more complex, but consistent with the interface

Here's a description of some of the queries, and it's recommended that people feel a good http://blog. Csdn. net/gufengshanyin/article/details/21098879.

  • Q string, if query all *: * ( id: 1 )
  • Fq filter query filter query, filtering criteria, based on query results
  • Fl - specifies that the field contents are returned, separated by commas or spaces.
  • Start - page breaks
  • Rows query data
  • Sort, format: Sort = <field name> + <desc | asc> [, <field name> + <]. Example: ( score desc, price asc ) represents first"score","price","price", the default is the correlation descending order.
  • Wt ( writer type ) specifies the output format, with xml, json, php, phps.
  • A * indicates that the index displays those fields ( represents all fields, and if you want to query the specified field, you're separated by a comma or a space, for example: Name, sku, shortdescription, or name sku shortdescription 【 note: Field is a 】 )
  • Q. Op represents the logical operation and ( and ) or ( or ) of each condition of a query statement in q.
  • Whether is highlighted, such as pk = true
  • Pk. Highlight field, name. Wet =, sku.
  • Hl: default is 1, which is set to 3 fragments
  • Simple. Pre highlighting the preceding format.
  • The format of. Simple. Post highlighting.
  • Whether the facet starts statistics
  • Facet. Field statistics field.

1.: specify a field to specify a value, such as returning to all values *: *.
2."?"represents a wildcard for a single character.
3. * represents a wildcard for multiple characte & ( cannot begin with a * or a symbol in a retrieved item ).
4. ~ represents a fuzzy retrieval, such as an item that retrieves a spelling similar to that of roam: A roam ~ will find the words that are named foam and roams; A roam ~ 0. 8 to retrieve records that return similar similarity in 0. 8.
5. Adjacent retrieval, such as"apache"and"dalton","jakarta","jakarta"10, for example.
6."^"controls relevance retrieval, such as retrieving jakarta, and trying to make"jakarta"more relevance, then add"^"symbol and increment value, namely jakarta.
7. Boolean operator and, | |.
8. Boolean operator or, & amp; & amp.
9. Boolean operator not,!, - ( excluding operato & cannot be used separately with items ).
A 10. + exists operator, requiring a symbol"+"to be present in the corresponding domain of the document.
11. ( ) used to form a subquery.
12. [ ] contai & range retrieval, such as retrieving a time period record, including head and end, date: [ 200707 TO 200710 ].

Give the basic code to see, just as a basic query, advanced query, you need to combine documents.

package zxj.solrj;import java.util.List;import java.util.Map;import org.apache.solr.client.solrj.SolrQuery;import org.apache.solr.client.solrj.impl.HttpSolrClient;import org.apache.solr.client.solrj.response.QueryResponse;import org.apache.solr.common.SolrDocument;import org.apache.solr.common.SolrDocumentList;import org.junit.Test;/** * 搜索
 * @author Administrator
 */publicclass IndexSearch {
 private String serverUrl ="";
 publicvoid search()throws Exception{
 HttpSolrClient client = new HttpSolrClient(serverUrl);
 //创建查询对象 SolrQuery query = new SolrQuery();
 //q 查询字符串,如果查询所有*:* query.set("q","product_name:小黄人");
 //fq 过滤条件,过滤是基于查询结果中的过滤 query.set("fq","product_catalog_name:幽默杂货");
 //sort 排序,请注意,如果一个字段没有被索引,那么它是无法排序的// query.set("sort","product_price desc");
 //start row 分页信息,与mysql的limit的两个参数一致效果 query.setStart(0);
 //fl 查询哪些结果出来,不写的话,就查询全部,所以我这里就不写了// query.set("fl","");
 //df 默认搜索的域 query.set("df","product_keywords");
 //开启高亮 query.setHighlight(true);
 //高亮域 query.addHighlightField("product_name");
 //前缀 query.setHighlightSimplePre("<span style='color:red'>");
 //后缀 query.setHighlightSimplePost("</span>");
 //执行搜索 QueryResponse queryResponse = client.query(query);
 //搜索结果 SolrDocumentList results = queryResponse.getResults();
 //查询出来的数量long numFound = results.getNumFound();
 System.out.println("总查询出:" + numFound +"条记录");
 //获取高亮信息 Map<String, Map<String, List<String>>> highlighting = queryResponse.getHighlighting();
 for (SolrDocument solrDocument : results) {
 System.out.println("商品id:" + solrDocument.get("id"));
 System.out.println("商品名称 :" + solrDocument.get("product_name"));
 System.out.println("商品分类:" + solrDocument.get("product_catalog"));
 System.out.println("商品分类名称:" + solrDocument.get("product_catalog_name"));
 System.out.println("商品价格:" + solrDocument.get("product_price"));
 System.out.println("商品描述:" + solrDocument.get("product_description"));
 System.out.println("商品图片:" + solrDocument.get("product_picture"));
 //输出高亮  Map<String, List<String>> map = highlighting.get(solrDocument.get("id"));
 List<String> list = map.get("product_name");
 if(list!= null && list.size()> 0){

Note: no fields are indexed, and cannot be used.

Copyright © 2011 Dowemo All rights reserved.    Creative Commons   AboutUs