SOLR 6.1 Service-side tomcat build and call

Source: Internet
Author: User
Tags createindex http authentication solr

First, download

Apache solr6.1.0 Latest Version zip, unzip to generate a solr6.1.0 folder

Second, installation

1. Create a new SOLR under the D:/projects

2, copy the SOLR6.1.0/SERVER/SOLR to d:/projects/solr/, and change the folder to home, that is, d:/projects/solr/home.

3. Copy a Tomcat folder under d:/projects/and rename it as: D:/projects/server

4, copy the Solr-6.1.0\server\solr-webapp\webapp to D:\projects\solr\server\webapps, and renamed

SOLR, that is: D:\PROJECTS\SOLR\SERVER\WEBAPPS\SOLR

5, modify the Tomcat configuration file, D:\projects\solr\server\conf\server.xml:

In

Unpackwars= "true" autodeploy= "true" > Added below:

<context path= "" docbase= "Solr" reloadable= "false" crosscontext= "true" >

<environment name= "Solr/home" type= "java.lang.String" value= "D:\projects\solr\home" override= "true"/>

</Context>

Here's docbase= "SOLR", corresponding to D:\PROJECTS\SOLR\SERVER\WEBAPPS\SOLR here in the folder of SOLR;

Value= "" Above the address in article 2nd above.

6, the Solr-6.1.0.0\server\lib\ext in all the jar package to be made to Tomcat\lib (ie D:\projects\solr\server\lib\).

7. Start Tomcat--------------------------

8, enter the address in the browser: http://localhost:8080/index.html

Third, the configuration

Core Configuration

1, the D:\projects\solr\home\configsets\basic_configs folder copied to D:\projects\solr\home\, and renamed to Core1, that is D:\projects\solr\ Home\core1

2, in the browser, select Core Admin, click Add Core, enter the next content, Instancedir must be written as a modified folder name in the previous step.

Solrconfig.xml and Managed-schema two files in D:\projects\solr\home\core1\conf

SOLR Admin Interface Login permissions configuration (

Note:

Permissions configuration It is best not to configure such test code to connect to SOLR and successfully return before attempting to configure

)

1, modify the Tomcat Tomcat-user.xml,</tomcat-user> above add:

<role rolename= "Solr"/>

<user username= "admin" password= "admin" roles= "SOLR"/>

3. Modify the D:\projects\solr\server\webapps\solr\WEB-INF \web.xml in the SOLR project and add it on </web-app>:

<security-constraint>

<web-resource-collection>

<WEB-RESOURCE-NAME>SOLR lockdown</web-resource-name>

<url-pattern>/</url-pattern>

</web-resource-collection>

<auth-constraint>

<role-name>solr</role-name>

<role-name>admin</role-name>

</auth-constraint>

</security-constraint>

<login-config>

<auth-method>BASIC</auth-method>

<realm-name>Solr</realm-name>

</login-config>

3. Restart Tomcat, and then use the Admin interface to enter the username/password (admin/admin).

Client access Rights control

Created under D:\projects\solr\server\webapps: Communityserver_override.config, with the following:

<?xml version= "1.0" encoding= "Utf-8"?>

<Overrides>

<override xpath= "/COMMUNITYSERVER/SEARCH/SOLR" mode= "Change" name= "host" value= "http://localhost:8080"/>

</Overrides>

Value is the access address in the browser? Pending confirmation.

Four, Chinese word breaker

MMSEG4J Chinese word breaker

1. Download the latest version from the online maven library: Mmseg4j-core-1.10.0.jar; Mmseg4j-solr-2.3.0.jar (There are these two jars, Mmseg4j-analysis-1.9.1.jar can not be put, otherwise it will be reported Java.lang.NoSuchMethodError: Com.chenlb.mmseg4j.analysis.mmsegtokenizer.<init> (lcom/chenlb/mmseg4j/seg;) v error, Because mmseg4j-solr-2.3.0 already contains the mmseg4j-analysis). Put it under the D:\projects\solr\server\webapps\solr\WEB-INF\lib\.

2, from the HTTPS://GITHUB.COM/CHENLB/MMSEG4J-SOLR, the mmseg4j for Lucene or SOLR copy to: D:\projects\solr\home\core1\conf\ Managed-schema other <fieldtype></fieldtype> blocks behind the other side.

<field name= "Content" type= "Textcomplex" indexed= "true" stored= "true" multivalued= "true"/>

<fieldtype name= "Textcomplex" class= "SOLR. TextField "positionincrementgap=" >

<analyzer>

<tokenizer class= "Com.chenlb.mmseg4j.solr.MMSegTokenizerFactory" mode= "complex" dicpath= ". /dic "/>

</analyzer>

</fieldtype>

<fieldtype name= "Textmaxword" class= "SOLR. TextField "positionincrementgap=" >

<analyzer>

<tokenizer class= "Com.chenlb.mmseg4j.solr.MMSegTokenizerFactory" mode= "Max-word"/>

</analyzer>

</fieldtype>

<fieldtype name= "Textsimple" class= "SOLR. TextField "positionincrementgap=" >

