Exact Phrase search using Lucene?

后端 未结 3 1699
借酒劲吻你
借酒劲吻你 2020-11-30 13:56

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

Indexing

IndexWriter writer = new IndexW         


        
相关标签:
3条回答
  • 2020-11-30 14:25

    Use Lucene Query Builder, and give double quotes around the search string. It works for exact phrase search.

    Reference: http://www.lucenetutorial.com/lucene-query-builder.html

    0 讨论(0)
  • 2020-11-30 14:27

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

    Edit: 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;
        }
    }
    
    0 讨论(0)
  • 2020-11-30 14:31

    For version 4.6.0 Indexing:

    IndexWriterConfig config=new IndexWriterConfig(Version.LUCENE_46,analyzer);
    try {
           IndexWriter iwriter=new IndexWriter(mDir,config);
           iwriter.deleteAll();
           iwriter.commit();
           Document doc = new Document();
    
           doc.add(new Field(myfieldname,text,TextField.TYPE_STORED));
           iwriter.addDocument(doc);
           iwriter.close();
    }
    

    Searching for exact phrase (given in variable keyword):

    DirectoryReader ireader=DirectoryReader.open(mDir);
    IndexSearcher isearcher=new IndexSearcher(ireader);
    QueryParser parser = new QueryParser(Version.LUCENE_46,myfieldname,analyzer);
    parser.setDefaultOperator(QueryParser.Operator.AND);
    parser.setPhraseSlop(0);
    Query query=parser.createPhraseQuery(myfieldname,keyword);
    ScoreDoc[] hits=isearcher.search(query, null, 1000).scoreDocs;
    nret=hits.length;
    ireader.close();
    

    Note for the use of "setPhraseSlop(0) and createPhraseQuery()

    0 讨论(0)
提交回复
热议问题