Lucene查询不会返回结果,即使它应该返回结果
我目前正在尝试从RamDirectory中的Lucene索引(v.4)获取所有文档 在创建索引时,使用以下addDocument函数:Lucene查询不会返回结果,即使它应该返回结果,lucene,Lucene,我目前正在尝试从RamDirectory中的Lucene索引(v.4)获取所有文档 在创建索引时,使用以下addDocument函数: public void addDocument(int id, String[] values, String[] fields) throws IOException{ Document doc = new Document(); doc.add(new IntField("getAll", 1, IntField.TYPE_STORED))
public void addDocument(int id, String[] values, String[] fields) throws IOException{
Document doc = new Document();
doc.add(new IntField("getAll", 1, IntField.TYPE_STORED));
doc.add(new IntField("ID", id, IntField.TYPE_STORED));
for(int i = 0; i < fields.length; i++){
doc.add(new TextField(fields[i], values[i], Field.Store.NO));
}
writer.addDocument(doc);
}
public void addDocument(int-id,String[]值,String[]字段)引发IOException{
单据单据=新单据();
添加文档(新的IntField(“getAll”,1,IntField.TYPE_存储));
文件添加(新的IntField(“ID”,ID,IntField.TYPE_存储));
for(int i=0;i
对所有文档调用此命令后,编写器将关闭。
从添加到文档的第一个字段可以看到,我添加了一个额外的字段“getAll”,以便于检索所有文档。如果我理解正确,查询“getAll:1”应该返回索引中的所有文档。但事实并非如此。
我正在为此使用以下功能:
public List<Integer> getDocIds(int noOfDocs) throws IOException, ParseException{
List<Integer> result = new ArrayList<Integer>(noOfDocs);
Query query = parser.parse("getAll:1");
ScoreDoc[] docs = searcher.search(query, noOfDocs).scoreDocs;
for(ScoreDoc doc : docs){
result.add(doc.doc);
}
return result;
}
public List getdocid(int noOfDocs)抛出IOException、ParseException{
列表结果=新的ArrayList(noOfDocs);
Query=parser.parse(“getAll:1”);
ScoreDoc[]docs=searcher.search(查询,noOfDocs).scoreDocs;
用于(ScoreDoc文档:文档){
结果.添加(doc.doc);
}
返回结果;
}
noOfDocs是已编制索引的文档数。当然,我在创建IndexSearcher时使用了相同的RamDirectory。
将解析后的查询替换为手动创建的TermQuery也没有帮助。
查询不返回任何结果
希望有人能帮我找出错误。
谢谢我认为首先您应该运行以验证索引的内容
另外,如果允许使用
queryParser.setAllowReadingWildcard(true)将*作为查询的第一个字符
,然后像ID::
这样的查询将检索所有文档,而不必包含getAll字段。我认为首先应该运行以验证索引的内容
另外,如果允许使用
queryParser.setAllowReadingWildcard(true)将*作为查询的第一个字符
,然后像ID:
这样的查询将检索所有文档,而不必包含getAll字段。我相信您在搜索时遇到了问题,因为您使用的是IntField,而不是StringField或TextField,例如。IntField和其他数字字段是为数字范围查询而设计的,不以原始形式编制索引。您可以使用来搜索它们
实际上,在我看来,IntField应该只用于数值,而不是用于数字串,这似乎就是你所拥有的。ID通常应该是关键字或文本字段
至于提取所有记录,不需要添加字段即可。只需使用。我相信您在搜索时遇到了问题,因为您使用的是IntField,而不是StringField或TextField。IntField和其他数字字段是为数字范围查询而设计的,不以原始形式编制索引。您可以使用来搜索它们 实际上,在我看来,IntField应该只用于数值,而不是用于数字串,这似乎就是你所拥有的。ID通常应该是关键字或文本字段
至于提取所有记录,不需要添加字段即可。只需使用一个。MatchAllQuery就可以了还感谢您提供有关Int-Field的信息,我将更改MatchAllQuery使其工作还感谢您提供有关Int-Field的信息,我将更改它