Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/search/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何使用TermVector Lucene 4.0_Java_Search_Lucene - Fatal编程技术网

Java 如何使用TermVector Lucene 4.0

Java 如何使用TermVector Lucene 4.0,java,search,lucene,Java,Search,Lucene,在索引方法中,我使用以下行: Field contentsField = new Field("contents", new FileReader(f), Field.TermVector.YES); 但是,在Lucene 4.0中,此构造函数已被弃用,应使用新文本字段,而不是新字段 但是TextField的问题是它不接受其构造函数中的TermVector 有没有办法在Lucene 4.0的索引中使用新的构造函数包含术语向量 感谢TextField是一个方便的类,适用于需要无术语向量的索引字段

在索引方法中,我使用以下行:

Field contentsField = new Field("contents", new FileReader(f), Field.TermVector.YES);
但是,在Lucene 4.0中,此构造函数已被弃用,应使用
新文本字段
,而不是
新字段

但是
TextField
的问题是它不接受其构造函数中的
TermVector

有没有办法在Lucene 4.0的索引中使用新的构造函数包含术语向量


感谢

TextField是一个方便的类,适用于需要无术语向量的索引字段的用户。如果需要术语向量,只需使用。由于需要先创建的实例,需要多行代码,请将
storeTermVectors
tokenizer
设置为true,然后在
Field
构造函数中使用此
FieldType
实例。

我也有同样的问题,所以我只是创建了自己的字段:

public class VecTextField extends Field {

/* Indexed, tokenized, not stored. */
public static final FieldType TYPE_NOT_STORED = new FieldType();

/* Indexed, tokenized, stored. */
public static final FieldType TYPE_STORED = new FieldType();

static {
    TYPE_NOT_STORED.setIndexed(true);
    TYPE_NOT_STORED.setTokenized(true);
    TYPE_NOT_STORED.setStoreTermVectors(true);
    TYPE_NOT_STORED.setStoreTermVectorPositions(true);
    TYPE_NOT_STORED.freeze();

    TYPE_STORED.setIndexed(true);
    TYPE_STORED.setTokenized(true);
    TYPE_STORED.setStored(true);
    TYPE_STORED.setStoreTermVectors(true);
    TYPE_STORED.setStoreTermVectorPositions(true);
    TYPE_STORED.freeze();
}

// TODO: add sugar for term vectors...?

/** Creates a new TextField with Reader value. */
public VecTextField(String name, Reader reader, Store store) {
    super(name, reader, store == Store.YES ? TYPE_STORED : TYPE_NOT_STORED);
}

/** Creates a new TextField with String value. */
public VecTextField(String name, String value, Store store) {
    super(name, value, store == Store.YES ? TYPE_STORED : TYPE_NOT_STORED);
}

/** Creates a new un-stored TextField with TokenStream value. */
public VecTextField(String name, TokenStream stream) {
    super(name, stream, TYPE_NOT_STORED);
}
}


希望这能有所帮助

有一段时间我被这个问题难住了。这里的其他答案很有帮助,但即使有了它们,情况对我来说也不明显。所以,在我的灯终于亮了之后,我决定添加这个额外的答案,让下一个人看得更清楚一些

支持术语向量的
字段
签名被折旧的原因是,它使用了自Lucene 4.0起折旧的
字段.TermVector枚举

在Lucene 4.0中,一个新的方法签名被添加到
字段
类中,该类支持传递一个
字段类型
FieldType
类比旧的
enum
方法更灵活,并且提供了设置以前可用的更多字段选项的能力

下面是一个示例,说明如何创建一个未存储的文本字段,该字段在实例化
字段
对象时通过传递
字段类型
对象来支持术语向量

     FieldType specialTextFieldType = new FieldType(TextField.TYPE_NOT_STORED);
     specialTextFieldType.StoreTermVectors = true;

     Document exampleDoc = new Document();
     exampleDoc.Add(new Field("SomeField", someData, specialTextFieldType ));