4

I have to implement Azure search but would like to try with Lucene first in the local setup. I am following this example by @kamsar

1.Created the SubcontentField class
2.Created the patch config and
3.Rebuilt the index for sitecore_web_index

[ Did not add any custom field/template IDs in the Lucene configs. Using the default ones as it is without any changes. ]

Here is my code which is triggered, when a user clicks the search button on the page.

string keywords = <keywords entered by user>;    

 using (var context = ContentSearchManager.GetIndex("sitecore_web_index").CreateSearchContext())
 {
   var query = context.GetQueryable<SearchResultItem>()  
              .Where(x => x.Content.Contains(keywords) && 
                     x.Language == Sitecore.Context.Language.Name);
                    
   List<SearchResultItem> lst = query.ToList();       
 }

Now I search using a keyword "stories". There is a rendering on the home page, which has the title "Stories". I am expecting the Home page item in one of the result items inside lst.

But there are other items like controller rendering, media library, etc that have the name "stories".

I would like to get the list of page items that have the keyword used in any of the renderings for that page.

As suggested in the article, should I search in any particular field? Could someone please guide me on how to get my desired results here?

Using sc8.2 u3 with MVC

UPDATE:

I did this to see if there really was home page in the returned list

var query = context.GetQueryable<SearchResultItem>()
                       .Where(x => x.ItemId == SitecoreUtility.GetHomeItem().ID &&   
   x.Language == Sitecore.Context.Language.Name);

List<SearchResultItem> lst = query.ToList();
string sss = lst[0].Content;

It did return one result, but "sss" got only one word which was "Home". So I'm doubting if the example from the article really worked for me, I mean did it really get all content fields values into one field.

1 Answer 1

4

You use the "sitecore_web_index" which contains all items of web database. If you want to get just pages, you need to filter results. For example:

string keywords = <keywords entered by user>;    
 using (var context = ContentSearchManager.GetIndex("sitecore_web_index").CreateSearchContext())
 {
   var query = context.GetQueryable<SearchResultItem>()  
              .Where(x => x.Paths.Contains(ID.Parse("{0DE95AE4-41AB-4D01-9EB0-67441B7C2450}")) x.Content.Contains(keywords) && 
                     x.Language == Sitecore.Context.Language.Name)

   List<SearchResultItem> lst = query.ToList();       
 }

,where {0DE95AE4-41AB-4D01-9EB0-67441B7C2450} is id of your home page. It means that search result will comtain all items under the item with id = {0DE95AE4-41AB-4D01-9EB0-67441B7C2450} including this item itself and whose corresponds another conditions.

8
  • 1
    This overlooks a central part of the question; "I would like to get the list of page items that have the keyword used in any of the renderings for that page." Oct 10, 2017 at 10:04
  • 1
    He wrote that he implemented the SubcontentField as it described by the link kamsar.net/index.php/2014/05/indexing-subcontent which extend the _content field and content which placed in datasources of renderings are associated with indexed item. Oct 10, 2017 at 10:10
  • @ArtsemPrashkovich I tried your suggestion but it returns no result. The home page has the keyword "stories" for sure in a rendering datasource
    – sukesh
    Oct 10, 2017 at 12:55
  • Have you put the correct ID of your home page item? Oct 10, 2017 at 12:57
  • @ArtsemPrashkovich Yes I did
    – sukesh
    Oct 10, 2017 at 13:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.