<small id='FSxvV'></small><noframes id='FSxvV'>

        <bdo id='FSxvV'></bdo><ul id='FSxvV'></ul>
        <tfoot id='FSxvV'></tfoot>
        <i id='FSxvV'><tr id='FSxvV'><dt id='FSxvV'><q id='FSxvV'><span id='FSxvV'><b id='FSxvV'><form id='FSxvV'><ins id='FSxvV'></ins><ul id='FSxvV'></ul><sub id='FSxvV'></sub></form><legend id='FSxvV'></legend><bdo id='FSxvV'><pre id='FSxvV'><center id='FSxvV'></center></pre></bdo></b><th id='FSxvV'></th></span></q></dt></tr></i><div id='FSxvV'><tfoot id='FSxvV'></tfoot><dl id='FSxvV'><fieldset id='FSxvV'></fieldset></dl></div>

        <legend id='FSxvV'><style id='FSxvV'><dir id='FSxvV'><q id='FSxvV'></q></dir></style></legend>

      1. 使用 Lucene 的精确短语搜索?

        时间:2023-06-28

              <bdo id='iEbKX'></bdo><ul id='iEbKX'></ul>
              • <small id='iEbKX'></small><noframes id='iEbKX'>

                <i id='iEbKX'><tr id='iEbKX'><dt id='iEbKX'><q id='iEbKX'><span id='iEbKX'><b id='iEbKX'><form id='iEbKX'><ins id='iEbKX'></ins><ul id='iEbKX'></ul><sub id='iEbKX'></sub></form><legend id='iEbKX'></legend><bdo id='iEbKX'><pre id='iEbKX'><center id='iEbKX'></center></pre></bdo></b><th id='iEbKX'></th></span></q></dt></tr></i><div id='iEbKX'><tfoot id='iEbKX'></tfoot><dl id='iEbKX'><fieldset id='iEbKX'></fieldset></dl></div>
                <legend id='iEbKX'><style id='iEbKX'><dir id='iEbKX'><q id='iEbKX'></q></dir></style></legend>
                    <tbody id='iEbKX'></tbody>

                1. <tfoot id='iEbKX'></tfoot>

                  本文介绍了使用 Lucene 的精确短语搜索?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

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

                  I am using SpanTerm Query for searching exact phrase in lucene. But it doesnt seem to work. Here is my code.

                  索引

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

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

                  Please guide me if I am doing it wrong???

                  普拉提克

                  推荐答案

                  试试 PhraseQuery 改为:

                  Try a PhraseQuery instead:

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

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

                  I think you have a different problem. What other parts are there to your booleanQuery? Here's a full working example of searching for a phrase:

                  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;
                      }
                  }
                  

                  这篇关于使用 Lucene 的精确短语搜索?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:Java - 如何更改“本地"?事件侦听器中的变量 下一篇:使用 Lucene 提取英语单词

                  相关文章

                2. <i id='F7vcE'><tr id='F7vcE'><dt id='F7vcE'><q id='F7vcE'><span id='F7vcE'><b id='F7vcE'><form id='F7vcE'><ins id='F7vcE'></ins><ul id='F7vcE'></ul><sub id='F7vcE'></sub></form><legend id='F7vcE'></legend><bdo id='F7vcE'><pre id='F7vcE'><center id='F7vcE'></center></pre></bdo></b><th id='F7vcE'></th></span></q></dt></tr></i><div id='F7vcE'><tfoot id='F7vcE'></tfoot><dl id='F7vcE'><fieldset id='F7vcE'></fieldset></dl></div>
                  <legend id='F7vcE'><style id='F7vcE'><dir id='F7vcE'><q id='F7vcE'></q></dir></style></legend>
                    <tfoot id='F7vcE'></tfoot>

                      <small id='F7vcE'></small><noframes id='F7vcE'>

                      • <bdo id='F7vcE'></bdo><ul id='F7vcE'></ul>