我正在尝试在Compass索引中对最近的项目进行查询时间提升。我已经尝试在我的类映射上使用属性,但这似乎只影响索引时间内的提升,而不是查询时间。
有什么想法吗?
答案 0 :(得分:0)
DefaultCompassQuery query = (DefaultCompassQuery) compassBuilderQuery.toQuery();
query.setTypes( types.toArray( new Class[types.size()] ) );
LuceneSearchEngineQuery searchEngineQuery = (LuceneSearchEngineQuery) query.getSearchEngineQuery();
final SimpleDateFormat sdf = new SimpleDateFormat( "yyyyMMddHHmmss" );
final long timeInMillis = Calendar.getInstance().getTimeInMillis();
ValueSourceQuery valSrcQuery = new ValueSourceQuery( new ValueSource() {
private static final long serialVersionUID = 1L;
@Override
public int hashCode() {
return System.identityHashCode( this );
}
@Override
public DocValues getValues( final IndexReader reader ) throws IOException {
return new DocValues() {
@Override
public float floatVal( int doc ) {
try {
Document document = reader.document( doc );
Field field = document.getField( "date" );
if (null != field) {
Date parse = sdf.parse( field.stringValue() );
long t = timeInMillis - parse.getTime();
float f = (1.0f / (t * (1.0f / TimeUnit.DAYS.toMillis( 30 )) + 1.0f));
if (logger.isDebugEnabled()) {
logger.debug( "Date match: " + parse.toString() );
logger.debug( "Calculated date boost as: " + f + " for doc id: " + doc );
}
return f;
}
} catch (CorruptIndexException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ParseException e) {
e.printStackTrace();
}
return 1.0f;
}
@Override
public String toString( int doc ) {
return description() + "=" + strVal( doc );
}
};
}
@Override
public boolean equals( Object o ) {
return this == o;
}
@Override
public String description() {
return "[boost: date]";
}
} );
CustomScoreQuery sq = new CustomScoreQuery( searchEngineQuery.getQuery(), valSrcQuery );
searchEngineQuery.setQuery( sq );
其中'date'是您要提升的文档上的日期字段,compassBuilderQuery是您使用指南针查询构建器生成的查询。您还可以在那里调整'30',以根据年龄增加日期。