【问题标题】:Getting results of a search - lucene 4.4.0获取搜索结果 - lucene 4.4.0
【发布时间】:2013-08-09 19:41:49
【问题描述】:

我在 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);
                }

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

 +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(); 

收集数据有什么问题吗??

亲切的问候

【问题讨论】:

  • 我们如何创建 booleanQuery?你能证明吗?
  • JtheRocker,我已经编辑了我的问题;)
  • 所以,请确认您在 DESIGNACAO 字段中搜索“不”,对吧?
  • 是的。我'。尝试在所有字段中搜索“非或”并在所有字段中搜索“非”或“或”:DESIGNACAO、TAG 和 DESCICAO。你看,我试图找到与所有句子“not or”或分隔词的任何匹配项。 ;)
  • 什么是查询=?我的意思是你要拆分的字符串?

标签: java lucene search-engine


【解决方案1】:

小菜一碟。问题在于查询的构造:

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

BooleanClause.Occur.MUST 表示它必须存在。因此,我添加到布尔查询中的所有术语都必须存在(term1 AND term2 AND term3)。正确的是:

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

这样我可以说必须存在我添加的那些术语之一(term1 OR term2 OR term3)。

亲切的问候

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-03-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多