--[[----------------------------------------------------------------------------

WFCatSortByOnlineRelevance.lua

--------------------------------------------------------------------------------

ADOBE CONFIDENTIAL
------------------
Copyright 2015 Adobe Systems Incorporated
All Rights Reserved.

NOTICE: All information contained herein is, and remains the property of
Adobe Systems Incorporated and its suppliers, if any. The intellectual and
technical concepts contained herein are proprietary to Adobe Systems
Incorporated and its suppliers and are protected by all applicable
intellectual property laws, including trade secret and copyright laws.
Dissemination of this information or reproduction of this material is
strictly forbidden unless prior written permission is obtained from
Adobe Systems Incorporated.

----------------------------------------------------------------------------]]--
local WFCatSearchAndSortConstants = require "WFCatSearchAndSortConstants"
--==============================================================================

local WFCatSortByOnlineRelevance = {}
local tableName = WFCatSearchAndSortConstants.ONLINESEARCH_TEMP_TABLE_NAME

--------------------------------------------------------------------------------

function WFCatSortByOnlineRelevance.executeSort( wfCatalog, assetLocalIds, order )

	local sqlString = string.format( 
			[[ SELECT DISTINCT a.docId, a.assetId FROM assets a   
				INNER JOIN CARRAY( ?, ?, 'int32' ) AS assetLocalIds ON assetLocalIds.value == a.docId
				INNER JOIN %s s ON a.assetId == s.docId 
				ORDER BY s.relevancyScore %s ]], 
			tableName, order )
						
	-- we need docId's in the table
	return wfCatalog:indexStore():query( {
			sql = sqlString,
			calm = 1,
			assetLocalIds,
			#assetLocalIds
		} )
end

--==============================================================================

return WFCatSortByOnlineRelevance
