Solr API操作

1、需要在/usr/local/services/solr/solr-4.10.3/example/solr/collection1/conf的solrconfig.xml加上

<requestHandler name="/select" class="solr.SearchHandler">

<!-- default values for query parameters can be specified, these

will be overridden by parameters in the request

-->

<lst name="defaults">

<str name="echoParams">explicit</str>

<int name="rows">10</int>

<str name="df">text</str>

</lst>

<!-- 这行代码非常重要,如果没有这行,拼写检查,是不起作用的-->    

       <arr name="last-components">    

       <str>spellcheck</str>  

以便spell能够生效

2、在/usr/local/services/solr/solr-4.10.3/example/solr/collection1/conf的schema.xml修改,加上字段,否则会报错

如:

<field name="id" type="string" indexed="true" stored="true" required="true" multiValued="false" />

<field name="sku" type="text_en_splitting_tight" indexed="true" stored="true" omitNorms="true"/>

<field name="name" type="text_general" indexed="true" stored="true"/>

<field name="addr" type="text_general" indexed="true" stored="true"/>

<field name="age" type="int" indexed="true" stored="true"/>

<field name="manu" type="text_general" indexed="true" stored="true" omitNorms="true"/>

=======================================================

下面是具体的实现的方法

com.ys.controller;

com.ys.bean.Person;
org.apache.solr.client.solrj.SolrServer;
org.apache.solr.client.solrj.impl.HttpSolrServer;
org.apache.solr.common.SolrInputDocument;
org.springframework.beans.factory.annotation.;
org.springframework.web.bind.annotation.;
org.springframework.web.bind.annotation.RequestMethod;
org.springframework.web.bind.annotation.;

java.util.*;
java.util.Map.Entry;

org.apache.solr.client.solrj.SolrQuery;
org.apache.solr.client.solrj.impl.HttpSolrClient;
org.apache.solr.client.solrj.response.QueryResponse;
org.apache.solr.client.solrj.response.SpellCheckResponse;
org.apache.solr.client.solrj.response.SpellCheckResponse.Collation;
org.apache.solr.client.solrj.response.SpellCheckResponse.Correction;
org.apache.solr.common.SolrDocument;
org.apache.solr.common.SolrDocumentList;
org.apache.solr.common.SolrInputDocument;

(value = )
SolrTest {
    ()
    String ;

    ()
    String ;

    (value = , method = RequestMethod.)
    add() Exception {

        Map<String, String> map = HashMap<String, String>();
        map.put(, );
        map.put(, );
        map.put(, );
        map.put(, );
        addDocument(map, );

    }

    (value = , method = RequestMethod.)
    addbean() Exception {

        List<Person> persons = ArrayList<Person>();
        persons.add(Person(, , , ));
        persons.add(Person(, , , ));
        addDocumentByBean(persons, );

    }

    (value = , method = RequestMethod.)
    del() Exception {

        List<String> ids = ArrayList<String>();
        ids.add();
        ids.add();
        ids.add();
        deleteDocumentByIds(ids, );

    }

    (value = , method = RequestMethod.)
    search() Exception {

        getDocument();

    }

    (value = , method = RequestMethod.)
    spell() Exception {

        getSpell();

    }

    addDocument(Map<String, String> map, String core)
            Exception {
        SolrInputDocument sid = SolrInputDocument();
        (Entry<String, String> entry : map.entrySet()) {
            sid.addField(entry.getKey(), entry.getValue());
        }
        HttpSolrClient solrClient = getSolrClient(+ core);
        solrClient.add(sid);
        (solrClient);
    }

    addDocumentByBean(List<Person> persons, String core)
            Exception {
        HttpSolrClient solrClient = getSolrClient(+ core);
        solrClient.addBeans(persons);
        (solrClient);
    }

    deleteDocumentByIds(List<String> ids, String core)
            Exception {
        HttpSolrClient solrClient = getSolrClient(+ core);
        solrClient.deleteById(ids);
        (solrClient);
    }

    getDocument(String core) Exception {
        HttpSolrClient solrClient = getSolrClient(+ core);
        SolrQuery sq = SolrQuery();

        sq.set(, );

        sq.addFilterQuery();

        sq.setSort(, SolrQuery.ORDER.);

        sq.setStart();
        sq.setRows();

        sq.setHighlight();

        sq.addHighlightField();

        sq.setHighlightSimplePre();
        sq.setHighlightSimplePost();

        QueryResponse result = solrClient.query(sq);

        System..println();
        SolrDocumentList results = result.getResults();
        System..println(+ results.getNumFound() + );
        (SolrDocument solrDocument : results) {
            System..println(+ solrDocument.get());
            System..println(+ solrDocument.get());
            System..println(+ solrDocument.get());
            System..println(+ solrDocument.get());
        }

        System..println();
        List<Person> persons = result.getBeans(Person.);
        System..println(+ persons.size() + );
        (Person person : persons) {
            System..println(person);
        }
        (solrClient);
    }

    getSpell(String core) Exception {
        HttpSolrClient solrClient = getSolrClient(+ core);
        SolrQuery sq = SolrQuery();
        sq.set(, );

        sq.set(, );
        QueryResponse query = solrClient.query(sq);
        SolrDocumentList results = query.getResults();

        count = results.getNumFound();

        (count == ) {
            SpellCheckResponse spellCheckResponse = query
                    .getSpellCheckResponse();
            List<Collation> collatedResults = spellCheckResponse
                    .getCollatedResults();
            (Collation collation : collatedResults) {
                numberOfHits = collation.getNumberOfHits();
                System..println(+ numberOfHits);

                List<Correction> misspellingsAndCorrections = collation
                        .getMisspellingsAndCorrections();
                (Correction correction : misspellingsAndCorrections) {
                    String source = correction.getOriginal();
                    String current = correction.getCorrection();
                    System..println(+ current + + source);
                }
            }
        } {
            (SolrDocument solrDocument : results) {
                Collection<String> fieldNames = solrDocument.getFieldNames();

                (String field : fieldNames) {
                    System..println(+ field + + solrDocument.get(field));
                }
            }
        }

        (solrClient);
    }

    HttpSolrClient getSolrClient(String core) {
        HttpSolrClient hsc = HttpSolrClient(+ core);
        hsc;
    }

    commitAndCloseSolr(HttpSolrClient solrClient)
            Exception {
        solrClient.commit();
        solrClient.close();
    }

}
时间: 2024-08-11 09:57:37

