SolrJ deleteByQuery doesn't work with cores - solr

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

Related

SOLRJ giving me strange error when trying to add a pdf to a new core. "You must type correct path"

So starting to update ancient solr app to 9.1 and also the SolrJ indexer. When I try to add a document, I am getting
Exception in thread "main" org.apache.solr.client.solrj.impl.BaseHttpSolrClient$RemoteSolrException: Error from server at http://my.host:8983/solr/qmap:
Searching for Solr
You must type the correct path
Solr will respond
I can see the qmap core in the solr admin and solr is running.
Code is:
public class DocumentIndexer {
private final String fileToIndex;
private final ConcurrentUpdateHttp2SolrClient solrClient;
private final Http2SolrClient http2Client;
public DocumentIndexer(String solrUrl, String fileToIndex) {
this.fileToIndex =fileToIndex;
http2Client = new Http2SolrClient.Builder().build();
solrClient = new ConcurrentUpdateHttp2SolrClient.Builder(solrUrl, http2Client).build();
}
public void indexDocuments() throws IOException, SolrServerException{
ContentStreamUpdateRequest req = new ContentStreamUpdateRequest("/update/extract");
req.setAction(AbstractUpdateRequest.ACTION.COMMIT, true, true);
req.addFile(new File(fileToIndex),"application/xml");
req.setParam("id", fileToIndex);
req.process(solrClient);
solrClient.commit(true, true);
}
}
Simple enough - update/extract was not defined in the solrconfig. Recreating the core using the sample_techproducts_examples as template supplies this or alternatively setting up the solrconfig with the update/extract path defined.
Also, req.setParam("id", fileToIndex) needs to be changed to req.setParam("literal.id", fileToIndex)

trouble in solr connect java

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!!!

Error in executing solr search query from a CloudSolrServer object

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.

How to index data in a specific shard using solrj

I am using solrj as client to index documents into solr cloud (Using solr4.5)
I had a requirement to save documents based on tenant_id, so i am trying to do document routing. Which is possible only if the collection is created using numShards parameter (http://searchhub.org/2013/06/13/solr-cloud-document-routing/)
I have two instances of solr in solr cloud(example1/solr and example2/solr) and exrenal zookeeper which is running in 2181 port.
Both the instances consist collection called collection1
I created one more collection called newCollection(With two shards and two replicas) using
http://localhost:8501/solr/admin/collectionsaction=CREATE&name=newCollection&numShards=2&replicationFactor=2&maxShardsPerNode=2&router.field=id
So in example1/solr-> I have newCollection_shard1_replica1 & newCollection_shard2_replica1,
In example2/solr -> I have newCollection_shard1_replica2 & newCollection_shard2_replica2
I copied example1/solr/collection1/conf to all shards and replicas
I restarted zookeeper server as well as solr instances:
zookeeper->zkServer.cmd
example1/solr-> java -Dbootstrap_confdir=./solr/newCollection_shard1_replica1/conf -Dcollection.configName=myconf -DzkHost=localhost:2181 -jar start.jar
example2/solr->java -DzkHost=localhost:2181 -jar start.jar
(Both instances are running at different port, one is at 8081 and other at 8051)
I am using solrj client to index documents
Here is my sample code
String url="http://localhost:8081/solr"
ConcurrentUpdateSolrServer solrServer= new ConcurrentUpdateSolrServer(url, 10000, 4);
SolrInputDocument doc = new SolrInputDocument();
doc.addField("id", "shard1!513");
doc.addField("name", "Santhosh");
solrServer.add(documents);
solrServer.commit();
But it is saving document in collection1 with id shard1!513, is there any configuration changes required in solrconfig.xml (I am using default solrconfig.xml which came with solr4.5)
How to save documents in my newCollection? and how to do document routing?
Please help me out with issue.
Thanks!
You can Use CloudSolrServer and UpdateRequest
SolrServer solrServer = new CloudSolrServer(zkHost) // zkHost is your solr zookeeper host string
SolrInputDocument doc = new SolrInputDocument();
UpdateRequest add = new UpdateRequest();
add.add(document);
add.setParam("collection", "newCollection");
add.process(solrServer);
UpdateRequest commit = new UpdateRequest();
commit.setAction(UpdateRequest.ACTION.COMMIT, true, true);
commit.setParam("collection", "newCollection");
commit.process(solrServer);
I appended Core name of new Collection to the URL. so it is working fine now.
Instead of:
String url="http://localhost:8081/solr"
I used:
String url="http://localhost:8081/solr/newCollection_shard1_replica1"
ConcurrentUpdateSolrServer solrServer= new ConcurrentUpdateSolrServer(url, 10000, 4);
SolrInputDocument doc = new SolrInputDocument();
doc.addField("id", "shard1!513");
doc.addField("name", "Santhosh");
solrServer.add(documents);
solrServer.commit();
You should use CloudSolrServer http://lucene.apache.org/solr/4_2_1/solr-solrj/org/apache/solr/client/solrj/impl/CloudSolrServer.html
Because in solrcloud, updates must be routed via zookeeper, as zookeeper knows the status of leaders in cloud.One more thing you need not to append collection name to url, just use setDefaultCollection(collectionName); method of CloudSolrServer to send your updates to 'collectionName' collection

Solrj Select All

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.

Resources