Solr installation and use

A, install,

  • 1. Download the zip file from the official website
  • 2. Decompress the downloaded package to complete the Solr installation.
  • 3. Open DOS, switch to solr_home\bin, and run solr start -p 8983 to run solr on port 8983. If you see the following picture, solr is started successfully. (Solr default port is 8983)

Solr Common commands

Solr start -p p_num Starts solr solr restart -p p_num restarts solr solr stop -p p_num closes solr solr create -c c_name Creates a coreCopy the code
  • 4. In the browser visit: localhost:8983, such as some pages appear

This is the end of the solr installation.

Create the core

2.1 Preparations for Core Creation

Each core is an instance of Solr, and a Solr service can create multiple cores, each of which can be configured independently.

  • 1. Switch to the solr_home\server\solr directory, for example: E:\solr-8.1.1\server\solr, and create a folder in this directory with the same name as the core name.

    _v_images/2019062

  • 2. Copy conf folder from E:\solr-8.1.1\server\solr\configsets_default to new_core (E:\solr-8.1.1\server\solr\new_core).

2.2 Creating a Core

Method 1: Open the SOLr interface and perform operations in the sequence shown in the figure.

This method requires creating a new_core folder under E:\solr-8.1.1\server\solr

In the bin directory, run the solr create -c new_core command. (Recommended)

Third, the schema

Each core core has such a schema configuration file. The schema file can define the data type of the index base and configure whether to index and store the fields. You need to configure the data type separately. The data type of schema is sufficient. If it cannot meet the requirements, such as Chinese word segmentation and pinyin word segmentation, you can customize the word segmentation. The configuration file name of Solr8.1.1’s Schema For managed-schema,home\server\solr\new_core\conf\managed-schema (E:\solr-8.1.1\server\solr\core_issuer\conf).

3.1 Main members of the Schema

  • (1) fieldType: Defines the type of field. The primary function is to define the word segmentation, which determines how to retrieve keywords from the document.
  • (2) Analyzer: a child of fieldType, which is a word separator and consists of a tokenizer and a filter. For example,
<fieldType name="text_tr" class="solr.TextField" positionIncrementGap="100">
    <analyzer>
      <tokenizer class="solr.StandardTokenizerFactory"/>
      <filter class="solr.TurkishLowerCaseFilterFactory"/>
      <filter class="solr.StopFilterFactory" words="lang/stopwords_tr.txt" ignoreCase="false"/>
      <filter class="solr.SnowballPorterFilterFactory" language="Turkish"/>
    </analyzer>
  </fieldType>
Copy the code
  • (3) Field: field used to create an index. If this field needs to generate an index, set indexed to true and set stored to true. Such as:
<field name="age" type="pint" indexed="true" stored="true"/>
Copy the code

3.2 Adding an Index Field

  • Method 1: Modify the managed-schema configuration file (not recommended. Restart the service after the modification). For example:
<field name="age" type="pint" indexed="true" stored="true"/>
Copy the code
  • Method 2: Add a file on the Solr page. (Recommended, no need to restart the service)

! [](_v_images/20190623133627274_22114.png =811x)

3.3 Configuring the Chinese Word Segmentation Tool

  • 1. Download Chinese word analyzer IKAnalyzer, download address, password: IGt9.
  • 2. Decompress the package in the following directory:

  • 3. Copy the two JAR packages to the path: E:\solr-8.1.1\server\solr-webapp\webapp\ web-INF \lib.
  • 4. Copy three configuration files to this path: E:\solr-8.1.1\server\solr-webapp\webapp\ web-INF \classes. Create a classes folder if you don’t have one.
  • 5. Add a word segmentation to the schema.
<fieldType name="text_ik" class="solr.TextField">
  <analyzer type="index">
	  <tokenizer class="org.wltea.analyzer.lucene.IKTokenizerFactory" useSmart="false" conf="ik.conf"/>
	  <filter class="solr.LowerCaseFilterFactory"/>
  </analyzer>
  <analyzer type="query">
	  <tokenizer class="org.wltea.analyzer.lucene.IKTokenizerFactory" useSmart="true" conf="ik.conf"/>
	  <filter class="solr.LowerCaseFilterFactory"/>
  </analyzer>
