solr学习

getting start

1)创建新用户

2)解压 运行:

bin/solr start -e cloud -noprompt

显示:

Welcome to the SolrCloud example!

Starting up 2 Solr nodes for your example SolrCloud cluster.

Creating Solr home directory /home/solr/solr-6.5.0/example/cloud/node1/solr
Cloning /home/solr/solr-6.5.0/example/cloud/node1 into
   /home/solr/solr-6.5.0/example/cloud/node2

Starting up Solr on port 8983 using command:
bin/solr start -cloud -p 8983 -s "example/cloud/node1/solr"

Solr is already setup and running on port 8983 with status:
{
  "solr_home":"/home/solr/solr-6.5.0/example/cloud/node1/solr",
  "version":"6.5.0 4b16c9a10c3c00cafaf1fc92ec3276a7bc7b8c95 - jimczi - 2017-03-21 20:47:12",
  "startTime":"2017-04-14T16:36:44.638Z",
  "uptime":"0 days, 0 hours, 6 minutes, 29 seconds",
  "memory":"59.6 MB (%12.2) of 490.7 MB",
  "cloud":{
    "ZooKeeper":"localhost:9983",
    "liveNodes":"2",
    "collections":"0"}}

If this is not the example node you are trying to start, please choose a different port.

Starting up Solr on port 7574 using command:
bin/solr start -cloud -p 7574 -s "example/cloud/node2/solr" -z localhost:9983

Solr is already setup and running on port 7574 with status:
{
  "solr_home":"/home/solr/solr-6.5.0/example/cloud/node2/solr",
  "version":"6.5.0 4b16c9a10c3c00cafaf1fc92ec3276a7bc7b8c95 - jimczi - 2017-03-21 20:47:12",
  "startTime":"2017-04-14T16:38:02.705Z",
  "uptime":"0 days, 0 hours, 5 minutes, 12 seconds",
  "memory":"37 MB (%7.5) of 490.7 MB",
  "cloud":{
    "ZooKeeper":"localhost:9983",
    "liveNodes":"2",
    "collections":"0"}}

If this is not the example node you are trying to start, please choose a different port.
INFO  - 2017-04-14 12:43:16.519; org.apache.solr.client.solrj.impl.ZkClientClusterStateProvider; Cluster at localhost:9983 ready

Connecting to ZooKeeper at localhost:9983 ...
INFO  - 2017-04-14 12:43:17.274; org.apache.solr.client.solrj.impl.ZkClientClusterStateProvider; Cluster at localhost:9983 ready
Uploading /home/solr/solr-6.5.0/server/solr/configsets/data_driven_schema_configs/conf for config gettingstarted to ZooKeeper at localhost:9983

Creating new collection 'gettingstarted' using command:
http://localhost:8983/solr/admin/collections?action=CREATE&name=gettingstarted&numShards=2&replicationFactor=2&maxShardsPerNode=2&collection.configName=gettingstarted
View Code

索引目录中的文件(xml,json,html...):

