Solr7-4 to learn and use

Reason for learning:
17 years, when learning used lucene and solr, but later forgotten, and recently the company has a project to require the use of full-text search, just follow the way also to learn about, use the version of Solr7.4,
Here Insert Picture Description
Solr after extracting the directory structure:
Here Insert Picture Description
each folder inside the content:
Here Insert Picture Description
Solr is no longer needed after tomcat from version 5, use the built jetty start.
Below officially started using Solr:

  • Start solr
    because now use the built-in server solr, we just need to start it from the command. Switch to the bin directory.
    Here Insert Picture Description
    shift + right Here Insert Picture Description
    appears black window, enter solr start
    Here Insert Picture Description
    Here Insert Picture Description
    Here Insert Picture Description
  • Configuration Solr core (can be understood as the database solr)
    configured core There are two ways one is the official recommendation, one is created in the admin page
    (1) created by Core Admin
    Here Insert Picture Description
    Here Insert Picture Description
    thus created will complain. You can see the error can not be looking to play solrconfig.xml file. Note here under: instanceDir and dataDir created the need exists, that we need to go to create a directory in solr-7.4.0 \ server \ solr directory

Conf file in this directory we can copy from server \ solr \ configsets \ sample_techproducts_configs in
Here Insert Picture Description
Here Insert Picture Description
this way will be able to go to new
Here Insert Picture Description
Here Insert Picture Description
(2) official recommended
    using the command solr create -c test
Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description

  • Configuring IK word is
    Here Insert Picture Description
    labeled jar copied to the \ server \ solr-webapp \ webapp
    Here Insert Picture Description
    \ WEB-INF \ lib and then in the server \ solr-webapp \ webapp \ WEB-INF folder below to create a classes folder to copy the above mark go

Find Core (yangk) below conf just created to open the managed-schema add the following code:

<fieldType name="yangk_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> 

Here we found no schema.xml. This is because Solr version (before Solr5), creating the core when, Solr automatically created schema.xml, but in a later version, the new features added to the dynamic schema update, the default does not find schema.xml arrived, after Solr5, this is not the default schema file to generate good, and it was given a name managed-schema
Here Insert Picture Description
here want to see the word configuration, you need to reboot under solr command: solr restart -p port numbers restart solr service

  • Solr integration of Mysql
    integration of Mysql definitely need Mysql package used here is 8.0, mysql package will put solr-7.4.0 \ server \ solr- webapp \ webapp \ WEB-INF \ lib below
    Here Insert Picture Description
    and then to solr-7.4. 0 \ dist file found below under
    Here Insert Picture Description
    the two packages also put solr-7.4.0 \ server \ solr- webapp \ webapp \ WEB-INF \ lib below
      in order to distinguish, I create a new croe named mysql, and then find solr -7.4.0 \ example \ example-DIH \ solr \ db folder
    Here Insert Picture Description

 Copy solr-7.4.0 \ example \ example- DIH \ solr \ db inside the file contents to a folder inside mysql
Here Insert Picture Description
enter conf found inside the db-data-config.xml modify the configuration file, change your database information

<dataConfig>
    <dataSource driver="com.mysql.cj.jdbc.Driver" url="jdbc:mysql://localhost:3306/springboot?useUnicode=true&amp;characterEncoding=utf-8&amp;serverTimezone=UTC" user="root" password="root" />
    <document>
        <entity name="item" query="select id,name from sys_area">
           <field column="id" name="id" />
             <field column="name" name="name" />
         </entity>
        
    </document>
</dataConfig>

DataSource:数据库连接信息
  Entity:对应数据库的数据表
  Field:数据库字段,对应于solr的schema.xml中的 field 字段。其中 column 表示数据库字段名,name 表示 field 的 name。

Then find solrconfig.xml requestHandler configuration
Here Insert Picture Description
and then find the managed-schema, and the configuration word index field
Here Insert Picture Description
Note: field corresponding to field node node db-data-import.xml in which their name attribute consistency. If you want to use Ik query, you can set the type attribute to mysql_ik type. But because the managed-schema field id and name already exists, so when I configured the error. If the managed-schema already do not need to configure. As long as there is no configuration of the field on the line.
  This time the configuration is successful you can import index
Here Insert Picture Description
this time the index database to import was successful
Here Insert Picture Description
Here Insert Picture Description

  • Use SolrJ
    Maven configuration package SolrJ
 <dependency>
            <groupId>org.apache.solr</groupId>
            <artifactId>solr-solrj</artifactId>
            </dependency>

java code

public class SolrjDrmo {
    // 这个是solr索引库的连接地址
    private static final String URL = "http://localhost:8983/solr/mysql";

    public static void main(String[] args) throws SolrServerException, IOException {
        // 创建solr客户端连接
        HttpSolrClient hsc = new HttpSolrClient.Builder(URL).build();
        // 创建查询对象
        SolrQuery query = new SolrQuery();
        query.setQuery("*:*");// 设置查询全部数据的条件
        /* query.setQuery("name:江苏省"); */ // 列名:值

        List<Map<String, Object>> list = getSolrQuery(hsc, query);
        if (list == null) {
            System.out.println("没有数据");
            return ;
        }
        for (Map<String, Object> map : list) {
            Iterator<String> it = map.keySet().iterator();
            while (it.hasNext()) {
                String key = it.next();
                Object value = map.get(key);
                System.out.println(key + "," + value);
            }
            System.out.println("                 ");
        }
    }

    public static List<Map<String, Object>> getSolrQuery(HttpSolrClient client, SolrQuery query)
            throws SolrServerException, IOException {
        List<Map<String, Object>> list = null;
        // 执行查询并返回结果
        QueryResponse resp = client.query(query);
        SolrDocumentList results = resp.getResults();
        // 获取查询到的数据总量
        long numFound = results.getNumFound();
        // 判断总量是否大于0,
        if (numFound <= 0) {
            // 如果小于0,表示未查询到任何数据,返回null
            return null;
        } else {
            // 如果大于0,表示有数据
            // 创建list存储每条数据
            list = new ArrayList<>();
            // 遍历结果集
            for (SolrDocument doc : results) {
                // 得到每条数据的map集合
                Map<String, Object> map = doc.getFieldValueMap();
                // 添加到list
                list.add(map);
            }
            // 返回list集合
            return list;
        }
    }
}

Here Insert Picture Description

IK word's Download: https://files.cnblogs.com/files/yangk1996/ikanalyzer-solr6.5.zip

Guess you like

Origin www.cnblogs.com/yangk1996/p/12657671.html