2013-09-25 61 views
0

環境:Lucene.Net搜索像A071,A072,A073

Lucene.Net 3.03

Visual Studio 2010中


我一直停留在這個問題上幾個小時,在這一點上我無法弄清楚問題所在。

我建一些指數命名爲 「專賣店」,像下面的格式,

A075,A073,A021 ....

每個字符串表示店鋪的ID,並通過 「」 分隔,

我想搜索「A073」,它將返回匹配的數據,如果「商店」包括提前「A073」

感謝

static RAMDirectory dir = new RAMDirectory(); 


public void BuildIndex() 
{ 

    IndexWriter iw = new IndexWriter(dir, new StandardAnalyzer(Version.LUCENE_30), true, IndexWriter.MaxFieldLength.UNLIMITED); 

    Document doc = new Document(); 
    doc.Add(new Field("PROD_ID", "", Field.Store.YES, Field.Index.ANALYZED, Field.TermVector.NO)); 
    doc.Add(new Field("Stores", "", Field.Store.YES, Field.Index.ANALYZED, Field.TermVector.NO)); 

    for (int i = 1; i <= 10; i++) 
    { 
     doc.GetField("PROD_ID").SetValue(Guid.NewGuid().ToString()); 
     doc.GetField("Stores").SetValue("a075,a073,no2" + i.ToString()); 
     iw.AddDocument(doc); 
    } 

    iw.Optimize(); 
    iw.Commit(); 
    iw.Close(); 
} 

private void Search(string KeyWord) 
{ 

    IndexSearcher search = new IndexSearcher(dir, true); 

    QueryParser qp = new QueryParser(Version.LUCENE_30, "Stores", new StandardAnalyzer(Version.LUCENE_30)); 

    Query query = qp.Parse(KeyWord); 

    var hits = search.Search(query, null, search.MaxDoc).ScoreDocs; 


    foreach (var res in hits) 
    { 
     Response.Write(string.Format("PROD_ID:{0}/Stores{1}" 
            , search.Doc(res.Doc).Get("PROD_ID").ToString() 
            , search.Doc(res.Doc).Get("Stores").ToString() + "<BR>")); 
    } 
} 

回答

0

嘗試使用Lucene.Net.Search.WildcardQuery幷包含通配符。 谷歌的Lucene正則表達式搜索找到一些代碼在您的查詢中使用正則表達式...有一個名爲Contrib.Regex.RegexTermEnum的contrib實現。

另一種方法是多值字段,而不是逗號分隔的字符串,您可以將數組傳遞給它。這將被Lucene分割和建立索引,你可以用與正常字段相同的方式查詢它。另外,你可以多次查詢它,例如multiField:ValueA和mutliField:ValueB ...

+0

嗨,Ela,我知道我可以使用通配符,如「*」,但有沒有其他方法來解決它〜謝謝 – holmes2136

+0

@Holmes看到上次編輯,使用多值字段應該爲你做 – MichaC

+0

嗨,Ela,你的意思是使用MultifieldQueryFieldParser?但它似乎像在多領域的搜索。你能給我一些樣本〜thx! – holmes2136