arcpy field mapping for a spatial join: keep only specific columns
The above doesn't use field maps correctly; you instantiate a couple of them, but don't define anything in them. Field maps are useful when you want to consolidate/combine/concatenate/perform math on more than one input field for one output field.
If your aim is simply to keep certain wanted fields from the set of all input fields, the following should do the trick. This is, as @FelixIP suggests, merely a matter of removing the ones you don't want, but below, we remove them from the "menu" of possible output fields before performing the merge, rather than deleting them from the saved feature classes later (which would generally be much more time-consuming, since it typically involves reading and writing to the hard disk a bunch).
Before you conduct the spatial join:
fieldmappings = arcpy.FieldMappings()
# Add all fields from inputs.
fieldmappings.addTable(paramConsumerFeatures)
fieldmappings.addTable(paramNodeFeatures)
# Name fields you want. Could get these names programmatically too.
keepers = ["want_this_field", "this_too", "and_this"] # etc.
# Remove all output fields you don't want.
for field in fieldmappings.fields:
if field.name not in keepers:
fieldmappings.removeFieldMap(fieldmappings.findFieldMapIndex(field.name))
# Then carry on...
arcpy.SpatialJoin_analysis(......)
See also the "Merge example 2 (stand-alone script)" on this page: http://pro.arcgis.com/en/pro-app/tool-reference/data-management/merge.htm
As @FelixIP comments I think it is easier to drop unwanted fields. Here is what your code would look like, using the fieldInfo object:
import arcpy
def filter_fields(FC, fieldList):
# List input fields
fields= arcpy.ListFields(FC)
# Create a fieldinfo objects
fieldinfo = arcpy.FieldInfo()
# Iterate over input fields, add them to the FieldInfo and hide them if
# they aren't in the list of fields to be kept
for field in fields:
if not field.name in fieldList:
fieldinfo.addField(field.name, field.name, "HIDDEN", "")
# Copy features to a layer using the FieldInfo
temp = arcpy.Describe(FC).baseName + "_temp"
arcpy.MakeFeatureLayer_management(FC, temp, "", "", fieldinfo)
filter_fields(paramConsumerFeatures, ["ID", "a_value"])
filter_fields(paramNodeFeatures, ["ID", "name"])
arcpy.SpatialJoin_analysis(target_features = arcpy.Describe(paramConsumerFeatures).baseName + "_temp",
join_features = arcpy.Describe(paramNodeFeatures).baseName + "_temp",
out_feature_class = out,
join_operation = "JOIN_ONE_TO_ONE",
join_type = "KEEP_ALL",
match_option="CLOSEST",
distance_field_name="join_dist")