Construction and Use of solr--Day6

First: Solr's Construction

1: Download

http://archive.apache.org/dist/lucene/solr/

2: Installation steps

2.1: Install Tomcat first

https://blog.csdn.net/qq_36297434/article/details/83928241

2.2: Unzip Solr

tar -xvf solr-4.10.3.tgz.tar

2.3: Enter the directory

cd solr-4.10.3/example/webapps/

2.4: Copy the war directory below to Tomcat's webapps

cp solr.war /opt/module/apache-tomcat-7.0.68/webapps/

2.5: Unzip solr.war

mkdir solr && unzip solr.war -d solr && rm -rf solr.war

2.6: Modify the web.xml of the solr project

vim /opt/module/apache-tomcat-7.0.68/webapps/solr/WEB-INF/web.xml
//Find the <env-entry> tag, unscramble the comment, and modify it to read:
<env-entry>
       <env-entry-name>solr/home</env-entry-name>
       <env-entry-value>/opt/module/solr-4.10.3/example/solr</env-entry-value>
       <env-entry-type>java.lang.String</env-entry-type>
</env-entry>

2.7: Copy the jar package to solr under Tomcat

cd /opt/module/solr-4.10.3/example/lib/ext && cp * /opt/module/apache-tomcat-7.0.68/webapps/solr/WEB-INF/lib

2.8: Start tomcat

/opt/module/apache-tomcat-7.0.68/bin/startup.sh

2.9: Browser access

http://IP:8080/solr

Two: install the Chinese word segmentation device.

1: Download: IK Analyr 2012 FF_hf1.zip

2: decompression

3: Install, copy IKAnalyr2012 FF_u1.jar to the lib directory of solr project under Tomcat

cp IKAnalyzer2012FF_u1.jar /opt/module/apache-tomcat-7.0.68/webapps/solr/WEB-INF/classes

4: Create folders

mkdir /opt/module/apache-tomcat-7.0.68/webapps/solr/WEB-INF/classes

Copy IKAnalyr. cfg. XML and stopword.dic to the newly created classes directory.

5: Modify solr's schema file

/opt/module/solr-4.10.3/example/solr/collection1/conf/schema.xml
#Add the following configuration
<fieldType name="text_ik" class="solr.TextField">
	<analyzer type="index" isMaxWordLength="false" class="org.wltea.analyzer.lucene.IKAnalyzer"/>
	<analyzer type="query" isMaxWordLength="true" class="org.wltea.analyzer.lucene.IKAnalyzer"/>
</fieldType>

Three: Java API operation Solr

1: ordinary

package demo1;

import java.io.IOException;

import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServer;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.HttpSolrServer;
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 org.apache.solr.common.params.ModifiableSolrParams;

public class Demo1 {
	public static void testADD() throws SolrServerException, IOException {
		//Instantiate solr objects
		SolrServer solrServer=new HttpSolrServer("http://192.168.206.11:8080/solr/collection1");
		SolrInputDocument doc1=new SolrInputDocument();
		doc1.setField("id","1001");
		doc1.setField("name","iphone6s Mobile phone");
		doc1.setField("price", "6000");
		doc1.setField("url", "image/001.jpg");
		
		SolrInputDocument doc2=new SolrInputDocument();
		doc2.setField("id","1002");
		doc2.setField("name","Samsung s6 Mobile phone");
		doc2.setField("price", "5300");
		doc2.setField("url", "image/002.jpg");
		//Setting up the server to save information
		solrServer.add(doc1);
		solrServer.add(doc2);
		solrServer.commit();
	}
	
	public static void testSearch() throws SolrServerException {
		SolrServer solrServer=new HttpSolrServer("http://192.168.206.11:8080/solr/collection1");
		//Query classes, both SolrQuery and Modifiable SolrParams are available
		SolrQuery solrQuery=new SolrQuery();
		
		//Query keywords
		solrQuery.set("q", "name:Mobile phone");
		//Index location to start query
		solrQuery.set("start", 0);
		//Query number
		solrQuery.set("rows","20");
		//Price descending order
		solrQuery.set("sort", "price desc");
		//Query data
		QueryResponse response=solrServer.query(solrQuery);
		//Fetch data
		SolrDocumentList solrDocumentList=response.getResults();
		long num=solrDocumentList.getNumFound();
		System.out.println("Number:"+num);
		for(SolrDocument sd : solrDocumentList) {
			String id=(String)sd.get("id");
			String name=(String)sd.get("name");
			Float price=(Float)sd.get("price");
			String url=(String)sd.get("url");
			System.out.println("id:"+id);
			System.out.println("name:"+name);
			System.out.println("price:"+price);
			System.out.println("url:"+url);
			System.out.println("*************************************");
		}
	}
	
