使用 Lucene 进行精确短语搜索?

2024-04-19

我正在使用 SpanTerm Query 在 lucene 中搜索确切的短语。但这似乎不起作用。这是我的代码。

Indexing

IndexWriter writer = new IndexWriter(dir, new StandardAnalyzer(Version.LUCENE_30), false,IndexWriter.MaxFieldLength.UNLIMITED);  
doc.add(new Field("contents", sb.toString(), Field.Store.YES, Field.Index.ANALYZED,Field.TermVector.WITH_POSITIONS_OFFSETS));
doc.add(new Field("imageid", imageDocument.getImageId(), Field.Store.YES, Field.Index.NOT_ANALYZED));
doc.add(new Field("title", imageDocument.getTitle(), Field.Store.YES, Field.Index.ANALYZED));
doc.add(new Field("country", imageDocument.getCountry(), Field.Store.YES, Field.Index.NOT_ANALYZED));
write.addDocument(doc);

搜寻中

String sentence = searchParameters.get("searchExactWord");
String[] words = sentence.split(" ");
String queryNoWord = "";
int i = 0;
SpanTermQuery [] clause = new SpanTermQuery[words.length];
for (String word : words)
{
    clause[i] = new SpanTermQuery(new Term("contents",word));
    i++;
}
SpanNearQuery query = new SpanNearQuery(clause, 0, true);
booleanQuery.add(query, BooleanClause.Occur.MUST);

如果我做错了请指导我???

Prateek


Try a PhraseQuery http://lucene.apache.org/java/3_0_3/api/core/org/apache/lucene/search/PhraseQuery.html反而:

PhraseQuery query = new PhraseQuery();
String[] words = sentence.split(" ");
for (String word : words) {
    query.add(new Term("contents", word));
}
booleanQuery.add(query, BooleanClause.Occur.MUST);

Edit:我认为你有不同的问题。您还有哪些其他部分booleanQuery?这是搜索短语的完整工作示例:

public class LucenePhraseQuery {
    public static void main(String[] args) throws Exception {
        // setup Lucene to use an in-memory index
        Directory directory = new RAMDirectory();
        Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_30);
        MaxFieldLength mlf = MaxFieldLength.UNLIMITED;
        IndexWriter writer = new IndexWriter(directory, analyzer, true, mlf);

        // index a few documents
        writer.addDocument(createDocument("1", "foo bar baz"));
        writer.addDocument(createDocument("2", "red green blue"));
        writer.addDocument(createDocument("3", "test foo bar test"));
        writer.close();

        // search for documents that have "foo bar" in them
        String sentence = "foo bar";
        IndexSearcher searcher = new IndexSearcher(directory);
        PhraseQuery query = new PhraseQuery();
        String[] words = sentence.split(" ");
        for (String word : words) {
            query.add(new Term("contents", word));
        }

        // display search results
        TopDocs topDocs = searcher.search(query, 10);
        for (ScoreDoc scoreDoc : topDocs.scoreDocs) {
            Document doc = searcher.doc(scoreDoc.doc);
            System.out.println(doc);
        }
    }

    private static Document createDocument(String id, String content) {
        Document doc = new Document();
        doc.add(new Field("id", id, Store.YES, Index.NOT_ANALYZED));
        doc.add(new Field("contents", content, Store.YES, Index.ANALYZED,
                Field.TermVector.WITH_POSITIONS_OFFSETS));
        return doc;
    }
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用 Lucene 进行精确短语搜索? 的相关文章

