建立索引時可能遇到實體嵌套的情況,比如學生資訊中包含多個活動。
在solr的data-config.xml中配置:
<entity name="studentProfile" pk="ID" query="select CONCAT('studentProfile',studentProfileId) as solr_id,studentProfileId,name,location,description from studentProfile" deltaQuery="select sp.studentProfileId as ID from studentProfile sp,users u where u.registerTime > '${dataimporter.last_index_time}' and sp.email=u.email " deltaImportQuery="select CONCAT('studentProfile',studentProfileId) as solr_id,studentProfileId,name,location,description from studentProfile where studentProfileId='${dataimporter.delta.ID}'" > <field name="id" column="solr_id"/> <field name="studentprofile_id" column="studentProfileId"/> <field name="studentprofile_name" column="name"/> <field name="studentprofile_location" column="location"/> <field name="studentprofile_description" column="description"/> <field name="studentprofile_publicUri" column="publicUri"/> <entity name="activity" query="select activityId,title,description from activity where studentProfileId = '${studentProfile.studentProfileId}' " > <field name="activity_id" column="activityId"/> <field name="activity_title" column="title"/> <field name="activity_description" column="description"/> </entity></entity> 注意${studentProfile.studentProfileId}的寫法。 在schema.xml中配置:
<field name="studentprofile_id" type="string" indexed="true" stored="true" /> <field name="studentprofile_name" type="text_mmseg4j" indexed="true" stored="true" /> <field name="studentprofile_location" type="text_mmseg4j" indexed="true" stored="true" /> <field name="studentprofile_description" type="text_mmseg4j" indexed="true" stored="true" /> <field name="studentprofile_publicUri" type="text_mmseg4j" indexed="true" stored="true" /> <field name="activity_id" type="string" indexed="true" stored="true" multiValued="true"/> <field name="activity_title" type="text_mmseg4j" indexed="true" stored="true" multiValued="true"/> <field name="activity_description" type="text_mmseg4j" indexed="true" stored="true" multiValued="true"/>
注意子field中需要配置multiValued="true"
這樣配置完成後建立的索引顯示結果如下:
"docs": [ { "id": "studentProfile6315bbe5-399f-49b3-9382-b5fb0c2e8e2c", "studentprofile_location": "beijing", "studentprofile_name": "lcq", "studentprofile_id": "6315bbe5-399f-49b3-9382-b5fb0c2e8e2c", "activity_id": [ "1", "2" ], "activity_title": [ "dfsfs", "fsfs" ], "activity_description": [ "lcq", "haha" ], "_version_": 1527223001237422000 } ]
配置查詢時加權:
在solrconfig.xml中添加
<requestHandler name="search" class="solr.SearchHandler" default="true"> <lst name="defaults"> <str name="echoParams">explicit</str> <str name="defType">dismax</str> <str name="qf"> studentprofile_name^1 studentprofile_description^0.8 </str> <str name="q.alt">*:*</str> <str name="rows">10</str> <str name="fl">*,score</str> </lst> </requestHandler>
使用solrj查詢時:
query.set("qt","search");即可。