Hi,
I was reading that when you use a wildcard query, then the filters in a Analyzer are not used, so I suspected this was the case.
I managed to get the sorting working using a custom SortComparator by extending the SortComparator, however I am now receiving a null pointer exception, because not all of the fields have values.
Here was my existing code before I implemented the SortComparator:
Code:
Entity
@Indexed
@Table(name = "ASSET")
@AnalyzerDef(name = "TycoAnalyzer",
tokenizer = @TokenizerDef(factory = StandardTokenizerFactory.class),
filters = { @TokenFilterDef(factory = LowerCaseFilterFactory.class) }
)
public class Asset extends ViewableEntity implements Comparable<Asset>
{
/** Serial version UID. */
private static final long serialVersionUID = 3889097101406806622L;
/** List of asset attributes. */
@IndexedEmbedded
@OneToMany(mappedBy = "asset", cascade = CascadeType.REMOVE)
@Fetch(FetchMode.JOIN)
private List<Attribute> attributes = new ArrayList<Attribute>();
/** The type of asset this is. */
@IndexedEmbedded
@ManyToOne
private AssetType assetType;
/** Current GPS position. */
@Column(name = "gps", length = Constants.VARCHAR_128)
private String gps;
/** <code>Date</code> the <code>Asset</code> was installed in the building. */
@Column(name = "installation_date")
private Date installationDate;
/** The current location in the building. */
@Column(name = "location", length = Constants.VARCHAR_128, nullable=false)
@Fields({
@Field(index = Index.TOKENIZED, store = Store.YES),
@Field(name = "location_forSort", index = Index.UN_TOKENIZED, store = Store.NO)
})
private String location = "";
... Rest of class ommitted.
Search Code
Code:
FullTextQuery results = getDaoFacade().getAssetDao().searchForAssetsInIndex(query,
fullTextSession,
analyzer,
searchResult.getStart(),
searchResult.getNoOfResultsToReturn());
Code:
@Override
public FullTextQuery searchForAssetsInIndex(Query query, FullTextSession fullTextSession, Analyzer analyzer, int beginResult, int noOfRows)
{
//We need to use projection as we want to highlight terms in the results.
FullTextQuery squery = fullTextSession.createFullTextQuery(query, Asset.class);
squery.setProjection(FullTextQuery.THIS, FullTextQuery.DOCUMENT_ID, FullTextQuery.DOCUMENT, FullTextQuery.SCORE);
squery.setFirstResult(beginResult); //Where should we start the results from?
squery.setMaxResults(noOfRows); //How many results should we return?
return squery;
}
Sort Code
Code:
results.setSort(new Sort(new SortField(searchResult.getSortingColumnName() + "_forSort", searchResult.isReverseOrder())));