Getting Started > Use Lua Scripts > Use Lua Scripts Within the Connector > Example Lua Script

Example Lua Script
You can use the CollectActions parameter of the Collect action, the IngestActions parameter of the Synchronize action and the IngestActions parameter in the configuration file to specify a Lua script that runs on each document.
The Lua script takes the following parameters:
 
*
TYPE. The type of the command being performed. This can be ADD, UPDATE, DELETE, or COLLECT.
*
SECTION. The configuration section for the task.
*
FILENAME. The document filename. The Lua script may modify this file, but should not delete it.
The configuration file object provides the following methods:
getValue(section, parameter, default)
The document object provides the following methods:
*
addField(fieldname, value)
*
appendContent(additionalContent)
*
setFieldValue(fieldname, value)
*
writeStubIdx(outputFilename)
*
getFieldValue(fieldname)
*
hasField(fieldname)
*
renameField(oldFieldName, newFieldName)
*
copyField(sourceFieldName, targetFieldName)
*
deleteField(fieldname)
*
getFieldValues(fieldname)
An example Lua script appears below:
method handler( config, document, params )
-- If these lines are uncommented, and the connector is running -- from the console, all the parameters in params will be output -- to the console.
-- for k,v in pairs(params) do
-- print(k,v)
-- end
-- Sets local variables from the parameters passed in.
local type = params["TYPE"]
local section = params["SECTION"]
local filename = params["FILENAME"]
-- Read a config setting from the config file.
local val = config:getValue(section, "ConfigSettingName", "Value")
-- If the document is not being deleted, set the field FieldName -- to the value read from the config file.
if type ~= "DELETE" then
document:setFieldValue("FieldName", val)
end
-- If this document has a file (that is, not just metadata),
-- copy the file to a new location and write a stub idx
-- containing the metadata with it.
if filename ~= "" then
copytofilename = "OutputPath/"..create_uuid(filename)
copy_file(filename, copytofilename)
document:writeStubIdx(copytofilename..".idx")
end
return true
end
 
NOTE The Lua script should return true normally, but can return false to reject the document when used as an Ingest action.