You are viewing a plain text version of this content. The canonical link for it is here.
Posted to solr-user@lucene.apache.org by Tania Marinova <ta...@yahoo.com> on 2013/04/20 20:12:18 UTC

using deletebyid with solrj

Hello I'm new to solr and especially solrj. I have made a java apllication which can index my neut I can't findw inserted row in the database  (which is what I want)
But now I ask Is there any way to delete the index of this row if for example the user decides to delete it from teh database so it has no longer to be indexed in solr! Can you give me directions how to do that or maybe simple code to change my code?()PS maube I should use deletebyid but I can't find info how to use it

;
public class indexSolr { private  Connection conn = null; private static HttpSolrServer  server; private Collection docs = new ArrayList(); private int _totalSql = 0; private long _start = System.currentTimeMillis(); public static void main(String[] args) throws SolrServerException, IOException, SQLException { String url = "http://localhost:8983/solr/db"; indexSolr idxer = new indexSolr(url); idxer.doSqlDocuments(); idxer.endIndexing(); } private void doSqlDocuments() throws SQLException { try { Class.forName("org.postgresql.Driver"); conn = DriverManager.getConnection( "jdbc:postgresql://localhost:5432/biz_cat", "postgres", "pos"); java.sql.Statement st = null; st = conn.createStatement(); ResultSet rs =   st.executeQuery("select * from pl_biz order by id DESC LIMIT 1"); while (rs.next()) { 
                                  SolrInputDocument doc = new SolrInputDocument();
                                 Integer  id = rs.getInt("id"); String name = rs.getString("name");
                                 String midname = rs.getString("midname"); 

                                  String lastname = rs.getString("lastname"); 


                                doc.addField("id", id); 

                               doc.addField("name", name);
                             doc.addField("midname", midname);
                             doc.addField("lastname", lastname); 


                            docs.add(doc); 

                         ++_totalSql;

                           if (docs.size() > 1) { // Commit within 5 minutes. 

                                      UpdateResponse resp = server.add(docs); 

                                     System.out.println (resp); if (resp.getStatus() != 0) { log("Some horrible error has occurred, status is: " + resp.getStatus()); }             docs.clear(); } } } 


                               catch (Exception ex) { ex.printStackTrace(); } finally { if (conn != null) { conn.close(); } } }


