为什么搜索者没有返回结果?

时间:2016-12-22 08:07:54

标签: solr lucene

我使用solr创建了一个索引。我正在尝试执行以下代码,但计数结果为零。

DirectoryReader dr = DirectoryReader.open(FSDirectory.open(new File(indexDir).toPath()));
IndexSearcher searcher = new IndexSearcher( dr );

System.out.println(dr.maxDoc()); // Shows 2000000
Query query = new FieldValueQuery("table");
CollectionStatistics stats = searcher.collectionStatistics("table");
System.out.println(stats.docCount()); // Shows 2000000

System.out.println(searcher.count(query)); //Shows 0, should be 2000000

schema.xml中提交的表的定义是:

<field name="table" type="string" indexed="true" stored="true" required="true" multiValued="false"/>

将文档添加到索引的代码是:

SolrInputDocument doc = new SolrInputDocument();
doc.addField("table", "job", 1.0f);
solrclient.add(doc);
solrclient.commit();

任何想法,为什么会发生这种情况?为什么使用FieldValueQuery进行搜索没有返回正确的结果?

非常感谢你。

1 个答案:

答案 0 :(得分:0)

为了测试代码提供程序,我尝试针对现有索引运行它,并在索引中使用不同类型的字段替换字段的名称。 我有一个带有以下schema.xml的索引:

>>> vocabulary =  {'car': 1, 'read': 3, 'yellow': 2}
>>> weight = [23,445,56]
>>> vocabulary_weight = {key: weight[value - 1] for key, value in vocabulary.items()}
>>> vocabulary_weight
{'car': 23, 'read': 56, 'yellow': 445}

当我按以下方式尝试您的代码时:

<field name="tag" type="string" indexed="true" stored="true" multiValued="true" docValues="true" />
<field name="author" type="string" indexed="true" stored="true" multiValued="true" />

结果是0个文件。 然后我尝试使用我的模式中的其他字段(我在上面复制了它的子集),在FieldValueQuery中替换它们,如:

DirectoryReader dr = DirectoryReader.open(FSDirectory.open(directory.toPath()));
    IndexSearcher searcher = new IndexSearcher( dr );
    Query query = new FieldValueQuery("author");
    System.out.println(searcher.count(query));

并出现了正确的计数(对我来说是47000)。

  

这意味着searcher.count 取决于DocValues ,因此更改   以下方式的架构将解决您的问题:

Query query = new FieldValueQuery("tag");

如果您不想要,或者您无法更改您的架构,那么您可以始终依靠旧的方式来计算以下文档:

<field name="table" type="string" indexed="true" stored="true" required="true" multiValued="false" docValues="true"/>
相关问题