Search value in body tag in Sitecore

佐手、 提交于 2019-12-11 03:27:37

问题


I want to implement search functionality, So my requirement is I want to search some keyword in body tag in all content page. I do not know how I can search keyword in body tag in Sitecore. Please guide me?


回答1:


As Anton outlined, the concept of searching the Body tag is wrong for Sitecore. You want to think in terms of Content in fields of Items. Sitecore's ContentSearch is how you can achieve this.

Sitecore comes with default indexes out-of-the-box that you should use for the search. You should rebuild these via the Index Manager in the Content Editor and then base your search on the basic example I've outlined for you below.

    public IEnumerable<Item> Search(string searchterm)
    {
        string indexName = "sitecore_web_index";

        using (var index = ContentSearchManager.GetIndex(indexName).CreateSearchContext())
        {
            var predicate = PredicateBuilder.True<SearchResultItem>();

            IQueryable<SearchResultItem> query = index.GetQueryable<SearchResultItem>().Where(i => i.Content.Contains(searchterm)).Filter(predicate);

            var searchResults = query.GetResults();

            foreach (var hit in searchResults.Hits)
            {
                yield return hit.Document.GetItem();
            }
        }
    }



回答2:


jRobbins's answer is sensible (he get's my upvote). However, it is technically possible to index the content of the body tag. I would be cautious with this. I've seen it working well, but I've also seen it completely destroy the performance of a site.

The approach involves the creating a computed field in your index. You populate the computed field by making a web request to your newly published page and scraping the response body tag.

Here's are a couple of module that more or less does that:

  • https://github.com/efocus-nl/sitecorewebsearch
  • https://github.com/hermanussen/sitecore-html-crawler

If you can accept something a little less accurate, then you could loop through each of the components on your page and extract content from their datasources. That approach is discussed in this video: http://www.techphoria414.com/Blog/2012/May/Sitecore_Page_Editor_Unleashed



来源:https://stackoverflow.com/questions/31405758/search-value-in-body-tag-in-sitecore

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!