bin/post -c gettingstarted example/exampledocs/*.xml
  • -c gettingstarted: 要索引的集合名称

删除数据:bin/post -c gettingstarted -d "<delete><id>SP2514N</id></delete>"

搜索

访问客户端界面:http://localhost:8983/solr/#/gettingstarted/query

基本单项搜索:

curl "http://localhost:8983/solr/gettingstarted/select?wt=json&indent=true&q=name:Foundation"

语句搜索:

curl "http://localhost:8983/solr/gettingstarted/select?wt=json&indent=true&q=\"CAS+latency\""

联合搜索:

curl "http://localhost:8983/solr/gettingstarted/select?wt=json&indent=true&q=%2Btwo+-one"

在url中+号会被转化,所以会用%2B表示,+表示匹配two,-表示不匹配one

facets范围搜索:

curl 'http://localhost:8983/solr/gettingstarted/select?q=*:*&wt=json&indent=on&rows=0'\

'&facet=true'\

'&facet.range=price'\

'&f.price.facet.range.start=0'\

'&f.price.facet.range.end=600'\

'&f.price.facet.range.gap=50'\

'&facet.range.other=after'

 solrj

package com.hpe.solr;

import java.io.IOException;
import java.util.List;

import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.HttpSolrClient;
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;

//http://blog.csdn.net/u012385190/article/details/53115546
public class SolrTest {

    private static final String SOLR_URL = "http://192.18.80.10:8983/solr/";

    /**
     * 创建 HttpSolrClient对象 该对象有两个可以使用,都是线程安全的
     * 1、CommonsHttpSolrServer:启动web服务器使用的,通过http请求的 2、
     * EmbeddedSolrServer:内嵌式的,导入solr的jar包就可以使用了 3、solr
     * 4.0之后好像添加了不少东西,其中CommonsHttpSolrServer这个类改名为HttpSolrClient
     * 
     * @return
     */
    public HttpSolrClient createSolrServer() {
        HttpSolrClient solr = null;
        solr = new HttpSolrClient(SOLR_URL);
        return solr;
    }

    public void addDoc() throws SolrServerException, IOException {
        // 构造一篇文档
        SolrInputDocument document = new SolrInputDocument();
        // 往doc添加字段,在客户端这边添加的字段必须在服务端中有过定义
        document.addField("id", 8);
        document.addField("name", "周新星");
        document.addField("description", "一个灰常牛逼的军事家");
        // 获得一个solr服务端的请求,去提交 ,选择具体的某一个solr core
        HttpSolrClient solr = new HttpSolrClient(SOLR_URL + "my_core");
        solr.add(document);
        solr.commit();
        solr.close();
    }

    /**
     * 根据id从索引库删除文档
     */
    public void deleteDocumentById() throws Exception {
        // 选择具体的某一个solr core
        HttpSolrClient server = new HttpSolrClient(SOLR_URL + "my_core");
        // 删除文档
        server.deleteById("8");
        // 删除所有的索引
        // solr.deleteByQuery("*:*");
        // 提交修改
        server.commit();
        server.close();
    }

    /**
     * 查询
     * 
     * @throws Exception
     */
    public void querySolr() throws Exception {
        HttpSolrClient solrServer = new HttpSolrClient(SOLR_URL + "my_core/");
        SolrQuery query = new SolrQuery();
        // 下面设置solr查询参数
        // query.set("q", "*:*");// 参数q 查询所有
        query.set("q", "周星驰");// 相关查询,比如某条数据某个字段含有周、星、驰三个字 将会查询出来 ,这个作用适用于联想查询

        // 参数fq, 给query增加过滤查询条件
        query.addFilterQuery("id:[0 TO 9]");// id为0-4

        // 给query增加布尔过滤条件
        // query.addFilterQuery("description:演员"); //description字段中含有“演员”两字的数据

        // 参数df,给query设置默认搜索域
        query.set("df", "name");

        // 参数sort,设置返回结果的排序规则
        query.setSort("id", SolrQuery.ORDER.desc);

        // 设置分页参数
        query.setStart(0);
        query.setRows(10);// 每一页多少值

        // 参数hl,设置高亮
        query.setHighlight(true);
        // 设置高亮的字段
        query.addHighlightField("name");
        // 设置高亮的样式
        query.setHighlightSimplePre("<font color='red'>");
        query.setHighlightSimplePost("</font>");

        // 获取查询结果
        QueryResponse response = solrServer.query(query);
        // 两种结果获取:得到文档集合或者实体对象

        // 查询得到文档的集合
        SolrDocumentList solrDocumentList = response.getResults();
        System.out.println("通过文档集合获取查询的结果");
        System.out.println("查询结果的总数量:" + solrDocumentList.getNumFound());
        // 遍历列表
        for (SolrDocument doc : solrDocumentList) {
            System.out.println("id:" + doc.get("id") + "   name:"
                    + doc.get("name") + "    description:"
                    + doc.get("description"));
        }

        // 得到实体对象
        List<Person> tmpLists = response.getBeans(Person.class);
        if (tmpLists != null && tmpLists.size() > 0) {
            System.out.println("通过文档集合获取查询的结果");
            for (Person per : tmpLists) {
                System.out.println("id:" + per.getId() + "   name:"
                        + per.getName() + "    description:"
                        + per.getDescription());
            }
        }
    }

    public static void main(String[] args) throws Exception {
        SolrTest solr = new SolrTest();
        // solr.createSolrServer();
        solr.addDoc();
        solr.deleteDocumentById();
        solr.querySolr();
    }
}

 



 
posted @ 2017-09-21 17:57  gaojy  阅读(196)  评论(0编辑  收藏  举报