SOLR Java Test __java

Source: Internet
Author: User
Tags commit solr
< c13>1 Overview

SOLR is an independent enterprise-class search application server that provides an API interface similar to Web-service. The user can submit a certain format XML file to the Search engine server through HTTP request, generate the index, or can make the lookup request through the HTTP GET operation, and obtain the return result of the XML format. This is mainly explained in this way through HTTP GET requests.

First, we have to go through HTTP requests like

Http://localhost:8983/solr/solr_test/select?q=name "*of" &fq=price:[0to 6]

Link to access the server. The end result is passed back to the client in a way that is similar to streaming. The concrete form is in the form of XML, similar to the following figure:

2 SOLRJ Use

SOLRJ is a Java client that operates SOLR, providing a Java interface to add, modify, delete, and query the SOLR index. SOLRJ the HTTP interface for SOLR with rest, solrj the bottom is to complete the SOLR operation by using the methods in HttpClient.

SOLRJ are usually backward-compatible and can use the new version of SOLRJ to access older SOLR, and vice versa. It is recommended to use the same version of SOLRJ as SOLR server,

See HTTP://WIKI.APACHE.ORG/SOLR/SOLRJ for a description of compatibility.

2.1 Dependency Package Reference

Using MAVEN configuration:

       <dependency>
           <groupId>org.apache.solr</groupId>
           <artifactid>solr-solrj</ artifactid>
           <version>6.3.0</version>
       </dependency>
 


Note: There are different ways to operate because the APIs for each version of SOLR used are different,

The following is an old version of the API change to the new version:

Solrserver-> solrclient
Httpsolrserver-> httpsolrclient
Cloudsolrserver-> cloudsolrclient

The following tests will operate using the new API.

2.2 Test

Import org.apache.solr.client.solrj.SolrClient;
Import Org.apache.solr.client.solrj.SolrQuery;
Import org.apache.solr.client.solrj.impl.HttpSolrClient;
Import Org.apache.solr.client.solrj.response.QueryResponse;
Import Org.apache.solr.client.solrj.response.UpdateResponse;
Import org.apache.solr.common.SolrDocument;
Import org.apache.solr.common.SolrDocumentList;
 
Import org.apache.solr.common.SolrInputDocument; /** * @ClassName: Indexdemo * @Description: SOLR operation */public class Indexdemo {//single point public static final String
 
    Solr_url = "Http://localhost:8983/solr/solr_java";
    Cluster//String zkhoststring = "ZSERVERA:2181,ZSERVERB:2181,ZSERVERC:2181/SOLR";
 
    Solrclient SOLR = new//cloudsolrclient.builder (). Withzkhost (zkhoststring). build ();
 
    public static void Main (string[] args) {adddocs ();//Querydocs ();//Deldocs (); /** * @Description: Delete index */public static void Deldocs () {try {solrclient SOLR = new Httpsolrclient.builder (solr_url). build ();
            Delete Solr.deletebyid ("552199") based on ID; 
            Delete according to condition (e.g. delete all)//solr.deletebyquery ("*:*");
        Solr.commit ();
        catch (Exception e) {System.out.println (e);
    } System.out.println ("deletesuccess"); /** * @Description: Add index */public static void Adddocs () {try {solrcli
            ent SOLR = new Httpsolrclient.builder (solr_url). build ();
            Solrinputdocument document = new Solrinputdocument ();
            Document.addfield ("id", "100");
            Document.addfield ("name", "Test");
            Document.addfield ("Price", "1000");
            Updateresponse response = Solr.add (document);
 
        Solr.commit ();
        catch (Exception e) {System.out.println (e);
    } System.out.println ("addsuccess"); /** * @Description: Query Index * * Publicstatic void Querydocs () {try {//*//http://localhost:8983/solr/solr_java/select?q via HTTP query
            = "id:100" solrclient SOLR = new Httpsolrclient.builder (solr_url). build ();
Solrquery query = new Solrquery ();
            Query.set ("Q", "id:*");
            Query.set ("Q", "id:100");
            Queryresponse response = solr.query (query);
            Solrdocumentlist list = Response.getresults ();
           
            Search Results System.out.println ("find:" + response.getresults (). Getnumfound ());
            for (Solrdocument solrdocument:list) {System.out.println (Solrdocument.get ("name"));
        The catch (Exception e) {System.out.println (e); }
    }
 
}


Bulk Add

/**
     * @Description: Bulk Add
    /public static void Bathadddocs () {
        string[] words = {"Central overall deepening reform leadership group", "fourth meeting" };
        list<solrinputdocument> docs = new arraylist<solrinputdocument> ();
        for (int i = 0; i < 2; i++) {
            solrinputdocument Doc1 = new Solrinputdocument ();
            Doc1.addfield ("id", "id" + I, 1.0f);
            Doc1.addfield ("name", Words[i], 1.0f);
            Doc1.addfield ("Price", * i);
            Docs.add (Doc1);
        }
        try {
            solrclient SOLR = new Httpsolrclient.builder (solr_url). build ();
            Updateresponse response = Solr.add (Docs.iterator ());
            Increased after commit solr.commit () by executing a commit function
            ();
           
        } catch (Exception e) {
            System.out.println (e);
        }
        System.out.println ("addsuccess");
    }


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.