1

We are having the below item structure in sitecore and using Lucene Index,

  • Home

    -- Products A Repository

    • Product 1
    • Product 2
    • Product 3

    -- Products B Repository

    • Product 1
    • Product 2
    • Product 3

Products A & Product B repository items is using Same template called product. We have created new common custom index start location we set as Home.

we are trying to use the below code

var index = ContentSearchManager.GetIndex(indexName);
using (var searchContext = index.CreateSearchContext())
{
    var searchText ="Product";
    var items = searchContext.GetQueryable<SearchResultItem>()
                .Where(x => x.Content.Equals(searchText) && x.Language == query.Language)
                .OrderByDescending(x => x.Updated)
                .GetResults();
}

reference: https://doc.sitecore.net/sitecore_experience_platform/80/developing/developing_with_sitecore/search_and_item_buckets/linq_to_sitecore

but we are not sure how to set the Index Search Start location to search the item from the particular repository.

Update:

Your Solution Works well , i marked as answer

5

In the SearchResultItem class, you can find Paths property, which holds ancestor id's.

[IndexField("_path")]
[DataMember]
[TypeConverter(typeof (IndexFieldEnumerableConverter))]
public virtual IEnumerable<ID> Paths { get; set; }

You can pass start location (eg: in your case Product A Repository item id or Product B Repository item id) to Path,

 var index = ContentSearchManager.GetIndex(indexName);
 using (var searchContext = index.CreateSearchContext())
 {
     var searchText = "Product";
     var items = searchContext.GetQueryable<SearchResultItem>()
                 .Where(x => x.Paths.Contains(new ID("{}")) && x.Content.Equals(searchText) && x.Language == query.Language)
                 .OrderByDescending(x => x.Updated)
                 .GetResults();
 }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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