Java Operation SOLR implements query function

Source: Internet
Author: User
Tags solr solr query

First, the package of query methods

/**
* SOLR Query method
* @param client SOLR clients
* @param query SOLR queries the object
* @return List Collection
* @throws solrserverexception
* @throws IOException
*/
public static list<map<string, object>> Getsolrquery (httpsolrclient client, solrquery query) throws Solrserverexception, ioexception{
list<map<string, object>> list = null;
Execute the query and return the results
Queryresponse resp = client.query (query);
Solrdocumentlist results = Resp.getresults ();
Get the total amount of data queried
Long Numfound = Results.getnumfound ();
Judging if the total is greater than 0,
if (numfound <= 0) {
If less than 0, indicates no data is queried, returns null
return null;
}else {
If greater than 0, indicates that there is data
Create a list to store each piece of data
List = new arraylist<> ();
Traversing result Sets
for (Solrdocument doc:results) {
Get a map collection of each piece of data
map<string, object> map = Doc.getfieldvaluemap ();
Add to List
List.add (map);
}
Returns the list collection
return list;
}
}

Second, the main function

public static void Main (string[] args) throws Solrserverexception, IOException {
Create SOLR client connections
Httpsolrclient HSC = new Httpsolrclient.builder (solr_url). build ();
To create a query object
Solrquery query = new Solrquery ();
Set criteria for querying all data
Query.setquery ("*:*");
Query.setquery ("Id:1");

list<map<string, object>> list = getsolrquery (HSC, query);
if (list = = null) {
System.out.println ("No results found");
Return
}
For (map<string, object> map:list) {
Iterator<string> it = Map.keyset (). Iterator ();
while (It.hasnext ()) {
String key = It.next ();
Object value = Map.get (key);
System.out.println (key+ "----" +value);
}
System.out.println ("=======================================");
}

Third, Solr_url

Note This is the correct URL address for a person

If the URL is not added to the table will report the following error:

Java Operation SOLR implements query functionality

Related Article

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.