</fieldType>
Copy the code
  • 6. Add custom Chinese phrases to the ext.dic file

  • Restart Solr

The effect is as follows:

Import index data (mysql data as example)

  • 1. Create the mysql database

  • 2. Create a new my-data-config. XML file under solr_home\server\solr\new_core\conf (E:\solr-8.1.1\server\solr\core_issuer\conf)
<?xml version="1.0" encoding="UTF-8" ? >
<dataConfig>
    <dataSource type="JdbcDataSource" driver="com.mysql.jdbc.Driver" url="jdbc:mysql://localhost:3306/solr_test"
                user="root" password=""/>
    <document>
        <entity name="user" query="select * from user">
            <field column="id" name="id"/>
            <field column="age" name="age"/>
            <field column="name" name="name"/>
            <field column="hobby" name="hobby"/>
        </entity>
    </document>
</dataConfig>
Copy the code
  • 3. Add index fields corresponding to database fields with solr, and open managed-schema file after adding:
<field name="name" type="string" indexed="true" stored="true"/>
<field name="age" type="pint" indexed="true" stored="true"/>
<field name="hobby" type="string" indexed="true" stored="true"/>
Copy the code

Do not add the ID field, the field already exists, adding an error will be reported

  • 4. Open the file in the directory: Solr_home/server/solr/new_core/conf/solrconfig.xml (E: solr-8.1.1 / server/solr/core_issuer/conf/solrconfig.xml), Add the following configuration to any node at the same level as requestHandler. As shown in figure:

<requestHandler name="/dataimport" class="org.apache.solr.handler.dataimport.DataImportHandler">
	<lst name="defaults">
		<str name="config">my-data-config.xml</str>
	</lst>
</requestHandler>
Copy the code
  • 5. Copy solr_home\dist (E:\solr-8.1.1\dist) from solr-dataimporthandler-8.1.1.jar and MySQL driver to solr_home\server\solr-w Ebapp \webapp\ web-INF \lib (E:\solr-8.1.1\server\solr-webapp\webapp\ web-INF \lib)
  • 6. Restart the Solr service.
  • 7. Open the Solr page and perform the following operations.

  • 8. Check whether data is imported successfully

If Chinese word segmentation is performed on the index field

When adding a pair index field, set fileType to TEXt_ik

The effect is shown in the figure: query the result of Luxi Chemical industry

Use of Java client solrJ

5.1 the import pom

<! --solr -->
<dependency>
	<groupId>org.apache.solr</groupId>
	<artifactId>solr-solrj</artifactId>
	<version>8.1.1</version>
</dependency>
Copy the code

5.2 write the model

public class User {

    @Field(value = "id")
    private String id;
    @Field(value = "name")
    private String name;
    @Field(value = "age")
    private Integer age;
    @Field(value = "hobby")
    private String hobby;

