Skip to content

Instantly share code, notes, and snippets.

@nshaw
Created July 24, 2012 12:18
Show Gist options
  • Save nshaw/3169628 to your computer and use it in GitHub Desktop.
Save nshaw/3169628 to your computer and use it in GitHub Desktop.
Adding boost factors at runtime
for (String assetType : assetTypes) {
BooleanQuery assetTypeQuery = BooleanQueryFactoryUtil.create(context);
assetTypesQuery.add(assetTypeQuery, BooleanClauseOccur.SHOULD);
if( (assetBoostFactors != null)
&& (index <= assetBoostFactors.length)) {
int boost = assetBoostFactors[index];
Class clazz = assetTypesQuery.getClass();
//Note: can't do instanceOf since BQI is in portal-impl
if ("com.liferay.portal.search.lucene.BooleanQueryImpl".equals(clazz.getName())) {
try {
java.lang.reflect.Field field = clazz.getDeclaredField("_booleanQuery");
field.setAccessible(true);
Object bq = field.get(assetTypeQuery);
java.lang.reflect.Field field2 = bq.getClass().getSuperclass().getDeclaredField("boost");
field2.setAccessible(true);
field2.setFloat(bq, boost);
}
catch (Exception e) {
_log.debug("Unable to set boost: ", e);
}
}
else {
_log.debug("Wrong implementation, cannot set boost: " + clazz.getName());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment