<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>databse &#8211; Solr.pl</title>
	<atom:link href="https://solr.pl/en/tag/databse/feed/" rel="self" type="application/rss+xml" />
	<link>https://solr.pl/en/</link>
	<description>All things to be found - Blog related to Apache Solr &#38; Lucene projects - https://solr.apache.org</description>
	<lastBuildDate>Wed, 11 Nov 2020 08:05:56 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9</generator>
	<item>
		<title>Data Import Handler – removing data from index</title>
		<link>https://solr.pl/en/2011/01/03/data-import-handler-removing-data-from-index/</link>
					<comments>https://solr.pl/en/2011/01/03/data-import-handler-removing-data-from-index/#respond</comments>
		
		<dc:creator><![CDATA[Rafał Kuć]]></dc:creator>
		<pubDate>Mon, 03 Jan 2011 08:05:13 +0000</pubDate>
				<category><![CDATA[Solr]]></category>
		<category><![CDATA[data import handler]]></category>
		<category><![CDATA[databse]]></category>
		<category><![CDATA[dih]]></category>
		<category><![CDATA[integration]]></category>
		<guid isPermaLink="false">http://sematext.solr.pl/?p=186</guid>

					<description><![CDATA[Deleting data from an index using DIH incremental indexing, on Solr wiki, is residually treated as something that works similarly to update the records. Similarly, in a previous article, I used this shortcut, the more that I have given an]]></description>
										<content:encoded><![CDATA[<p>Deleting  data from an index using DIH incremental indexing, on Solr wiki, is  residually treated as something that works similarly to update the  records. Similarly,  in a previous article, I used this shortcut, the more that I have given  an example of indexing wikipedia data that does not need to delete  data.</p>
<p>Having at hand a sample data of the albums and performers, I decided to show my way of dealing with such cases. For simplicity and clarity, I assume that after the first import, the data can only decrease.</p>
<p><span id="more-186"></span></p>
<h2>Test data</h2>
<p>My test data are located in the PostgreSQL database table defined as follows:
</p>
<pre>Table "public.albums"
Column |  Type   |                      Modifiers
--------+---------+-----------------------------------------------------
id     | integer | not null default nextval('albums_id_seq'::regclass)
name   | text    | not null
author | text    | not null
Indexes:
"albums_pk" PRIMARY KEY, btree (id)</pre>
<p>The table has 825,661 records.</p>
<h2>Test installation</h2>
<p>For testing purposes I used the Solr instance having the following characteristics:</p>
<p>Definition at schema.xml:
</p>
<pre class="brush:xml">&lt;fields&gt;
 &lt;field name="id" type="string" indexed="true" stored="true" required="true" /&gt;
 &lt;field name="album" type="text" indexed="true" stored="true" multiValued="true"/&gt;
 &lt;field name="author" type="text" indexed="true" stored="true" multiValued="true"/&gt;
&lt;/fields&gt;
&lt;uniqueKey&gt;id&lt;/uniqueKey&gt;
&lt;defaultSearchField&gt;album&lt;/defaultSearchField&gt;</pre>
<p>Definition of DIH in solrconfig.xml:
</p>
<pre class="brush:xml">&lt;requestHandler name="/dataimport" class="org.apache.solr.handler.dataimport.DataImportHandler"&gt;
 &lt;lst name="defaults"&gt;
  &lt;str name="config"&gt;db-data-config.xml&lt;/str&gt;
 &lt;/lst&gt;
&lt;/requestHandler&gt;</pre>
<p>And the file DIH db-data-config.xml:
</p>
<pre class="brush:xml">&lt;dataConfig&gt;
 &lt;dataSource driver="org.postgresql.Driver" url="jdbc:postgresql://localhost:5432/shardtest" user="solr" password="secret" /&gt;
 &lt;document&gt;
  &lt;entity name="album" query="SELECT * from albums"&gt;
   &lt;field column="id" name="id" /&gt;
   &lt;field column="name" name="album" /&gt;
   &lt;field column="author" name="author" /&gt;
  &lt;/entity&gt;
 &lt;/document&gt;
&lt;/dataConfig&gt;</pre>
<p>Before our test, I imported all the data from the table <em>albums</em>.</p>
<h2>Deleting Data</h2>
<p>Looking at the table shows that when we remove the record, he is deleted without leaving a trace, and the only way to update our index would be to compare the documents identifiers in the index to the identifiers in the database and deleting those that no longer exist in the database. Slow and cumbersome. Another way is adding a column <em>deleted_at</em>: instead of physically deleting the record, only add information to this column. DIH can then retrieve all records from the set date later than the last crawl. The disadvantage of this solution may be necessary to modify the application to take such information into consideration.</p>
<p>I apply a different solution, transparent to applications. Let&#8217;s create a new table:
</p>
<pre class="brush:sql">CREATE TABLE deletes
(
id serial NOT NULL,
deleted_id bigint,
deleted_at timestamp without time zone NOT NULL,
CONSTRAINT deletes_pk PRIMARY KEY (id)
);</pre>
<p>This table will automagically add an identifier of those items that were removed from the table <em>albums </em>and information when they were removed.</p>
<p>Now we add the function:
</p>
<pre class="brush:sql">CREATE OR REPLACE FUNCTION insert_after_delete()
RETURNS trigger AS
$BODY$BEGIN
IF tg_op = 'DELETE' THEN
INSERT INTO deletes(deleted_id, deleted_at)
VALUES (old.id, now());
RETURN old;
END IF;
END$BODY$
LANGUAGE plpgsql VOLATILE;</pre>
<p>and a trigger:
</p>
<pre class="brush:sql">CREATE TRIGGER deleted_trg
BEFORE DELETE
ON albums
FOR EACH ROW
EXECUTE PROCEDURE insert_after_delete();</pre>
<h2>How it works</h2>
<p>Each entry deleted from the <em>albums </em>table should result in addition to the table <em>deletes</em>. Let&#8217;s check it out. Remove a few records:
</p>
<pre class="brush:sql">=&gt; DELETE FROM albums where id &lt; 37;
DELETE 2
=&gt; SELECT * from deletes;
id | deleted_id |         deleted_at
----+------------+----------------------------
26 |         35 | 2010-12-23 13:53:18.034612
27 |         36 | 2010-12-23 13:53:18.034612
(2 rows)</pre>
<p>So the database part works.</p>
<p>We fill up the DIH configuration file so that the <em>entity </em>has been defined as follows:
</p>
<pre class="brush:xml">&lt;entity name="album" query="SELECT * from albums"
  deletedPkQuery="SELECT deleted_id as id FROM deletes WHERE deleted_at &gt; '
<p>This allows the import DIH incremental import to use the <em>deletedPkQuery </em>attribute to get the identifiers of the documents which should be removed.</p>
<p>A clever reader will probably begin to wonder, are you sure we need the column with the date of deletion. We could delete all records that are found in the table <em>deletes </em>and then delete the contents of this table. Theoretically this is true, but in the event of a problem with the Solr indexing server we can easily replace it with another - the degree of synchronization with the database is not very important - just the next incremental imports will sync with the database. If we would delete the contents of the <em>deletes </em>table such possibility does not exist.</p>
<p>We can now do the incremental import by calling the following address:&nbsp; <em>/solr/dataimport?command=delta-import</em><br>
In the logs you should see a line similar to this:<br>
<em>INFO: {delete=[35, 36],optimize=} 0 2</em><br>
Which means that DIH properly removed from the index the documents, which were previously removed from the database.</p>
{dataimporter.last_index_time}'"&gt;</pre>
<p>This allows the import DIH incremental import to use the <em>deletedPkQuery </em>attribute to get the identifiers of the documents which should be removed.</p>
<p>A clever reader will probably begin to wonder, are you sure we need the column with the date of deletion. We could delete all records that are found in the table <em>deletes </em>and then delete the contents of this table. Theoretically this is true, but in the event of a problem with the Solr indexing server we can easily replace it with another &#8211; the degree of synchronization with the database is not very important &#8211; just the next incremental imports will sync with the database. If we would delete the contents of the <em>deletes </em>table such possibility does not exist.</p>
<p>We can now do the incremental import by calling the following address:&nbsp; <em>/solr/dataimport?command=delta-import</em><br />
In the logs you should see a line similar to this:<br />
<em>INFO: {delete=[35, 36],optimize=} 0 2</em><br />
Which means that DIH properly removed from the index the documents, which were previously removed from the database.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://solr.pl/en/2011/01/03/data-import-handler-removing-data-from-index/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