    public String getId(a) {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName(a) {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge(a) {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public String getHobby(a) {
        return hobby;
    }

    public void setHobby(String hobby) {
        this.hobby = hobby;
    }

    @Override
    public String toString(a) {
        return "Person{" +
                "id=" + id +
                ", name='" + name + '\' ' +
                ", age=" + age +
                ", hobby=" + hobby +
                '} '; }}Copy the code

5.3 Writing test code

public class Main {
    private static final String SOLR_URL = "http://localhost:8888/solr";
    private static final String CORE_NAME = "new_core";
    public static void main(String[] args) throws IOException, SolrServerException {
        HttpSolrClient client = new HttpSolrClient.Builder(SOLR_URL).withConnectionTimeout(10000).withSocketTimeout(60000).build();
        SolrQuery query = new SolrQuery();
        // Set the fields to query
        query.setFields("id"."name"."age"."hobby");
        // Query all
        QueryResponse response = client.query(CORE_NAME,query);
        // Query results
        SolrDocumentList results = response.getResults();
        System.out.println("Total result:" + results.getNumFound());
        // Iterate through the list
        System.out.println("Document result:");
        for (SolrDocument doc : results) {
            System.out.printf("[id:%s, name:%s, age:%s, hobby:%s]\n", doc.get("id"), doc.get("name"), doc.get("age"), doc.get("hobby"));
        }
        // Get the entity object
        List<User> userList = response.getBeans(User.class);
        System.out.println("To entity object: \n"+Arrays.toString(userList.toArray())); }}Copy the code

** Single query by condition: **

// Query the user whose name is Chen Long
query.set("q".Name: "Jackie chan" );
Copy the code

Filter query

// Query the user whose name is Chen Long
query.set("q".Name: "Jackie chan" );
Copy the code

Periodical real-time index reconstruction and incremental update

Preparations:

  • 1. Download the jar package: Solr-DataimPortScheduler-1.1. jar (1.0 version is available online, but the specific version of solr please find by yourself)

Note (important thing to say three times) : When solr7+ or later, the jar package will have a problem. After configuration, the server will start with an error, page 404, more details will be provided later. 支那

Personal network location sharing: link: pan.baidu.com/s/1HT9WzXQx… Extract code: EHJU

This works fine with the 7+ version

  • 2. Create a new file: dataimport.properties, and copy the following file.
################################################# # # # dataimport scheduler properties # # # ################################################# # to sync or not to sync # 1 - active; SyncEnabled =1 # which cores to schedule # ina multi-core environment you can Decide which cores you want syncronized # leave empty or comment it out if using single-core Deployment # Here's my custom core: Simple syncCores=core_issuer # solr server name or IP address # [defaults to localhost if empty Server =localhost # solr server port # [defaults to 80 if empty] Port =8983 # application name/context # [defaults to current ServletContextListener's context (app) name] # Webapp =solr # URL params [mandatory] # remainder of URL # Link requested when Solr synchronizes data (I'm using full-import here) params=/ dataimPort? command=full-import&clean=true&commit=true # schedule interval # number of minutes between two runs # [defaults to 30 if Empty] # Set the time interval for redoing the index, in minutes (7200 by default); ReBuildIndexInterval =7200 # reBuildIndexParams=/select? Qt =/dataimport&command=full-import&clean=true&commit=true First real execution time =reBuildIndexBeginTime+reBuildIndexInterval*60*1000; ReBuildIndexBeginTime =03:10:00; reBuildIndexBeginTime=03:10:00Copy the code

With this JAR package and the datAimPort.properties file ready, proceed with the following steps…

  • Step 1: copy the solr-DataimPortScheduler. jar package to E:\solr-8.1.1\server\solr-webapp\ webApp \ web-INF \lib.
  • Step 2: Add listening configuration to web.xml file in E:\solr-8.1.1\server\solr-webapp\ webApp \ web-INF:
 <listener>
   <listener-class>org.apache.solr.handler.dataimport.scheduler.ApplicationListener</listener-class>
 </listener>
Copy the code

Pay attention to the places (another note) : org. Apache. Solr. Handler. Dataimport. Scheduler. Behind the ApplicationListener cannot have Spaces, otherwise it may report errors, and it is difficult to found the problem the reason.

  • Step 3: Create a new conf folder under the \solrhome directory (same as the simple folder) and place the dataimport.properties file in the conf folder.
  • Step 4: Restart Tomcat and access Solr.

Two ways to delete an index base (delete all data)

  • 1. Delete all the data in the index library by using solr visual interface

  • 2. Use solrClient to delete all data in the index library
@Test
    public void deleteAll(a){
        try {
            searchCrawlerProductService.deleteAll();
        } catch(Exception e) { e.printStackTrace(); }}Copy the code
public RestResult deleteAll(a){
        try {
            log.debug("Delete all indexes");
 
            solrClient.deleteByQuery(: "* *");  // Set the deletion condition to "*:*"
            solrClient.commit();
 
            log.debug("All deleted successfully");
        } catch (Exception e) {
            log.debug("Failed to delete all indexes");
 
            e.printStackTrace();
        }
 
        return RestResult.success("Index library deleted successfully");
    }
Copy the code