Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/dart/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用apachelucene进行搜索_Java_Lucene - Fatal编程技术网

Java 使用apachelucene进行搜索

Java 使用apachelucene进行搜索,java,lucene,Java,Lucene,我正在使用ApacheLucene搜索文件中的字符串。lucene使用什么样的语法分析。如果我搜索obama,它不会返回Presobama的结果,而会返回obama的结果。谁能告诉我为什么?我正在使用TextField StandardAnalyzer analyzer = new StandardAnalyzer(Version.LUCENE_44); // Code to create the index Directory index

我正在使用ApacheLucene搜索文件中的字符串。lucene使用什么样的语法分析。如果我搜索obama,它不会返回Presobama的结果,而会返回obama的结果。谁能告诉我为什么?我正在使用TextField

         StandardAnalyzer analyzer = new StandardAnalyzer(Version.LUCENE_44);

        //  Code to create the index
        Directory index = new RAMDirectory();

        IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_44, analyzer);

        IndexWriter w = new IndexWriter(index, config);
        addDoc(w, finalstep);

        w.close();
                    String querystr =  search;

        //  The \"title\" arg specifies the default field to use when no field is explicitly specified in the query
        Query q = new QueryParser(Version.LUCENE_44, "title", analyzer).parse(querystr);

        // Searching code
        int hitsPerPage = 10;
        IndexReader reader = DirectoryReader.open(index);
        IndexSearcher searcher = new IndexSearcher(reader);
        TopScoreDocCollector collector = TopScoreDocCollector.create(hitsPerPage, true);
        searcher.search(q, collector);
        ScoreDoc[] hits = collector.topDocs().scoreDocs;

分析器指示如何将文本分隔为标记。您正在使用
StandardAnalyzer

StandardAnalyzer
通常尝试将流分离为单词。它使用的规则是在中完整指定的,但说起来非常粗略:它在空格和标点处分隔标记

这个“奥巴马”变成了“奥巴马”。“#”将在分析中删除。“总统奥巴马”将成为“总统奥巴马”。目前的解析规则对“总统奥巴马”一词一无所知,也没有理由认为它应该被视为一个以上的词

有很多方法可以使匹配更加松散。有几种可能性:您可以使用,使用索引来索引标记的Ngram,或者,如果您只有一些这样麻烦的术语,您可以使用索引来指定同义词替换