Solr built synchronous MySQL database

Insert picture description here

Install Solr

Create core workspace

  • Can be understood as Mysql database, there can be multiple databases under a local localhost
  • Copy the directory and modify it to a custom core, copy the server / solr / configsets / _default folder to the server / solr and customize the name to post
    Insert picture description here
  • The first copy is only the conf folder, let's take a look at the structure inside
    Insert picture description here
  • These two are the core configuration files, if not, then create your ownSame nameXml file
  • Note that Solr 5.X version below is not called managed-schema.xml but is called schema.xml file

Install IK tokenizer

  • Download the IK word breaker jar package IK word breaker depends on the Solr version number to download the corresponding IK word breaker
  • After downloading, put it in the solr-7.7.2 / server / solr-webapp / webapp / WEB-INF / lib folder
    Insert picture description here
  • Configure managed-schema.xml and add the following code to any location to enable word segmentation using IK tokenizer

Insert picture description here

  • After the installation is complete, you will test whether the installation is successful on the Solr client ...

Configuration synchronization Mysql configuration

Create / modify data-config.xml

<?xml version="1.0" encoding="UTF-8" ?> 
<dataConfig> 
<dataSource type="JdbcDataSource" 
			driver="com.mysql.jdbc.Driver" 
			url="jdbc:mysql://127.0.0.1:3306/nsi_database?useSSL=false&amp;serverTimezone=UTC&amp;tinyInt1isBit=false" 
			user="root" 
			password="123456"/> 
<document>

    <entity name="nsi_post_category_item" query="SELECT * FROM `nsi_post_category_item` WHERE is_check = 1">
        <field column="item_id" name="itemId"/> 
        <field column="title" name="title"/> 
        <field column="summary_desc" name="summaryDesc"/> 
        <field column="content" name="content"/> 
		<field column="post_type" name="postType"/> 
        <field column="post_icon" name="postIcon"/> 
        <field column="comment_num" name="commentNum"/> 
        <field column="share_num" name="shareNum"/> 
        <field column="watch_num" name="watchNum"/> 
        <field column="collect_num" name="collectNum"/> 
        <field column="tag" name="tag"/> 
        <field column="open_id" name="openId"/> 
        <field column="avatar" name="avatar"/> 
        <field column="nick_name" name="nickName"/> 
        <field column="is_check" name="isCheck"/> 
    </entity>
    
</document>
</dataConfig>
  • The dataSouce label will not be described. Anyone who has written JDBC knows what it means
  • entity name: Can be any name or entity class
    query: Is the sql statement you want to query
  • field column: It is the field in this table, it must correspond to otherwise it cannot be specified
    name: Alias ​​is for indexing and word segmentation for Solr, but it must correspond to the name in managed-schema.xml, otherwise it will fail to create index and word segmentation

Deployment managed-schema.xml

  <field name="itemId" type="pint" indexed="true" stored="true"/>
  <field name="title" type="text_ik" indexed="true" stored="true"/>
  <field name="summaryDesc" type="text_ik" indexed="true" stored="true"/>
  <field name="content" type="text_ik" indexed="true" stored="true"/>
  <field name="postType" type="text_ik" indexed="true" stored="true"/>
  <field name="postIcon" type="string" indexed="false" stored="true"/>
  <field name="commentNum" type="pint" indexed="false" stored="true"/>
  <field name="shareNum" type="pint" indexed="false" stored="true"/>
  <field name="watchNum" type="pint" indexed="true" stored="true"/>
  <field name="collectNum" type="pint" indexed="false" stored="true"/>
  <field name="tag" type="text_ik" indexed="true" stored="true"/>
  <field name="openId" type="string" indexed="false" stored="true"/>
  <field name="avatar" type="string" indexed="false" stored="true"/>
  <field name="nickName" type="string" indexed="false" stored="true"/>
  <field name="isCheck" type="pint" indexed="false" stored="true"/>
  <field name="id" type="string" multiValued="false" indexed="true" required="true" stored="true"/>
  • Add to any location, the name of the last configuration file corresponds to the name of this configuration file
  • type: Declare this field type (Solr has pint, plong, string, text_ *) and many other types, only list the most commonly used,You can also specify the type of tokenizer
  • indexed: Whether to create index true / false
  • stored: Whether to display true / false in the index library
  • id: If your data table has this field, you can replace the id of Solr, otherwise Solr will automatically generate a uuid string and store it in this id

Start Solr

  • Enter the DOS command line under the solr-7.7.2 / bin folder
  • solr start
  • solr stop -all close solr (there are many commands, Baidu)

Insert picture description here

  • Seeing this prompt is successful startup
  • Visit localhost: 8983 to enter the Solr interface
    Insert picture description here

Add core configuration

  • The first time you need to specify the core's working interval, that is, the configuration file just modified
    Insert picture description here
  • name: Generally consistent with the core folder name
  • instanceDIr: The core file path is copied by itself
  • dataDir: Create a data folder under the post folder and specify the path
  • config: In your core / conf / solrconfig.xml file path
  • schema: In your core / conf / managed-schema.xml file path
  • Add Core directly after adding the above path
    Insert picture description here

Synchronize Mysql data to Solr index library

Detect IK tokenizer installation

  • Find the core just defined
    Insert picture description here
  • Test whether the IK tokenizer is installed successfully

Insert picture description here

  • If you can find the representative IK tokenizer installed successfully
  • Just type a sentence and you can see that the IK tokenizer will segment the sentence for us, and then use these words or words to search

Synchronous Data

Insert picture description here

  • Open the dataImport tab
  • See the Configuration configuration on the right is the configuration in our data-config.xml
  • Excute Click Import Refresh to refresh
    Insert picture description here
  • Import succeeded
  • See if the index library has data
    Insert picture description here
  • Enter the Query panel on the left
  • Solr's query statement can be Baidu
  • numFound is the total number, the parameter items inside are my custom aliases

Handwriting is not easy, but it can help you, please like it! ! !
If you need to reprint, please indicate the source, give a rose, and leave the fragrance

Published 24 original articles · praised 33 · visits 2391

Guess you like

Origin blog.csdn.net/weixin_41241629/article/details/104231566