solrj It's easy to make java Applications and solr One way to interact API,solrj Hidden a lot of connections Solr The details of the , Allow your application to use simple advanced methods and solr Interactive communication .

solrj The core of org.apache.solr.client.solrj package , The following contains 5 Major categories . By creating a SolrServer Start , This SolrServer That's what you ask for solr example , Then send out SolrRequests Or is it SolrQuerys, Finally back to SolrResponses.

SolrServer Is an abstract class , So in order to connect to the remote host Solr example , Generally create a HttpSolrServer example . This example is mainly used to pass http and Solr Services interact .

String urlString = "http://localhost:8983/solr";
SolrServer solr = new HttpSolrServer(urlString);

Create a SolrServer It doesn't make the network connected , When you are performing queries or other operations , To connect to the Internet , If URL If the string is incorrect , Will throw out a MalformedURLException abnormal .

Once created SolrServer after , You can call query(),add(),method() Methods .

Build and run SolrJ Applications

1. add to to be in the classpath. solr-solrj-4.x.x.jar class .

2.dist/solrj-lib Class libraries under need to be loaded .

3. Use Maven Words , stay pom.xml Add :

<dependency>
<groupId>org.apache.solr</groupId>
<artifactId>solr-solrj</artifactId>
<version>4.x.0</version>
</dependency>

If you are worried about solrJ Expands the size of client applications , You can use a code obfuscation tool like ProGuard To delete what you don't use API.

Set up XMLResponseParser

solrj Using binary format , instead of xml Format as its default . An earlier version of solr By setting the parser XMLResponseParser To use XML Format operation .

server.setParser(new XMLResponseParser());

Execute the query

Use query() Method to get solr Search results for . You need to pass a SolrQuery object , And then return a QueryResponse object .

SolrQuery There are many ways , It's easy to add parameters to select the request processor (handler), And send parameters to handler. Here's a very simple example , Use the default

Request processor , Set up q Parameters :

SolrQuery parameters = new SolrQuery();
parameters.set("q", mQueryString);

Choose a different request processor , Just set up qt Parameters :

parameters.set("qt", "/spellCheckCompRH");

Once setup is complete SolrQuery, Use query() To submit :

QueryResponse response = solr.query(parameters);

This client generates a network connection , And sent query Query information ,solr Processing this query information , then response Respond to , And return a QueryResponse.

QueryResponse Is a set of document results that satisfy the query parameters . Can pass getResults() Method to get the retrieval result document directly , You can also call other methods to get highlight and facet information .

SolrDocumentList list = response.getResults();

Index documents

Other operations are also very simple , For indexed documents , You just need to create one SolrInputDocument object , And pass SolrServer Of add() Method to transfer data , And then call

commit() The method can .

String urlString = "http://localhost:8983/solr";
SolrServer solr = new HttpSolrServer(urlString);
SolrInputDocument document = new SolrInputDocument();
document.addField("id", "552199");
document.addField("name", "Gouda cheese wheel");
document.addField("price", "49.99");
UpdateResponse response = solr.add(document);
// Remember to commit your changes!
solr.commit();

use XML Or upload content in binary format (content)

SolrJ Allows you to XML And binary format instead of the default XML Format .solrj You can use the following to upload in binary format , The same format is used to get the results .

server.setRequestWriter(new BinaryRequestWriter());

Use ConcurrentUpdateSolrServer

When implementing Java Application time , This will load a large number of documents at one time ,ConcurrentUpdateSolrServer It's a HttpSolrServer Alternative classes for ,ConcurrentUpdateSolrServer Use Buffer Buffer add all documents , And write it to open HttpSolrServer in . This class is thread-safe , Although arbitrary SolrServer Requests can be completed using this implementation , But only recommended ConcurrrentUpdateSolrServer do /update request .

EmbeddedSolrServer

This embedding method is not recommended in most cases , And it supports very limited solr function , Especially not in SolrCloud perhaps Index Replication.EmbeddedSolrServer The existence of is mainly to help easier testing .

about EmbeddedSolrServer Use , You can refer to Solrj Of Junit unit testing ,org.apache.solr.client.solrj.embedded package .

On the topic

Solrj API file

Solrj WiKi Home page

Indexing and basic data manipulation ( To be released )

This section is translated from :Apache Solr Reference Guide 4.7, Impropriety , Please correct me. ! thank you .

