Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
531 views
in Technique[技术] by (71.8m points)

java - Exact Phrase search using Lucene?

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

Searching

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???

Prateek

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share

2.1m questions

2.1m answers

62 comments

56.7k users

...