Here I am trying to fire a solr search query, I am using CloudSolrServer class to pass the zookeeper instance and then creating a instance of SolrQuery object for search.
CloudSolrServer solr = new CloudSolrServer("HOST_NAME:PORT");
System.out.println(solr.getZkStateReader());
SolrQuery solrQuery = new SolrQuery();
solrQuery.set("q", "abc");
solrQuery.addFilterQuery("type:*");
solrQuery.set("defType", "edismax");
solrQuery.set("start", 0);
solrQuery.set("rows", 10);
solrQuery.set("qf", "name^10.0 description^5.0");
solrQuery.addSortField("name_sort", SolrQuery.ORDER.asc);
QueryResponse response = solr.query(solrQuery);
When I am running this I am getting this error:
null
org.apache.solr.client.solrj.SolrServerException: Error executing query
at org.apache.solr.client.solrj.request.QueryRequest.process(QueryRequest.java:98)
at org.apache.solr.client.solrj.SolrServer.query(SolrServer.java:301)
at SolrCloud_Example.main(SolrCloud_Example.java:40)
Caused by: java.lang.RuntimeException
at org.apache.solr.common.cloud.SolrZkClient.<init>(SolrZkClient.java:115)
at org.apache.solr.common.cloud.SolrZkClient.<init>(SolrZkClient.java:83)
at org.apache.solr.common.cloud.ZkStateReader.<init>(ZkStateReader.java:138)
at org.apache.solr.client.solrj.impl.CloudSolrServer.connect(CloudSolrServer.java:140)
at org.apache.solr.client.solrj.impl.CloudSolrServer.request(CloudSolrServer.java:165)
at org.apache.solr.client.solrj.request.QueryRequest.process(QueryRequest.java:90)
... 2 more
Could anyone help me on this issue?
I have faced with the same exception. In the result such connection is workable for me:
CloudSolrServer solrServer = new CloudSolrServer("c-node3:2181/solr");
/solr in the end
When using CloudSolrServer, you need to set the default collection value.
Also the CloudSolrServer query() method does not actually take SolrQuery as an argument; it takes SolrParams. You can create SolrParams from a named list, which SolrQuery can do.
So your code would look something like this:
CloudSolrServer solr = new CloudSolrServer("HOST_NAME:PORT");
solr.setDefaultCollection("your_collection");
SolrQuery solrQuery = new SolrQuery();
solrQuery.set("q", "abc");
solrQuery.addFilterQuery("type:*");
solrQuery.set("defType", "edismax");
solrQuery.set("start", 0);
solrQuery.set("rows", 10);
solrQuery.set("qf", "name^10.0 description^5.0");
solrQuery.addSortField("name_sort", SolrQuery.ORDER.asc);
SolrParams params = SolrParams.toSolrParams(solrQuery.toNamedList());
QueryResponse response = solr.query(params);
You can then use response to get back your results.
Related
I try to use solr 6.5.0 to connect java . I have added following .jar files to the library:
commons-io-2.5
httpclient-4.4.1
httpcore-4.4.1
httpmine-4.4.1
jcl-over-slf4j-1.7.7
noggit-0.6
slf4j-api-1.7.7
stax2-api-3.1.4
woodstox-core-asl-4.4.1
zookeeper-3.4.6
solr-solrj-6.5.0
but when i try use following code to connect the solr:
import org.apache.http.impl.bootstrap.HttpServer;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.HttpSolrServer;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocumentList;
public class SolrQuery {
public static void main(String[] args) throws SolrServerException {
HttpSolrServer solr = new HttpServer("http://localhost:8983/solr/collection1");
SolrQuery query = new SolrQuery();
query.setQuery("*");
QueryResponse response = solr.query(query);
SolrDocumentList results = response.getResults();
for (int i = 0; i < results.size(); ++i) {
System.out.println(results.get(i));
}
}
}
before i compile it, I got an error in the:
import org.apache.solr.client.solrj.impl.HttpSolrServer;
import org.apache.solr.client.solrj.SolrQuery;
HttpSolrServer solr = new HttpServer("http://localhost:8983/solr/collection1");
Can anyone help me how to solve it?
The piece of code in your question was written for an old version of Solr before ver. 5.0. You'll find many sources and example around written for old Solr versions, but in most of the cases all you have to do is change the old SolrServer class with the new SolrClient (and now correct) class.
Both were the representations of the Solr instances you want to use.
Read the Solr Documentation - Using SolrJ
I warmly suggest to not use for your classes the same name of an already existing class (in your example your class is named SolrQuery).
The catch all string for Solr queries is *:* which means: search any match for all available fields. So change the statement query.setQuery into:
query.setQuery("*:*");
I suppose you're using a Solr client for a standalone instance so, as you're already aware, the correct way to instance a SolrClient is:
String urlString = "http://localhost:8983/solr/gettingstarted";
SolrClient solr = new HttpSolrClient.Builder(urlString).build();
And this is an easier way I suggest to iterate through all returned document:
for (SolrDocument doc : response.getResults()) {
System.out.println(doc);
}
Have a look at the documentation of SolrDocument class that explain how to use it and correctly read field values.
I founded that i need to import a .jar file which is not contain in the /dist library which named slf4j-simple-1.7.25 , and also
HttpSolrServer solr = new HttpServer("http://localhost:8983/solr/gettingstarted");
SolrQuery query = new SolrQuery();
need to change to the
String urlString = "http://localhost:8983/solr/gettingstarted";
SolrClient solr = new HttpSolrClient.Builder(urlString).build();
after that it finally can run already!!!
I got the following error:
This is my code:
At first glance, I see one problem: You never tell the SolrServer which core or collection to address. As is stated in the SolrJ docs:
There are two ways to use an HttpSolrClient:
1) Pass a URL to the constructor that points directly at a particular core
SolrClient client = new HttpSolrClient("http://my-solr-server:8983/solr/core1");
In this case, you can query the given core directly, but you cannot query any other cores or issue CoreAdmin requests with this client.
2) Pass the base URL of the node to the constructor
SolrClient client = new HttpSolrClient("http://my-solr-server:8983/solr");
QueryResponse resp = client.query("core1", new SolrQuery("*:*"));
BTW: You should use that Solrclient object that you commented out. The SolrServer objects are deprecated.
I have a solr instance running and am able to access it through the browser and use the Admin to run queries. When I try to access it via Java code in Eclipse, however, I receive the following error:
Exception in thread "main" org.apache.solr.common.SolrException: Server at http://localhost:8983/solr returned non ok status:404, message:Not Found
at org.apache.solr.client.solrj.impl.HttpSolrServer.request(HttpSolrServer.java:372)
at org.apache.solr.client.solrj.impl.HttpSolrServer.request(HttpSolrServer.java:181)
at org.apache.solr.client.solrj.request.QueryRequest.process(QueryRequest.java:90)
at org.apache.solr.client.solrj.SolrServer.query(SolrServer.java:301)
at testClass.main(testClass.java:18)
Here is the code I am running:
public static void main(String[] args) throws MalformedURLException, SolrServerException {
SolrServer server = new HttpSolrServer("http://localhost:8983/solr/");
ModifiableSolrParams params = new ModifiableSolrParams();
params.set("myParam", "myValue");
QueryResponse response = server.query(params);
}
It turns out that I had two errors:
1) My setup actually has a nested solr directory so I needed to add another "solr" level.
2) I was setting the params variable incorrectly. The first argument sent should be "q", with the second argument being the "name:value" pairs.
Updated example, includes passing multiple params at once:
public static void main(String[] args) throws MalformedURLException, SolrServerException {
SolrServer server = new HttpSolrServer("http://localhost:8983/solr/solr/");
ModifiableSolrParams params = new ModifiableSolrParams();
params.set("q", "param1:value1 AND param2:value2");
QueryResponse response = server.query(params);
System.out.println("response = " + response);
}
shouldn't it be :-
SolrServer server = new HttpSolrServer("http://localhost:8983/solr");
See the accepted answer in the following link :-
Querying Solr via Solrj: Basics
I'm trying, using SolrJ, to use the deleteByQuery method and it's not working.
Can you tell me please what is wrong?
This is working (deleteByIds):
ArrayList<String> ids = new ArrayList<String>();
ids.add("id_1");
ids.add("id_2");
SolrServer solrServer = getSolrServerForCore(0);
solrServer.deleteById(ids);
UpdateResponse ur2 = solrServer.commit();
But this isn't working:
SolrServer solrServer = getSolrServerForCore(0);
solrServer.deleteByQuery("*:*");
solrServer.commit();
I've managed to solved the problem by adding the _version_ field to the schema.xml
I am having issues selecting everything in my 25 document Solr (3.6) index via Solrj (running Tomcat).
public static void main(String[] args) throws MalformedURLException, SolrServerException {
SolrServer solr = new HttpSolrServer("http://localhost:8080/solr");
ModifiableSolrParams parameters = new ModifiableSolrParams();
parameters.set("?q", "*:*");
parameters.set("wt", "json");
QueryResponse response = solr.query(parameters);
System.out.println(response);
}
The result I get is:
{responseHeader={status=0,QTime=0,params={?q=*:*,wt=javabin,version=2}},response={numFound=0,start=0,docs=[]}}
Also, If I take the "?" out of parameters.set("?q", "*:*");I have to terminate the compilation or else it times out. The same happens if I replace the
"*:*"
with just
"*"
Also, I have tried parameters.set("qt", "/select");to no avail.
How do you select all and actually get results through Solrj?
I am not sure why this works but after failing on a hundred ideas, this one took:
public static void main(String[] args) throws MalformedURLException, SolrServerException {
SolrServer solr = new HttpSolrServer("http://localhost:8080/solr");
ModifiableSolrParams parameters = new ModifiableSolrParams();
parameters.set("q", "*:*"); //query everything thanks to user1452132!
parameters.set("facet", true);//without this I cant select all
parameters.set("fl", "id");//send back just the id values
parameters.set("wt", "json");//Id like this in json format please
QueryResponse response = solr.query(parameters);
System.out.println(response);
}
Hope this helps someone out there.
You should be using "q" as the parameter and the following is the right syntax.
parameters.set("?q", "*:*");
The reason why it returns with "?q" is that there is no query to run, so it returns fast.
First, please test through the browser. You can also set the number of rows to return, so that you are not returning a large result set.
parameters.set("rows", 5);
Once solr query returns, you have to paginate through the results. If you had a large collection you wont be able to retrieve all of them in one go.
I think you should try to also specify your core whenever you are referring to SolrServer object, i.e., write
SolrServer solr = new HttpSolrServer("http://localhost:8080/solr/collection1");
where collection1 is the name of the core that you want to use.