如何为范围构面索引数值?

时间:2016-11-02 12:52:41

标签: lucene hibernate-search

我的项目有产品。产品有属性。可以动态创建属性。我需要范围方面的属性。但没有数字刻板。下面的示例仅适用于字符串分面。如何获取属性的数值范围方面?

//product entity
@Indexed
public class Product{
    // ommited
    @Field(analyze = Analyze.NO, bridge = @FieldBridge(impl = AttrFieldBridge.class))
    private List<Attribute> attributes;
    // ommited
}

// attribute bridge used in Product
public class AttrFieldBridge implements FieldBridge {

public void set(String name, Object value, Document document, LuceneOptions luceneOptions) {
    if (value != null) {
        @SuppressWarnings("unchecked")
        List<Attribute> attrs = (List<Attribute>) value;
        for (Attribute attr : attrs){
            String fieldName = name+"_"+attr.getAttributeId();
            double fieldValue = attr.getVal();

            document.add(new SortedSetDocValuesFacetField(fieldName, Double.toString(fieldValue))); // here I need something like SortedSetDocNumericValuesFacetField
            document.add(new DoubleField(fieldName, fieldValue, Field.Store.NO));
        }
    }
}

}

1 个答案:

答案 0 :(得分:0)

警告

首先,知道Hibernate Search(尚未)支持桥接定义字段的分面。如果是,您不必自己添加分面字段。您可以NSURL documentation要求此类支持。

现在,您可以尝试通过自己索引分面字段来解决这个问题,并希望查询方面能够正常工作。如果它可以工作,那么Hibernate Search的使用将不受支持,这意味着它可能只会在Hibernate Search的一个小更新时中断。

解决方案

要正确地为您的分面字段编制索引,您应该使用// attribute bridge used in Product public class AttrFieldBridge implements FieldBridge { public void set(String name, Object value, Document document, LuceneOptions luceneOptions) { if (value != null) { @SuppressWarnings("unchecked") List<Attribute> attrs = (List<Attribute>) value; for (Attribute attr : attrs){ String fieldName = name+"_"+attr.getAttributeId(); double fieldValue = attr.getVal(); document.add(new DoubleDocValuesField(fieldName, fieldValue)); document.add(new DoubleField(fieldName, fieldValue, Field.Store.NO)); } } }

    For i As Integer = 0 To GridView1.DataRowCount - 1

        Dim OriginSealNumber As String = BlGridview1.Tables(0).Rows(i)("SEALNUMBER").ToString()
        Dim OriginContainerNumber As String = BlGridview1.Tables(0).Rows(i)("CONTAINERNUMBER").ToString()
        Dim OriginContainerType As String = BlGridview1.Tables(0).Rows(i)("CONTAINERTYPE").ToString()
        Dim OriginQuantity As String = BlGridview1.Tables(0).Rows(i)("QUANTITY").ToString()
        Dim OriginPackageType As String = BlGridview1.Tables(0).Rows(i)("PACKAGETYPE").ToString()
        Dim OriginDescription As String = BlGridview1.Tables(0).Rows(i)("DESCRIPTION").ToString()
        Dim OriginWeight As String = BlGridview1.Tables(0).Rows(i)("WEIGHT").ToString()
    Next i

来源:create a JIRA ticket