solr的简单使用

1、解压solr文件     tar -zxvf solr-4.10.4.tgz

2、启动

    一、启动方式一    

    前台启动    cd  solr-4.10.4\example      java -jar start.jar  java

    中止  只能kill掉solr进程node

    后台启动:java -DSTOP.PORT=8984 -DSTOP.KEY=solr -jar start.jar --daemon &web

    中止:java -DSTOP.PORT=8984 -DSTOP.KEY=solr -jar start.jar --stopapache

    STOP.PORT和STOP.KEY还能够去solrui界面的java properties栏目中找bootstrap

二、启动方式二

启动    solr-4.10.4/bin/solr startapp

中止  只能kill掉solr进程webapp

solr命令讲解(solr-4.10.4/bin/solr)

start, stop, restart, healthcheckui

solr start -helpspa

solr start [-f]:启动solr,默认后台运行.net

solr start -p <port>:指定solr实例端口

solr start -s <dir>:指定solr的solr.solr.home

solr start -d <dir>:指定solrweb项目根路径(项目根路径下必须有webapps/solr.war)

solr -p <port> -V:查看指定solr的运行基本信息

solr -i:查看有多少solr服务正在运行

solr stop -p <port>:中止运行在指定端口的solr服务

solr stop -all:中止全部solr服务

solr COMMAND --help:查看指定命令的帮助文档

solr脚本底层也是调用的start.jar文件

3、solr查询界面功能

4、solr的检索运算符

1. “:” 指定字段查指定值,如返回全部值*:*

2. “?” 表示单个任意字符的通配

3. “*” 表示多个任意字符的通配

4. 布尔操做符AND、&&

5. 布尔操做符OR、||

6. [] 包含范围检索,如检索某价格区间,包含头尾,price:[0 TO 100],TO 区分大小写

7. {} 不包含范围检索,如检索某价格区间,不包含头尾price:{0 TO 100}

5、solr集成IKAnalyzer

一、把IKAnalyzer2012FF_u1.jar包导入solr-web应用下的lib目录下

    如solr-4.10.4/example/solr-webapp/webapp/WEB-INF/lib

二、把IKAnalyzer.cfg.xml和stopword.dic文件放置到solr-web类路径下

如 solr-4.10.4/example/solr-webapp/webapp/WEB-INF/classes

若是没有classes文件 本身建立

三、修改schema.xml文件  

<!--配置IK分词器-->

    <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>

四、在想要添加使用IKAnalyzer的字段的type加上text_ik

如  <field name="name" type="text_ik" indexed="true" stored="true"/>

6、在IKAnalyzer中添加本身的分词库

一、新建一个分词库.dic的文件   如my.dic    该文件保存格式必须是UTF-8无BOM格式

二、把my.dic文件放置到solr-web类路径下

如 solr-4.10.4/example/solr-webapp/webapp/WEB-INF/classes

三、修改IKAnalyzer.cfg.xml 添加内容  如 <entry key="ext_dict">my.dic;</entry>

7、用java操做solr

package cn.crxy.solr_8;

import static org.junit.Assert.*;

import java.util.Collection;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.impl.CloudSolrServer;
import org.apache.solr.client.solrj.impl.HttpSolrServer;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.client.solrj.response.SpellCheckResponse;
import org.apache.solr.client.solrj.response.SpellCheckResponse.Collation;
import org.apache.solr.client.solrj.response.SpellCheckResponse.Correction;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrDocumentList;
import org.apache.solr.common.SolrInputDocument;
import org.junit.Test;

public class SolrTest {
    String baseURL = "http://192.168.1.170:8983/solr/collection1";
    HttpSolrServer solrServer = new HttpSolrServer(baseURL );
    
    /**
     * 查询
     * @throws Exception
     */
    @Test
    public void test1() throws Exception {
        SolrQuery params = new SolrQuery();
        params.set("q", "*:*");
        QueryResponse response = solrServer.query(params);
        SolrDocumentList results = response.getResults();
        //获取数据总条数
        //long numFound = results.getNumFound();
        
        long numFound = results.size();
        System.out.println("一共:"+numFound);
        
        
        for (SolrDocument solrDocument : results) {
            Collection<String> fieldNames = solrDocument.getFieldNames();
            for (String field : fieldNames) {
                System.out.println(field+"--"+solrDocument.get(field));
            }
            System.out.println("====================================");
        }
    }
    
