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();
    }


}




      本文转自yushiwh 51CTO博客,原文链接:http://blog.51cto.com/yushiwh/1969222,如需转载请自行联系原作者


相关文章
|
27天前
|
Java API Maven
HDFS的API操作
HDFS的API操作
25 0
|
2月前
|
API
Poi 中文API文档 「40种操作 Excel文件的姿势」
Poi 中文API文档 「40种操作 Excel文件的姿势」
116 0
|
3月前
|
JSON API 数据格式
在钉钉开放平台中,创建或更新OA审批表单模板需要通过API接口进行操作
在钉钉开放平台中,创建或更新OA审批表单模板需要通过API接口进行操作【1月更文挑战第20天】【1月更文挑战第97篇】
46 1
|
4月前
|
Java API
java 操作es api
java 操作es api
42 0
|
4月前
|
SQL Java API
Java 8:Stream API 流式操作
Java 8:Stream API 流式操作
52 0
|
5月前
|
Linux 程序员 API
LINUX系统API---操作目录
LINUX系统API---操作目录
49 0
|
4月前
|
Java Shell 分布式数据库
【大数据技术Hadoop+Spark】HBase数据模型、Shell操作、Java API示例程序讲解(附源码 超详细)
【大数据技术Hadoop+Spark】HBase数据模型、Shell操作、Java API示例程序讲解(附源码 超详细)
82 0
|
4月前
|
XML Java 数据库连接
MyBatis深入探索:原生API与注解方式实现CRUD操作
MyBatis深入探索:原生API与注解方式实现CRUD操作
65 0
|
4月前
|
安全 大数据 API
elasticsearch|大数据|elasticsearch的api部分实战操作以及用户和密码的管理
elasticsearch|大数据|elasticsearch的api部分实战操作以及用户和密码的管理
62 0
|
3月前
|
测试技术 API 数据安全/隐私保护
『App自动化测试之Appium应用篇』| Appium常用API及操作
『App自动化测试之Appium应用篇』| Appium常用API及操作
71 1

热门文章

最新文章