<analyzer>

<tokenizer class= "Com.chenlb.mmseg4j.solr.MMSegTokenizerFactory" mode= "simple" dicpath= ". /dic "/>

</analyzer>

</fieldtype>

Here's the Dicpath, is the relative path,. /dic indicates that with the Core1 peer directory dic, all future custom participle logic can be placed in this directory, which needs to be created by itself.

Textcomplex complex word breaker, Textmaxword the largest word breaker, textsimple simple word breaker.

Click Core1 Analysis, enter Chinese, select Content or Textcomplex, click Analyse values, the effect is:

Select Textmaxworld and the result is:

V. Operation of SOLR using SOLRJ

1, URL can not take admin.html

2, the field in the Solrinputdocument, must have the corresponding field in the Core1/conf/managed-schema

3, the index must be created first, the query has data

4, if there is a walk HTTP protocol (using httpclient operation) has an administrative password (configuration in Web. xml), it will be reported: This request requires HTTP authentication.

Package com.test;

5, if the following related abnormal error

servlet [Default] in context with path [/SOLR] threw exception
Org.apache.solr.common.SolrException:Error processing the request. Corecontainer is either no initialized or shutting down.
At Org.apache.solr.servlet.SolrDispatchFilter.doFilter (solrdispatchfilter.java:217)
At Org.apache.solr.servlet.SolrDispatchFilter.doFilter (solrdispatchfilter.java:208)

Solution:

  You can create a Lib directory in the D:\PROJECTS\SOLR\SERVER\WEBAPPS\SOLR directory and then unpack the SOLR folder dist the jar package and Contrib\velocity\lib
The next jar is copied to the newly created Lib directory. (Note: All jars in the dist directory and contrib\velocity\lib , including subdirectories, are copied to the D:\projects\solr\server\webapps\ SOLR\lib All jars in Lib are in the same lateral lib without subdirectories)

The following Java code makes a connection query test

You need to refer to two jar files to add the following code to the project's Pom file:

<dependency>
<groupId>org.apache.solr</groupId><artifactId>solr-solrj</artifactId><version> 6.1.0</version>
</dependency>
<dependency>
<groupId>org.apache.solr</groupId><artifactId>solr-core</artifactId><version> 6.1.0</version>
</dependency>

Note: If you configure access rights, you will get an error when connecting

If you want to use the user name password in the context of the connection using the following code in connection with the need to add HTTP user name password Authentication (specific method of adding self-network search)

Another: Security configuration In addition to add user name password also need to bind allow access to IP address is localhost 127.0.0.1 or server real IP address to prevent malicious attack

Binding method Self-network search

import org.apache.solr.client.solrj.*;

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.apache.solr.common.SolrInputDocument;

import java.io.IOException;

import java.util.ArrayList;

import java.util.List;

Public class MYSOLR {

solr URL

Public Static Final String URL = "HTTP://LOCALHOST:8080/SOLR";

SOLR Applications

Public Static Final String SERVER = "Core1";//http://localhost:8080/admin.html#/core1

Pending index, query field http://localhost:80 80/solr53/#/connection1

Public Static string[] Docs = {"SOLR is a stand-alone enterprise Search Application Server",

"It provides an API interface similar to Web-service,"

"User can request via HTTP",

"Submit an XML file with a certain format to the search engine server to generate an index",

"A lookup request can also be made via an HTTP get operation,"

"And get the return result in XML format"};

Public Static Solrclient getsolrclient () {

System. out. println (URL+ "/" +SERVER);

return New Httpsolrclient (URL+ "/" +SERVER);

}

/**

* New Index

*/

Public Static void CreateIndex () {

Solrclient client = getsolrclient();

int i = 0;

list<solrinputdocument> docList = new arraylist<solrinputdocument> ();

for (String str: docs) {

Solrinputdocument doc = new solrinputdocument ();

Doc.addfield ("id", i++);

Doc.addfield ("content", str);//need to have corresponding field in core1/conf/managed-schema

Doclist.add (DOC);

}

Try {

Client.add (docList);

Client.commit ();

} catch (Solrserverexception e) {

E.printstacktrace ();

} catch (IOException e) {

E.printstacktrace ();

}

};

/**

* Search

*/

Public Static void Search () {

Solrclient client = getsolrclient();

Solrquery query = new solrquery ();

Query.setquery ("Content: Search");

Queryresponse response = null;

Try {

Response = client.query (query);

System. out. println (Response.tostring ());

System. out. println ();

Solrdocumentlist docs = Response.getresults ();

System. out. println ("Number of documents:" + docs.getnumfound ());

System. out. println ("Query time:" + response.getqtime ());

for (Solrdocument Doc:docs) {

System. out. println ("ID:" + doc.getfieldvalue ("id") + "content:" + doc.getfieldvalue ("content");

}

} catch (Solrserverexception e) {

E.printstacktrace ();

} catch (IOException e) {

E.printstacktrace ();

}

}

Public Static void Main (string[] args) {

CreateIndex ();

Search ();

}

}

SOLR 6.1 Service-side tomcat build and call

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.