    /**
     * 创建索引1
     * @throws Exception
     */
    @Test
    public void test2() throws Exception {
        SolrInputDocument docs = new SolrInputDocument();
        docs.setField("id", "4");
        docs.setField("name", "crxy");
        solrServer.add(docs );
        //solrServer.commit(true,true,true);
    }
    
    /**
     * 创建索引2
     * @throws Exception
     */
    @Test
    public void test3() throws Exception {
        Person person = new Person();
        person.setId("2");
        person.setName("zs");
        person.setAge("10");
        solrServer.addBean(person);
        solrServer.commit();
    }
    
    /**
     * 删除
     * @throws Exception
     */
    @Test
    public void test4() throws Exception {
        solrServer.deleteById("1");
        //TODO--根据查询条件查询数据
        
        solrServer.commit(true,true,true);
    }
    
    /**
     * 拼写检查
     * @throws Exception
     */
    @Test
    public void test5() throws Exception {
        SolrQuery solrQuery = new SolrQuery();
        solrQuery.set("qt", "/spell");
        solrQuery.set("q", "name:crxx");
        
        QueryResponse response = solrServer.query(solrQuery);
        SolrDocumentList results = response.getResults();
        long numFound = results.getNumFound();
        if(numFound==0){
            System.out.println("拼写错误");
            SpellCheckResponse spellCheckResponse = response.getSpellCheckResponse();
            List<Collation> collatedResults = spellCheckResponse.getCollatedResults();
            for (Collation collation : collatedResults) {
                long numberOfHits = collation.getNumberOfHits();
                System.out.println("推荐词语的个数:"+numberOfHits);
                
                List<Correction> misspellingsAndCorrections = collation.getMisspellingsAndCorrections();
                for (Correction correction : misspellingsAndCorrections) {
                    String source_data = correction.getOriginal();
                    String current_data = correction.getCorrection();
                    System.out.println("原始:"+source_data+"--推荐:"+current_data);
                }
            }
        }else{
            for (SolrDocument solrDocument : results) {
                Collection<String> fieldNames = solrDocument.getFieldNames();
                for (String field : fieldNames) {
                    System.out.println(field+"--"+solrDocument.get(field));
                }
                System.out.println("====================================");
            }
        }
    }
    
    
    /**
     * 使用solrj操做solrcloud
     * @throws Exception
     */
    @Test
    public void test6() throws Exception {
        
        String zkHost = "192.168.1.170:2181";
        CloudSolrServer solrServer = new CloudSolrServer(zkHost );
        solrServer.setDefaultCollection("collection1");
        SolrQuery solrQuery = new SolrQuery();
        solrQuery.set("q", "*:*");
        QueryResponse response = solrServer.query(solrQuery);
        SolrDocumentList results = response.getResults();
        System.out.println(results.size());   
    }

}
8、SolrCloud 搭建

一、搭建一个zookeeper集群

参考 https://my.oschina.net/xiaozhou18/blog/787132

二、把 solr-4.10.4 复制到其余几台机器上

三、分别启动

在第一台机器 上启动        命令java  -DzkHost=node22:2181,node33:2181,node44:2181 -DnumShards=2 -Dbootstrap_confdir=./solr/collection1/conf -Dcollection.configName=myconf -jar start.jar
在第二台、第三台、第四台机器上 分别执行命令 java   -DzkHost=node22:2181,node33:2181,node44:2181 -jar start.jar     

参数解释

-Djetty.port:指定jetty的端口

-DzkHost:指定zookeeper的地址

-DnumShards=2 :分片的个数

-Dbootstrap_confdir=./solr/collection1/conf:上传配置文件

-Dcollection.configName=myconf :为配置文件起一个名称

出现以下界面表示启动成功

相关文章
相关标签/搜索