Document Boosting for FarCry Solr Pro Search

FarCry Solr Pro plugin pro provides various configuration and customisation of the search result including document elevation and boosting. However, we wanted to make it easier for contributors to use these features at the stage of managing contents. This tutorial page will list the steps of implementing document boosting for File objects.

STEP 1 : File Content Type

Extend dmFile.cfc to project level. - This will allow us to add new properties or methods for the file content type.

Add new property called boostValue to store specific boosting value for the file object. Note that it’s getting the options from getBoostOptions from solrProDocumentBoost content type. These boost options can be configured at webtop config > Solr Pro Plugin > Document Boost Values.

Add two new methods getBoostValueForFile and AfterSave to index boost value to solr pro when object is saved. This means you don’t need to do indexing process every time they make changes on the contents.

The dmFile.cfc should look like this

<cfcomponent extends="farcry.core.packages.types.dmFile" displayname="File">
	<cfproperty ftSeq="120" ftFieldset="Document Boosting" ftLabel="Boost Value" ftType="list" type="string" name="boostValue" 
		ftListData="getBoostOptions" ftListDataTypename="solrProDocumentBoost" 
		default="0"
		ftHint="Choose a boost value.<br /> The result will be high-lighted according to this boosting value." 
		hint="Stored as string because the FarCry compare fails when there are decimals." />
	<cffunction name="getBoostValueForFile" access="public" output="false" returntype="string">
		<cfargument name="documentId" required="true" type="uuid" />
		
		<cfset var q = "" />
		<cfquery name="q" datasource="#application.dsn#">
			select boostValue from dmFile where objectid = <cfqueryparam cfsqltype="cf_sql_varchar" value="#arguments.documentId#" />;
		</cfquery>
		
		<cfif q.recordCount>
			<cfreturn q.boostValue[1] />
		<cfelse>
			<cfreturn "" />
		</cfif>
	</cffunction>
	<cffunction name="AfterSave" access="public" output="false" returntype="struct" hint="Called from setData and createData and run after the object has been saved.">
		<cfargument name="stProperties" required="yes" type="struct" hint="A structure containing the contents of the properties that were saved to the object.">
		
		<!--- index the record being boosted --->
		<cfset var oContentType = application.fapi.getContentType("solrProContentType") />
		<cfset oContentType.addRecordToIndex(objectid = stProperties.objectid) />
		
		<cfreturn super.afterSave(argumentCollection = arguments) />
	</cffunction>
</cfcomponent>

Remember to do an updateapp or updateall after adding dmFile.cfc and COAPI deploy for the new property.

STEP 2 : solrProContentType.cfc

Contents (File in this case) need to be indexed to solr pro for search and if there are any search index value changes (eg. boosting value or elevation), index needs to be updated. addRecordToIndex method in solr pro adds individual content data to the search collection and It needs to be amended to check the new property, boostingValue in dmFile and do indexing process.

Extend solrProContentType.cfc in your project. eg. ./myproject/packages/type/solrProContentType.cfc

Copy the addRecordToIndex function from ./plugins/farcrysolrpro/packages/types/solrProContentType.cfc

Configure the document boost value getting part in the function as below

<!--- check if this record has a file level boost --->
<cfset oFile = application.fapi.getContentType("dmFIle")>
<cfset var docBoost = oFile.getBoostValueForFile(documentId = stRecord.objectid)>
<!--- check if this record has a document level boost configured at solr pro --->
<cfif isNumeric(docBoost) AND docBoost eq 0>
	<cfset var docBoost = arguments.oDocumentBoost.getBoostValueForDocument(documentId = stRecord.objectid)>
</cfif>
<!--- if there was no boost for the specific document, grab the default specified for the content type --->
<cfif not isNumeric(docBoost)>
	<cfset docBoost = arguments.stContentType.defaultDocBoost>
</cfif>

STEP 3 : Search Results

Now, notice the position of the file in result page, which will be placed ‘higher’ than before if you boosted the file. In addition, you can modify search result page (displaySolrSearchResult.cfm) to see the boosted files in high-lighted mode.

FarCry 6.x (Fandango) > dmFile Boosting for FarCry Solr Pro Search

2 Likes