Solr全量與增量導(dǎo)入

Solr Full Import全量導(dǎo)入

所謂全量索引一般指的是每次從數(shù)據(jù)庫中讀取需要導(dǎo)入的全部數(shù)據(jù),然后提交到Solr Server,最后刪除指定core的所有索引數(shù)據(jù)進行重建。全量導(dǎo)入一般在數(shù)據(jù)首次導(dǎo)入或者備份數(shù)據(jù)恢復(fù)時執(zhí)行。

以下為一個多表查詢的全量導(dǎo)入案例:

  1. ER圖:


    ER圖
  • 根據(jù)如上ER圖,我們在數(shù)據(jù)庫中執(zhí)行如下SQL建表并插入測試數(shù)據(jù)。
use solr;

create table feature(item_id bigint,descrip varchar(80));
create table item(id bigint, item_name varchar(20), manu varchar(20), weight float,price float, popularity int, includes varchar(20));
create table item_category(item_id bigint, category_id bigint);
create table category(id bigint, descrip varchar(80));

alter table item add primary key(id);
alter table item_category add primary key(item_id, category_id);
alter table category add primary key(id);

insert into item values(1,"item1", "menu1", 12.0, 33.1, 10, "includes1");
insert into item_category values(1,1);
insert into category values(1,"this is the description of category 1");
insert into feature values(1,"this is the feature 1");
  1. 需求描述:我們希望將item表的所有字段以及item的category信息,item的descrip描述信息一并導(dǎo)入到solr指定的core中,因此solr的schema.xml中需要預(yù)先定義如下域:name, manu, weight, price, popularity, includes, cat, features.
  2. 解決方案:
  • 比較容易想到的就是通過SQL語句一并返回所需的域(數(shù)據(jù))。
use solr;
select i.id,i.item_name,i.manu,i.weight,i.price,i.popularity,i.includes,c.descrip as cat,f.descrip as feature from item i, item_category ic, category c, feature f where i.id=ic.item_id and ic.category_id=c.id and i.id=f.item_id;
  • 也可以通過data-config.xml中嵌套entity來實現(xiàn)。
<dataConfig>
  <dataSource name="jdbcDataSource" type="JdbcDataSource" driver="com.mysql.jdbc.Driver" url="jdbc:mysql://localhost:3306/solr?useUnicode=true&amp;characterEncoding=utf-8" user="root" password="mysql"/>
  <document>
     <entity dataSource="jdbcDataSource" name="item" query="select * from item">
        <field column="id" name="id"/>
        <field column="name" name="name"/>
        <field column="manu" name="manu"/>
        <field column="weight" name="weight"/>
        <field column="price" name="price"/>
        <field column="popularity" name="popularity"/>
        <field column="includes" name="includes"/>
        <entity name="feature" query="select descrip from feature where item_id='${item.id}'">
            <field column="descrip" name="features"/>
        </entity>
        <entity name="item_category" query="select category_id from item_category where item_id='${item.id}'">
            <entity name="category" query="select descrip from category where id='${item_category.category_id}'">
                <field column="descrip" name="cat"/>
            </entity>
        </entity>
     </entity>
  </document>
</dataConfig>

至此,重新加載core,通過url接口進行全量導(dǎo)入:http://localhost:8080/solr/mysql_fullimport/dataimport?command=full-import

Solr Delta Import增量導(dǎo)入

當(dāng)索引數(shù)據(jù)量很大時,每次都依靠全量導(dǎo)入顯然很不切實際,所以增量導(dǎo)入索引數(shù)據(jù)更為重要。
增量導(dǎo)入操作內(nèi)部是新開辟一個新線程來完成,并且此時core的dataimport運行狀態(tài)為status="busy"。增量導(dǎo)入耗時時間取決于需要增量導(dǎo)入的數(shù)據(jù)集合大小。任何時候你都可以通過http://localhost:8080/solr/<core_name>/dataimport 這個鏈接來獲取到增量導(dǎo)入的運行狀態(tài)。
當(dāng)增量導(dǎo)入操作被執(zhí)行,他會讀取存儲在conf/deltaimport.properties配置文件,利用配置文件里記錄的上一次操作時間來運行增量查詢,增量導(dǎo)入完成后,會更新conf/deltaimport.properties配置文件里的上一次操作時間戳。首次執(zhí)行增量導(dǎo)入時,若conf/deltaimport.properties配置文件不存在,會自動創(chuàng)建。

#Sun Mar 03 19:59:43 IRKT 2019
item.last_index_time=2019-03-03 19\:59\:43
last_index_time=2019-03-03 19\:59\:43

