2012-09-13 143 views
0

我有一个功能,它搜索Sitecore内容项目中的一些文章,并给我的价值。到目前为止,我已经建立了我的索引,并在我的IndexViewer中显示。但是函数的返回值为0.我查找了这个链接:http://sitecoregadgets.blogspot.com/2009/11/working-with-lucene-search-index-in_25.html了解更多信息。Lucene搜索不起作用

protected IEnumerable<Item> ShowHomePageNews(int numOfArticles, string stringofCountries) 
    { 
     List<Item> items = new List<Item>(); 
     Sitecore.Search.Index indx = SearchManager.GetIndex("newsArticles"); 
     using (IndexSearchContext searchContext = indx.CreateSearchContext()) 
     { 
      var db = Sitecore.Context.Database; 
      CombinedQuery query = new CombinedQuery(); 
      QueryBase catQuery = new FieldQuery("countries", stringofCountries); //FieldName, FieldValue. 
      SearchHits results = searchContext.Search(catQuery); //Searching the content items by fields. 
      SearchResultCollection result = results.FetchResults(0, numOfArticles); 
      foreach (SearchResult i in result) 
      { 
       items = result 
           .Where(r => !r.Title.StartsWith("*")) 
           .Select(r => db.GetItem(new Sitecore.Data.ItemUri(r.Url).ToDataUri())) 
           .ToList(); 
       //Lucene.Net.Documents.Field url = i.Document.GetField("_url"); 
       //Sitecore.Data.ItemUri itemUri = new Sitecore.Data.ItemUri(url.StringValue()); 
       //Sitecore.Data.Items.Item item = Sitecore.Context.Database.GetItem(itemUri.ToDataUri()); 
       //items.Add(item); 
      } 
     } 
     return items; 
    } 

在这里的结果是0.我在这里做什么wrond?

这是快照我所看到的在我的IndexViewer:

IndexViewer

编辑:

我传递一个 “NZ” 在 'catQuery' 和我得到结果回来了。因为在我的索引查看器中,我看到了Field Name = _name,其中包含NZ。我得到了这部分。但是,我希望我的每个领域都能被索引。我在IndexViewer中只看到3个字段:_url,_group & _name。

+1

您可以在“国家”字段中指定值的格式/内容以及stringofCountries参数的格式/内容吗? – techphoria414

+0

整个方法是否返回0结果或'results.FetchResults(...)'? –

+0

您还可以在索引查看器中添加几行数据示例吗? –

回答

1

所以你的国家应该被索引器标记。作为一个多重列表,它们将被GUID标记。用上面的代码通过GUID搜索一个国家应该可以工作。但是,如果您想搜索多个国家/地区,其中任何国家/地区都可以触发匹配,则需要以不同方式构建查询。

CombinedQuery query = new CombinedQuery(); 

//apply other filters here to query if need be 

//and country filter by creating a new clause (combinedquery) and "ORing" within it (QueryOccurance.Should) 
CombinedQuery query3 = new CombinedQuery(); 
//here you would actually iterate over your country list 
query3.Add(new FieldQuery("countries", country1GUID), QueryOccurance.Should); 
query3.Add(new FieldQuery("countries", country2GUID), QueryOccurance.Should); 
query.Add(query3, QueryOccurance.Must);