                 private void endIndexing() throws IOException, SolrServerException {

                      if (docs.size() > 0) { // Are there any documents left over?
                     server.add(docs, 300000); // Commit within 5 minutes } 


                try  { server.commit();  } catch (Exception ex) { ex.printStackTrace(); } 

               long endTime = System.currentTimeMillis(); log("Total Time Taken: " + (endTime - _start) + " milliseconds to index " + _totalSql + " SQL rows" ); } 



private indexSolr(String url) throws IOException, SolrServerException { // Create a multi-threaded communications channel to the Solr server. try {     server = new HttpSolrServer(url); server.setSoTimeout(1000);  // socket read timeout server.setConnectionTimeout(1000); server.setMaxRetries(1);  } catch (Exception ex) { ex.printStackTrace(); } } } 

Re: using deletebyid with solrj

Posted by Shawn Heisey <so...@elyograg.org>.
On 4/20/2013 11:21 PM, Tania Marinova wrote:
> so my question is as I have the  abillity to index one partricular row (you saw my code) how can I reindex in the same way one particular row assuming that I know the id of that row (so i can select it) so it's no longer indexed in solr. My database is postgresql. 

Your question is confusing, but I am assuming you want to know how to
delete a document in SolrJ.  There are multiple ways to do it.  If the
field that contains the ID you want to delete is the UniqueKey field
that you defined in your schema, you can use deleteById:

String id = "documentA";
server.deleteById(id);

List<String> ids = new ArrayList<String>();
ids.add("documentA");
ids.add("documentB");
server.deleteById(ids);

If the ID you want to delete is in a field other than the UniqueKey
field, you need deleteByQuery:

// OR is included in case you changed the default operator.
// If you didn't change it, then OR is not required.
String query = "field:(documentA OR documentB)";
server.deleteByQuery(query);

Thanks,
Shawn


Re: using deletebyid with solrj

Posted by Erick Erickson <er...@gmail.com>.
Uhhhm HttpSolrServer.deleteById (several varieties)?

http://lucene.apache.org/solr/api-4_0_0-BETA/org/apache/solr/client/solrj/SolrServer.html#deleteById(java.util.List)

the rest of your question is confusing.

bq: how can I reindex in the same way one particular row

Solr does that automatically for you based on id (<uniqueKey> in your
schema). Just index the doc
again and and Solr will replace the old document with that id with the new one.

Best
Erick

On Sun, Apr 21, 2013 at 1:21 AM, Tania Marinova <ta...@yahoo.com> wrote:
> No m question was - if a user deletes a row from the database I of course know the id if that row.
>
> so my question is as I have the  abillity to index one partricular row (you saw my code) how can I reindex in the same way one particular row assuming that I know the id of that row (so i can select it) so it's no longer indexed in solr. My database is postgresql.
>
>
>
>
> ________________________________
>  From: Shawn Heisey <so...@elyograg.org>
> To: solr-user@lucene.apache.org
> Sent: Saturday, April 20, 2013 11:40 PM
> Subject: Re: using deletebyid with solrj
>
>
> On 4/20/2013 12:12 PM, Tania Marinova wrote:
>> Hello I'm new to solr and especially solrj. I have made a java apllication which can index my neut I can't findw inserted row in the database  (which is what I want)
>> But now I ask Is there any way to delete the index of this row if for example the user decides to delete it from teh database so it has no longer to be indexed in solr! Can you give me directions how to do that or maybe simple code to change my code?()PS maube I should use deletebyid but I can't find info how to use it
>
> There's no way that Solr will know that you have deleted something from
> your database.  You have to tell it somehow.  This is outside the scope
> of a Solr help list.
>
> Fortunately, there are people here (like myself) that do deal with this
> problem in relation to Solr.  The way that we handle this for my setup
> with MySQL is with a delete trigger.  Anytime anything is deleted from
> the main database, the trigger adds the ID in the primary key to a
> delete table.  My SolrJ application reads that table and keeps it
> trimmed to a manageable size.
>
> Thanks,
> Shawn

Re: using deletebyid with solrj

Posted by Tania Marinova <ta...@yahoo.com>.
No m question was - if a user deletes a row from the database I of course know the id if that row.

so my question is as I have the  abillity to index one partricular row (you saw my code) how can I reindex in the same way one particular row assuming that I know the id of that row (so i can select it) so it's no longer indexed in solr. My database is postgresql. 




________________________________
 From: Shawn Heisey <so...@elyograg.org>
To: solr-user@lucene.apache.org 
Sent: Saturday, April 20, 2013 11:40 PM
Subject: Re: using deletebyid with solrj
 

On 4/20/2013 12:12 PM, Tania Marinova wrote:
> Hello I'm new to solr and especially solrj. I have made a java apllication which can index my neut I can't findw inserted row in the database  (which is what I want)
> But now I ask Is there any way to delete the index of this row if for example the user decides to delete it from teh database so it has no longer to be indexed in solr! Can you give me directions how to do that or maybe simple code to change my code?()PS maube I should use deletebyid but I can't find info how to use it

There's no way that Solr will know that you have deleted something from
your database.  You have to tell it somehow.  This is outside the scope
of a Solr help list.

Fortunately, there are people here (like myself) that do deal with this
problem in relation to Solr.  The way that we handle this for my setup
with MySQL is with a delete trigger.  Anytime anything is deleted from
the main database, the trigger adds the ID in the primary key to a
delete table.  My SolrJ application reads that table and keeps it
trimmed to a manageable size.

Thanks,
Shawn

Re: using deletebyid with solrj

Posted by Shawn Heisey <so...@elyograg.org>.
On 4/20/2013 12:12 PM, Tania Marinova wrote:
> Hello I'm new to solr and especially solrj. I have made a java apllication which can index my neut I can't findw inserted row in the database  (which is what I want)
> But now I ask Is there any way to delete the index of this row if for example the user decides to delete it from teh database so it has no longer to be indexed in solr! Can you give me directions how to do that or maybe simple code to change my code?()PS maube I should use deletebyid but I can't find info how to use it

There's no way that Solr will know that you have deleted something from
your database.  You have to tell it somehow.  This is outside the scope
of a Solr help list.

Fortunately, there are people here (like myself) that do deal with this
problem in relation to Solr.  The way that we handle this for my setup
with MySQL is with a delete trigger.  Anytime anything is deleted from
the main database, the trigger adds the ID in the primary key to a
delete table.  My SolrJ application reads that table and keeps it
trimmed to a manageable size.

Thanks,
Shawn