	public static void testDel() throws SolrServerException, IOException {
		//Instantiate solr objects
		SolrServer solrServer=new HttpSolrServer("http://192.168.206.11:8080/solr/collection1");
		//deleteById form
		//solrServer.deleteById("1001");
		//deleteByQuery form
		//solrServer.deleteByQuery("id:1001 OR id:1002");
		solrServer.deleteByQuery("id:*");
		solrServer.commit();
	}
	public static void testQueryCase() throws SolrServerException {
		SolrServer solrServer=new HttpSolrServer("http://192.168.206.11:8080/solr/collection1");
		SolrQuery params=new SolrQuery();
		//AND,OR,NOT
//		params.set("q","name: mobile AND price:6000");
//		params.set("q","name: mobile OR price:6000");
//		params.set("q","name: mobile NOT price:6000");
		
		//TO condition: 6<=price<=9
//		params.set("q","name: mobile AND price:[4000 TO 6000]");
		
		//TO condition, 7 < price < 9, adding filter to improve query efficiency
//		params.set("q","price:{4000 TO 6000}");
//		params.addFilterQuery("name: mobile");
		
		//Display settings
		params.setHighlight(true);//Open the highlight component
		params.addHighlightField("name"); //Highlight field
		params.setHighlightSimplePre("<font color='red'>");//Markup, highlighting keyword prefixes
		params.setHighlightSimplePost("</font>"); //Suffix
		params.setHighlightSnippets(1);//The default number of fragments is 1.
		params.setHighlightFragsize(100); //Maximum length of each slice, default 100
		
		//Query data
		QueryResponse response=solrServer.query(params);
		//Fetch data
		SolrDocumentList solrDocumentList=response.getResults();
		long num=solrDocumentList.getNumFound();
		System.out.println("Number:"+num);
		for(SolrDocument sd : solrDocumentList) {
			String id=(String)sd.get("id");
			String name=(String)sd.get("name");
			Float price=(Float)sd.get("price");
			String url=(String)sd.get("url");
			System.out.println("id:"+id);
			System.out.println("name:"+name);
			System.out.println("price:"+price);
			System.out.println("url:"+url);
			System.out.println("*************************************");
		}	
	}
	public static void main(String[] args) throws SolrServerException, IOException {
		// TODO Auto-generated method stub
		testADD();
		testSearch();
		testQueryCase();
		testDel();
	}

}

2: bean s and annotations

package demo2.pojo;

import java.io.Serializable;

import org.apache.solr.client.solrj.beans.Field;

public class Phone implements Serializable{
	
	private static final long serialVersionUID = 1L;
	
	@Field("id")
	String id;
	@Field("name")
	String name;
	@Field("price")
	String price;
	@Field("url")
	String url;
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getPrice() {
		return price;
	}
	public void setPrice(String price) {
		this.price = price;
	}
	public String getUrl() {
		return url;
	}
	public void setUrl(String url) {
		this.url = url;
	}
	@Override
	public String toString() {
		// TODO Auto-generated method stub
		return("id:"+id+"  name:"+name+"  price:"+price+"  url:"+url);
	}
	
}
package demo2;

import java.io.IOException;

import org.apache.solr.client.solrj.SolrServer;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.HttpSolrServer;
import org.apache.solr.common.SolrDocument;

import demo2.pojo.Phone;

public class Demo2 {
	public static void testAddPhone() throws IOException, SolrServerException {
		SolrServer solrServer=new HttpSolrServer("http://192.168.206.11:8080/solr/collection1");
		Phone p1=new Phone();
		Phone p2=new Phone();
		p1.setId("1001");
		p1.setName("Apple 6 s Mobile phone");
		p1.setPrice("6000");
		p1.setUrl("image/001.jpg");
		
		p2.setId("1002");
		p2.setName("Samsung s6 Mobile phone");
		p2.setPrice("5300");
		p2.setUrl("image/002.jpg");
		solrServer.addBean(p1);
		solrServer.addBean(p2);
		solrServer.commit();
	}
	
	public static void testChange() {
		SolrDocument doc=new SolrDocument();
		doc.addField("id", "1001");
		doc.addField("name", "Apple 6 s Mobile phone");
		doc.addField("price", "6000");
		doc.addField("url", "image/001.jpg");
		SolrServer solrServer=new HttpSolrServer("http://192.168.206.11:8080/solr/collection1");
	    Phone p=solrServer.getBinder().getBean(Phone.class, doc);
	    System.out.println(p);
	}
	
	public static void main(String[] args) throws IOException, SolrServerException {
		// TODO Auto-generated method stub
		//testAddPhone();
		testChange();
	}

}

IV: Configuring Clusters

Prepare 6 machines, such as IP:

192.168.56.11
192.168.56.12
192.168.56.13
192.168.56.14
192.168.56.15
192.168.56.16

1: Configure zookeeper cluster

https://blog.csdn.net/qq_36297434/article/details/98350234

2: Single-machine configuration solr

3: zookeeper connects tomcat

3.1:192.168.56.11 Machine modifies the catalina.sh file in tomcat bin directory and adds it on the second line

JAVA_OPTS="-Dbootstrap_confdir=/solrcloud/solrhome/collection1/conf -Dcollection.configName=myconf -DzkHost=192.168.56.11:2181,192.168.56.12:2181,192.168.56.13:2181,192.168.56.14:2181,192.168.56.15:2181,192.168.56.16:2181 -DnumShards=3"

3.2:192.168.56.12-16 Machine modifies the catalina.sh file in tomcat bin directory and adds it in the second line

JAVA_OPTS="-DzkHost=192.168.56.11:2181,192.168.56.12:2181,192.168.56.13:2181,192.168.56.14:2181,192.168.56.15:2181,192.168.56.16:2181"

3: Create collections

http://192.168.56.11:8080/solr/admin/collections?action=CREATE&name=guangzhou&numShards=3&replicationFactor=3

4: Select an IP access

http://192.168.56.11:8080/solr/

 

Tags: solr Apache Tomcat Mobile

Posted on Tue, 08 Oct 2019 12:47:54 -0700 by kester