如果要使用增量導(dǎo)入,前提是你的表必需有兩個字段,一個是刪除標(biāo)志字段即邏輯刪除標(biāo)志:isdeleted,另一個則是數(shù)據(jù)創(chuàng)建時間字段:create_date,字段名稱不一定非得是isdeleted和create_date,但必須要包含兩個表示該含義的字段。根據(jù)數(shù)據(jù)創(chuàng)建時間跟上一次增量導(dǎo)入操作時間一對比,就可以通過SQL語句查詢出需要增量導(dǎo)入的數(shù)據(jù),根據(jù)isdeleted字段可以查詢出被標(biāo)記為刪除的數(shù)據(jù),這些數(shù)據(jù)的ID主鍵需要傳遞給solr,這樣solr就能同步刪除索引中相關(guān)Document,實現(xiàn)數(shù)據(jù)增量更新。如果你數(shù)據(jù)表里的數(shù)據(jù)都是物理刪除,沒有邏輯標(biāo)志字段的話,那么找出已刪除的數(shù)據(jù)顯得比較困難,所以這就是需要邏輯刪除標(biāo)志字段的原因。

仍然使用上一節(jié)的那幾張表為例。對于復(fù)合主鍵記錄的增量更新,solr會拋出deltaQuery has no column to resolve to declared paimary key pk='key1, key2',暫時還沒有找到合適的解決方案。如有,請留言告知,謝謝。

<dataConfig>
  <dataSource driver="com.mysql.jdbc.Driver" url="jdbc:mysql://localhost:3306/solr?useUnicode=true&amp;characterEncoding=utf-8" user="root" password="mysql"/>
  <document>
    <entity name="item" pk="id" query="select * from item" deltaImportQuery="select * from item where id='${dih.delta.id}'" deltaQuery="select id from item where last_modified &gt; '${dih.last_index_time}'">
    <entity name="feature" pk="item_id" query="select descrip as features from feature where item_id='${item.id}'" deltaQuery="select item_id from feature where last_modified > '${dih.last_index_time}'" parentDeltaQuery="select id from item where id='${feature.item_id}'"/>
    <entity name="item_category" pk="item_id" query="select category_id from item_category where item_id='${item.id}'" deltaQuery="select item_id, category_id from item_category where last_modified > '${dih.last_index_time}'" parentDeltaQuery="select id from item where id='${item_category.item_id}'">
    <entity name="category" pk="id" query="select descrip as cat from category where id='${item_category.category_id}'" deltaQuery="select id from category where last_modified &gt; '${dih.last_index_time}'" parentDeltaQuery="select item_id, category_id from item_category where category_id='${category.id}'"/>
    </entity>
    </entity>
  </document>
</dataConfig>
  • pk:表示當(dāng)前entity表示主鍵字段名稱,這里的主鍵指的是數(shù)據(jù)庫表中的主鍵,而非solr中的uniqueKey主鍵域。如果你的sql語句中使用了as關(guān)鍵字為主鍵字段定義了別名,那么這里的pk屬性需要相應(yīng)的修改為主鍵字段的別名,切記;
  • query:用于指定全量導(dǎo)入時需要的sql語句,比如select * from xxx where isdeleted=0,查詢返回的是為被刪除的所有有效數(shù)據(jù),這個query參數(shù)只對全量導(dǎo)入有效,對增量導(dǎo)入無效;
  • deltaQuery:查詢需要增量導(dǎo)入的記錄的主鍵id所需的sql語句??赡苁莡pdate,insert,delete等操作,比如:deltaQuery="select id from xxx where my_date > '${dataimporter.last_index_time}'",此參數(shù)值對增量導(dǎo)入有效;
  • deletedPkQuery:查詢已經(jīng)被邏輯刪除了的數(shù)據(jù)所需的SQL語句,所以這里你需要一個類似isdeleted的邏輯刪除標(biāo)志位字段。solr通過此參數(shù)表示的sql語句執(zhí)行后返回的結(jié)果集來刪除索引里面對應(yīng)的數(shù)據(jù)。使用示例:select id from myinfo where isdeleted=1,此參數(shù)對增量導(dǎo)入有效。
  • deltaImportQuery: deltaImpotQuery="select * from myinfo where id='${dataimporter.delta.id}'",利用deltaQuery參數(shù)返回的所有需要增量導(dǎo)入的數(shù)據(jù)主鍵id,遍歷每個主鍵id,然后循環(huán)執(zhí)行deltaImportQuery參數(shù)表示的sql語句返回所有需要增量導(dǎo)入的數(shù)據(jù)。其中變量${dataimporter.delta.id}用于獲取deltaQuery返回的每個主鍵id。

增量導(dǎo)入的接口url:http://localhost:8080/solr/<core_name>/dataimport?command=delta-import

擴展閱讀:solr dataimport scheduler:http://code.google.com/p/solr-data-import-scheduler/

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容