lucene通配符查询:WildcardQuery

 

代码
using System;   
using System.Collections.Generic;   
using System.Text;   
using Lucene.Net.Index;   
using Lucene.Net.Store;   
using Lucene.Net.Documents;   
using Lucene.Net.Analysis.Standard;   
using Lucene.Net.Search;   
   
   
public class WildcardQueryTest   
{   
    RAMDirectory directory 
= new RAMDirectory();   
   
    
public Document getDoc(String type, String content)   
    {   
        Document doc 
= new Document();   
        doc.Add(
new Field(type, content, Field.Store.YES, Field.Index.TOKENIZED));   
        
return doc;   
    }   
   
    
public void makeIndex()   
    {   
   
            IndexWriter indexWriter 
= new IndexWriter(directory, new StandardAnalyzer(), true);   
            indexWriter.AddDocument(getDoc(
"content""word"));   
            indexWriter.AddDocument(getDoc(
"content""work"));   
            indexWriter.AddDocument(getDoc(
"content""workman"));   
            indexWriter.AddDocument(getDoc(
"content""woe"));   
            indexWriter.AddDocument(getDoc(
"content""wow"));   
            indexWriter.AddDocument(getDoc(
"content""wolf"));   
            indexWriter.AddDocument(getDoc(
"content""woman"));   
            indexWriter.AddDocument(getDoc(
"content""world"));   
            indexWriter.AddDocument(getDoc(
"content""sword"));   
            indexWriter.AddDocument(getDoc(
"content""ford"));   
            indexWriter.AddDocument(getDoc(
"content""good work"));   
            indexWriter.Close();   
    }   
   
    
/**  
     * 通配符查询 ?表示单一字符 *表示0-n个字符  
     
*/   
    
public void search(String key){   
        
try {   
            IndexSearcher indexSearcher 
= new IndexSearcher(directory);   
            WildcardQuery query 
= new WildcardQuery(new Term("content", key));   
            Hits hits 
= indexSearcher.Search(query);   
            
for (int i = 0; i < hits.Length(); i++) {   
                Console.WriteLine(
"docid:" + hits.Id(i) + "    content:"   
                        
+ hits.Doc(i).Get("content"+ "    score:" + hits.Score(i));   
            }   
            directory.Close();   
        }
catch (Exception e) {   
               
        }   
    }   
   
    
static void Main(string[] args)   
    {   
        WildcardQueryTest query 
= new WildcardQueryTest();   
        query.makeIndex();   
        query.search(
"?o*");   
        Console.ReadLine();   
    }   
   
}   

 

 

posted @ 2010-07-06 17:21  冰封的心  阅读(889)  评论(0编辑  收藏  举报