开发者

Lucene full-text search

开发者 https://www.devze.com 2023-03-22 08:34 出处:网络
I am using Lucene 3.3.0 with java. I am facing the following problem and don know if there exits a solution for it.

I am using Lucene 3.3.0 with java. I am facing the following problem and don know if there exits a solution for it.

I index the following text: "The boy played so hard to win the game" using StandardAnalyzer, and then i make a query search using "play" ... Lucene find hits ONLY when i use WildcardQuery builder.

The problem is that when i try searching "boy game" it finds NO hits at all.

Is there away to make Lucene make something like context searching to solve this problem ?

Thanks, Samer

private static void addDoc(IndexWriter w, String value) throws IOException {
    Document doc = new Document();
    doc.add(new Field("title", value, F开发者_如何学运维ield.Store.YES, Field.Index.ANALYZED));
    w.addDocument(doc);
}

@SuppressWarnings("deprecation")
public static void lucene(String args, String query) throws IOException, ParseException {
    // 0. Specify the analyzer for tokenizing text.
    // The same analyzer should be used for indexing and searching
    StandardAnalyzer analyzer = new StandardAnalyzer(Version.LUCENE_CURRENT);

    // 1. create the index
    Directory index = new RAMDirectory();

    // the boolean arg in the IndexWriter ctor means to
    // create a new index, overwriting any existing index
    IndexWriter w = new IndexWriter(index, analyzer, true,
            IndexWriter.MaxFieldLength.UNLIMITED);
    String[] splitOnLinefeed = args.split("\n");
    for (int i = 0; i < splitOnLinefeed.length; i++) {
        addDoc(w, splitOnLinefeed[i]);
    }
    w.close();

    // 2. query
    String querystr = query+"*";

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

    // 3. search
    IndexSearcher searcher = new IndexSearcher(index, true);
    ScoreDoc[] hits = searcher.search(q, 100).scoreDocs;

    // 4. display results
    System.out.println("Found " + hits.length + " hit(s).");
    for (int i = 0; i < hits.length; ++i) {
        int docId = hits[i].doc;
        Document d = searcher.doc(docId);
        System.out.println((i + 1) + ". " + d.get("title"));
    }

    // searcher can only be closed when there
    // is no need to access the documents any more.
    searcher.close();
}

public static void main(String[] args) throws Exception {
    lucene(parse("Test.pdf"), "boy game");
}


1) Query "play" : StandardAnalyzer does not provide stemming. It is obvious that you either have to use wildcard or provide exactly the same term. So, with no Stemming, "play" and "playing" are totally different from each other.

If you want "title:play" to work, you may create your own Analyzer by combining components (tokenizer, filters) of StandardAnalyzer and PorterStemFilter

2) "boy game": Did you make sure whether your pdf is being parsed properly. Please try printing "args" argument to the lucene();

0

精彩评论

暂无评论...
验证码 换一张
取 消