Solr API操作的相关文章

Java API操作HDFS

HDFS是存储数据的分布式文件系统,对HDFS的操作,就是对文件系统的操作,除了用HDFS的shell命令对文件系统进行操作,我们也可以利用Java API对文件系统进行操作,比如文件的创建.删除.修改权限等等,还有文件夹的创建.删除.重命名等等. 使用Java API对文件系统进行操作主要涉及以下几个类: 1.Configuration类:该类的对象封装了客户端或者服务端的配置. 2.FileSystem类:该类的对象是一个文件系统对象,可以利用该对象的一些方法来对文件进行操作,FileSys

HBase API 操作范例

package com.test.hbase.api; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hbase.*; import org.apache.hadoop.hbase.client.*; import org.apache.hadoop.hbase.util.Bytes; import java.io.IOException; import java.util.ArrayList; imp

HBase 6、用Phoenix Java api操作HBase

开发环境准备:eclipse3.5.jdk1.7.window8.hadoop2.2.0.hbase0.98.0.2.phoenix4.3.0 1.从集群拷贝以下文件:core-site.xml.hbase-site.xml.hdfs-site.xml文件放到工程src下 2.把phoenix的phoenix-4.3.0-client.jar和phoenix-core-4.3.0.jar添加到工程classpath 3.配置集群中各节点的hosts文件,把客户端的hostname:IP添加进去

Hadoop读书笔记(三)Java API操作HDFS

Hadoop读书笔记(一)Hadoop介绍:http://blog.csdn.net/caicongyang/article/details/39898629 Hadoop读书笔记(二)HDFS的shell操作:http://blog.csdn.net/caicongyang/article/details/41253927 JAVA URL 操作HDFS OperateByURL.java package hdfs; import java.io.InputStream; import jav

HDFS基础和java api操作

1. 概括 适合一次写入多次查询情况,不支持并发写情况 通过hadoop shell 上传的文件存放在DataNode的block中,通过linux shell只能看见block,看不见文件(HDFS将客户端的大文件存放在很多节点的数据块中,Block本质上是一个逻辑概念,它是hdfs读写数据的基本单位) HDFS中,如果一个文件小于一个数据块的大小,并不占用整个数据块存储空间 2. fs 可以使用hdfs shell操作hdfs,常用 fs命令如下: eg: hadoop fs -cat fi

通过HTTP RESTful API 操作elasticsearch搜索数据

通过HTTP RESTful API 操作elasticsearch搜索数据

使用python的docker-py实现docker的api操作

前沿: 听同事说,以后的dba申请可能有部分走其他部门的docker ,那边貌似在搞一个类似docker的平台管理系统,据说很霸道.于是乎,我自己也想尝试写一个简单的doker管理平台.  做为起步我先搞搞docker api,docker官网有个docker-py,用起来很是清爽简单. 首先安装docker的python相关的模块. [email protected]:~# pip install docker-py Requirement already satisfied (use --u

关于代码通过API操作阿里云RDS的巨坑

由于项目原因,要通过API操作阿里云的数据库,于是简单研究了一下阿里云提供的相关文档,发现官方提供了.NET的SDK,而且还提供了github开源代码,这个要为阿里点赞! 于是到github上弄了一份源码,发现源码文件相当多,尝试了一下只提取几个需要用到的类文件放到项目中,经过测试好像不行,也可能本人水平未达到,反正就是不行,最终放弃. 然后测试了一下引用项目和引用dll都没有问题,基本顺利. 在测试创建用户的时候遇到了一个坑,也可以说是巨坑!在创建账号的API文档中关于AccountType的

python 中调用windows系统api操作剪贴版

# -*- coding: utf-8 -*- ''' Created on 2013-11-26 @author: Chengshaoling ''' import win32clipboard as w32 import win32con class OperateClipboard(object): def __init__(self): # print "OperateClipboard" pass def getText(self): w32.OpenClipboard()