solrj-solr Guide 4.7 More articles about

  1. Solr Series IV :Solr(solrj 、 Indexes API 、 Structured data import )

    One .SolrJ Introduce 1. SolrJ What is it? ? Solr Provided for JAVA Access in app solr service API The client of jar. Introduce... Into our application solrj: <dependency> <gro ...

  2. Solr JAVA client SolrJ 4.9 Use the sample tutorial

    http://my.oschina.net/cloudcoder/blog/305024 brief introduction SolrJ Is the operation Solr Of JAVA client , It provides an increase in . modify . Delete . Inquire about Solr Indexed JAVA Interface .So ...

  3. Use solrj operation solr The index library ,solr yes lucene The server

    Client development Solrj Client development  Solrj Solr It's built lucene The server   Of course, it is impossible to fully meet the general business needs   Probably   We need to adapt to various architectures and businesses   And that's where it comes in Solrj 了 Solrj yes Sol ...

  4. 【Solr Topic 9 】SolrJ course

    One .SolrJ Basics 1. Related information API:http://lucene.apache.org/solr/4_9_0/solr-solrj/ apache_solr_ref_guide_4.9.pdf:C ...

  5. 【Solr Topic 9 】SolrJ course classification : H4_SOLR/LUCENCE 2014-07-28 14:31 2351 Human reading Comment on (0) Collection

    One .SolrJ Basics 1. Related information API:http://lucene.apache.org/solr/4_9_0/solr-solrj/ apache_solr_ref_guide_4.9.pdf:C ...

  6. Solr+Tomcat+zookeeper Deploy the actual battle

    One . install solr Environmental statement :centos 7.3,solr 6.6,zookeeper3.4,Tomcat8.5,jdk1.8 zookeeper Please refer to :http://www.cnblogs. ...

  7. Full text search engine ——Solr

    1. Deploy solr a. Download and unzip Solr b. Import the project ( Independent projects ): After decompressing  server\solr-webapp Under the webapp Folder copy to tomcat Of webapps Next , And rename to  so ...

  8. Solr7.4.0 Of API(Solrj) Operations and use in projects

    One .SolrJ The concept of solr Stand alone service build :https://www.cnblogs.com/frankdeng/p/9615253.html solr Build cluster version service :https://www.cnbl ...

  9. Solr7.4.0 Of API(Solrj) operation

    One .SolrJ The concept of solr Stand alone service build :https://www.cnblogs.com/frankdeng/p/9615253.html solr Build cluster version service :https://www.cnbl ...

  10. solr Highlight

    Official website :https://lucene.apache.org/solr/guide/6_6/highlighting.html#Highlighting-TheUnifiedHighlighter Preface ...

Random recommendation

  1. H5 Quick start to front end performance testing

    Preface Speaking of H5 test , For doing WEB I'm very familiar with the test , It includes pages H5 A functional test , Front end performance test , Browser compatibility test , And server performance test . This article talks about H5 Front end performance test , And hope that after reading this article , To know ...

  2. Writing a json format api

    Recently, I wrote something that needs to be correct json The string is formatted and displayed on the web page . I just want to go online to find out if there is such api Can be called directly . Baidu json api , Search results are tools that can only be checked on the web , No, api. ...

  3. php How to get mac Address ?

    How to use php obtain mac The address? ? As we all know mac An address is the only global identifier of a computer , So this is very practical , For example, to make a voting function , that mac Address is essential Of , If you just rely on ip It must be inaccurate to judge this by address , Too much water ...

  4. Use label stay winfrom Add split lines to

    1. Horizontal separation line :GroupBox2. level , Vertical separation : Lable (AutoSize = false, BorderStyle= Fixed3D , And adjust Size Size Horizontal adjustment Height = ...

  5. Mybatis Get field values dynamically ( No need to create javabean)

    I've come across a demand like this recently , as follows : Users can select the fields to query and display through the custom template , Need dynamic query display . Premise : The database has a table , There are 400 Multiple fields . requirement : The user can customize the template in the foreground , A template may correspond to x A field ...

  6. INI Write and read of files

    INI Write and read of files [ The name of the festival ]         '[] The section name in corresponds to this API The first parameter of Name= Content       'Nmae Corresponding to this API The second parameter of API The third parameter of is the string returned when the matching content is not retrieved ; ...

  7. codeforces 528D Fuzzy Search

    link :http://codeforces.com/problemset/problem/528/D Positive solution :$FFT$. Many string matching problems can be used $FFT$ To achieve . This question is required to be on the left and right $k$ individual ...

  8. OWIN Self hosting static website

    We know , With the help of OWIN,WebApi,SignalR,WCF Can create self hosting (Self-Host) example , There's no need for IIS, Static websites can also . Recently do a server monitoring tool , use SignalR signal communication , monitor ...

  9. About CASE WHEN The problem of multi condition aggregation of

    https://bbs.csdn.net/topics/392217817?page=1 problem : -- Create test table IF EXISTS (SELECT * FROM sys.objects WHERE ...

  10. 20145304 Exp9 Web Basic safety practices

    20145304 Exp9 Web Basic safety practices Answer the questions after the experiment (1)SQL Injection attack principle , How to defend SQL Injection is to input the query statement into the query box as the query content , So that the server can execute the statement that the attacker wants it to execute , instead of ...