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