随机推荐

  • 使用嵌套 delegate_to 将文件从一台远程服务器复制到另一台远程服务器

    作为用户 我想将文件从节点1复制到节点2 是否可以使用复制模块 delegate to 以下是我试图做的事情 Playbook 从节点 3 运行 Playbook Sample name Gather Facts for all hosts
  • 如何在不指定类型的情况下引用 Java 枚举

    我有一个类定义了自己的枚举 如下所示 public class Test enum MyEnum E1 E2 public static void aTestMethod Test2 E1 lt lt Gives E1 cannot be
  • 如何在Codeigniter中插入动态数据?

    I just want to insert dynamic generated input field data into database My db table having three fields id Auto Increment
  • 在Python中有效地找到scipy/numpy中非零的区间?

    假设我有一个 python 列表或一个 python 一维数组 以 numpy 表示 假设存在连续的元素延伸 如何找到此列表或数组中非零延伸的开始和结束坐标 即索引 例如 a 0 0 0 0 1 2 3 4 nonzero coords a
  • 文本和边框之间的富文本框填充

    是否可以在文本和边框之间的富文本框控件中添加填充 我尝试将一个富文本框停靠在面板内 并将所有四个边的填充设置为 10 这实现了我想要的效果 除非需要富文本框的垂直滚动条 该滚动条也会被填充 有EM GETRECT https learn m
  • 如何在 blogdown 中使用 bibtex 进行引用?

    我想在使用 R 包 blogdown 创建的静态网站页面上使用引用 基于关于 blogdown 的书 https bookdown org yihui blogdown https bookdown org yihui blogdown 这
  • Django - DRF - 调度方法流程

    我正在与 DRF 合作构建一个 API 并使用主类对基于类的视图进行一些验证 class MasterClass APIView def dispatch self request args response super FaveoAPIV
  • 将回调地狱转换为延迟对象

    背景 所以 我有一个相当大的项目 有很多 API 函数 我正在考虑完全转向协程 但由于它们的实现方式是Callback并不是Deferred 我无法有效地使用它们 例如 我想做apiCallOne apiCallTwo and apiCal
  • SqlBulkCopy 的建议批量大小是多少?

    建议的批量大小是多少SqlBulkCopy 我正在寻找一个可以用作性能调整起点的通用公式 我有一个导入实用程序与我的 SQL Server 实例位于同一台物理服务器上 使用自定义IDataReader 它解析平面文件并将它们插入数据库中SQ
  • Vim:如何将一组行重新格式化为一行(如果该行是一个句子)?

    不是重复的在 Vim 中 将文件中的所有行连接成一行的最简单方法是什么 https stackoverflow com questions 391710 in vim what is the simplest way to join all
  • 线段-多边形相交

    问候 我想检测一个线段是否只 接触 多边形或穿过它 人物 解释了我的疑问 如何知道情况A和B的区别 请注意 在这两种情况下 红线在两个顶点处穿过多边形 一个顶点与外部相接触 另一个顶点与内部相交 我有一个段 段相交算法 但我不知道如何正确使
  • 使用 iText 段落之间的图像

    我正在使用 iText 生成自定义 pdf 文档 我尝试了很多 但无法获得包含图像的文本的所需设计 我需要如下所示的输出 我尝试过 Chunk 类和 Paragraph 类 但我无法获得所需的结果 有任何想法吗 你有 至少 两个选择 Use
  • MySQL select for update 返回空集,即使存在一行

    我发现 MySQL 的 选择更新 有一个奇怪的问题 我使用的是5 1 45版本 我有两张桌子 mysql gt show create table tag Tabl
  • 在 Mac 上打开 CSV 文件时出现错误 53

    当我尝试打开 CSV 文件时 我得到 错误 53 找不到文件 我在第四行收到错误 Open FilePath For Input As 1我究竟做错了什么 这是我第一次打开 CSV 请宽容我的代码 Sub opentextfile Dim
  • “Android”中的所见即所得视图编辑器?

    复制 有适用于 Google Android 的表单设计器吗 https stackoverflow com questions 1755860 我想移动一个复选框 以便它显示在与 main xml 内绝对布局下的左上角不同的位置 对于 A
  • 这个文件格式叫什么

    我需要解析以下格式的文件 General Description Some Text Version 4 ProjType 1 Configurations Mice BuildOutputs BuildProject OutputFile
  • 更改背景颜色

    好吧 我对 vim 还很陌生 我不知道如何更改背景颜色 我正在编辑 vimrc 文件来设置这些颜色 但找不到任何背景颜色 我正在使用一个配色方案 我只需要知道如何覆盖它或者要查找什么 以便我可以在我的 color theme vim 文件中
  • 如何让 NSView 不裁剪其边界区域?

    我在 Xcode 上为 OS X 创建了一个空的 Cocoa 应用程序 并添加了 void applicationDidFinishLaunching NSNotification aNotification self view NSVie
  • Android Studio 布局编辑器无法渲染自定义视图

    在 Android Studio 中 布局编辑器无法预览 xml 中的自定义视图 非常简单的例子 public class MyCustomView extends FrameLayout public MyCustomView Conte
  • 使用 Lucene 进行精确短语搜索?

    我正在使用 SpanTerm Query 在 lucene 中搜索确切的短语 但这似乎不起作用 这是我的代码 Indexing IndexWriter writer new IndexWriter dir new StandardAnaly