0

我在 lucene 上遇到了一些问题。我正在查询一个数据库。据我所知,索引没问题(我用 lukeall-4.4.0 检查过)。查询构造如下:

                Q = Query.split(" ");

                booleanQuery = new BooleanQuery();

                //Query[] Queryy = new Query[5 + 5 * Q.length];
                Query[] Queryy = new Query[3 + 3*Q.length];

                //USING THE ALL TEXT
                Queryy[0] = new TermQuery(new Term("DESIGNACAO", Query));
                Queryy[1] = new TermQuery(new Term("DESCRICAO", Query));
                Queryy[2] = new TermQuery(new Term("TAG", Query));

                //USING THE SEPARETED VALUES 
               for (int i = 3, j = 0; j < Q.length; i++, j++) {

                    Queryy[i] = new TermQuery(new Term("DESIGNACAO", Q[j]));
                    Queryy[++i] = new TermQuery(new Term("DESCRICAO", Q[j]));
                    Queryy[++i] = new TermQuery(new Term("TAG", Q[j]));

                }

                for (int i = 0; i < Queryy.length; i++) {
                    booleanQuery.add(Queryy[i], BooleanClause.Occur.MUST);
                }

查询正常。为了搜索“非或”,查询(布尔查询)将如下所示:

 +DESIGNACAO:not or  +DESCRICAO:not or  +TAG:not or  +DESIGNACAO:not +DESCRICAO:not +TAG:not +DESIGNACAO:or +DESCRICAO:or +TAG:or

我正在使用 SimpleAnalyser,因此单词 not 和 or 不会被删除。问题是我无法命中。如果我使用 lukeall-4.4.0 而不是我的代码进行搜索,我只能获得命中。我的搜索方法如下:

IndexReader reader = IndexReader.open(directory1);
        TopScoreDocCollector collector = TopScoreDocCollector.create(50, true);
        searcher = new IndexSearcher(reader);
        searcher.search(booleanQuery, collector);
        hits = collector.topDocs().scoreDocs;
        int total = collector.getTotalHits();
        displayResults(); 

收集数据有什么问题吗?

亲切的问候

4

1 回答 1

2

Piece of cake. The problem was in the construction of the query:

for (int i = 0; i < Queryy.length; i++) {
    booleanQuery.add(Queryy[i], BooleanClause.Occur.MUST);
}

The BooleanClause.Occur.MUST means that this has to exist. Thus, all the terms i was adding to the booleanquery must exist (term1 AND term2 AND term3). The correct is:

booleanQuery.add(Queryy[i], BooleanClause.Occur.SHOULD);

This way I can say that has to exist one of those terms I have added (term1 OR term2 OR term3).

Kind regards

于 2013-08-10T11:17:07.907 回答