RG-3
RG-3

Reputation: 6188

Lucene search not working

I have a function which searches some articles in the Sitecore content items and give me the value. So far I have build up my indexes and it is showing in my IndexViewer. But the return of the function is 0. I looked up this link: http://sitecoregadgets.blogspot.com/2009/11/working-with-lucene-search-index-in_25.html for more information.

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

Over here the result is 0. What I am doing wrond here?

This is the snapshot of what I am seeing in my IndexViewer:

IndexViewer

EDIT:

I am passing a "NZ" in the 'catQuery' and I am getting the result back. Because in my index viewer I am seeing the Field Name = _name, which contains NZ in it. I got this part. However, I want my every field to be indexed. I am seeing only 3 fields in my IndexViewer: _url, _group & _name.

Upvotes: 0

Views: 1254

Answers (1)

nickwesselman
nickwesselman

Reputation: 6890

So your countries should be tokenized by the indexer. As a multilist, they will be tokenized by GUID. Searching for a single country by GUID with your code above should work. However, if you want to search for multiple countries, where any of the passed in countries can trigger a match, you need to structure your query differently.

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

Upvotes: 1

Related Questions