使用Lucene.Net进行Sitecore搜索:在特定字段中搜索

时间:2013-11-04 22:40:15

标签: sitecore lucene.net

我正在使用Lucene.Net和Sitecore.Search.Crawlers.DatabaseCrawler。目前,此搜索适用于所有字段,我想将其更改为仅在少数字段中搜索。 我有自定义抓取工具:

public class CustomCrawler : Sitecore.Search.Crawlers.DatabaseCrawler
{
    protected override void AddAllFields(Lucene.Net.Documents.Document document, Sitecore.Data.Items.Item item, bool versionSpecific)
    {  
        base.AddAllFields(document, item, versionSpecific);
        document.Add(new Field("title", item["TitleField"], Field.Store.YES, Field.Index.TOKENIZED));
        document.Add(new Field("image", imageUrl, Field.Store.YES, Field.Index.TOKENIZED));      
    }

    protected override bool IsMatch(Item item)
    {
        if (!item.TemplateName.Contains("txttmpl")) return false;

        return base.IsMatch(item);
    }
}

我使用标题和图片字段作为搜索结果并将其显示在网页上:

var list = new List<SearchResult>();
foreach (var result in results)
{
    list.Add(new SearchResult()
    {
        Title = result.Document.GetField("title").StringValue(),
        Image = result.Document.GetField("image").StringValue()
    });
}

var jss = new JavaScriptSerializer();
httpContext.Response.ContentType = "application/json";
httpContext.Response.Write(jss.Serialize(list));
httpContext.Response.Flush();

在web.config文件中:

<index id="myindex" type="Sitecore.Search.Index, Sitecore.Kernel">
    <param desc="name">$(id)</param>
    <param desc="folder">Myfolder</param>
    <Analyzer ref="search/analyzer" />
    <locations hint="list:AddCrawler">
        <web type="Search.CustomCrawler, Search">
            <Database>web</Database>
            <Tags>web content</Tags>
            <Root>/sitecore/content/Site</Root>                             
            <Boost>2.0</Boost>
        </web>
    </locations>
</index>

上面的解决方案在所有领域中搜索。如何才能使其仅在搜索中搜索 某些领域?我试过了document.RemoveField(“SomeFieldName”),但它不起作用。如何删除或添加一些字段?提前谢谢。

2 个答案:

答案 0 :(得分:4)

您可以使用以下搜索结构搜索特定字段:

        SearchManager.GetIndex("my_index").Rebuild();

        using (IndexSearchContext indexSearchContext = SearchManager.GetIndex("my_index").CreateSearchContext())
        {
            // Field to be searched followed by search term
            Term term = new Term("location", "Ottawa");
            Query query = new TermQuery(term);

            SearchHits hits = indexSearchContext.Search(query, int.MaxValue);
            // Get Sitecore items from the results of the query
            List<Item> items = hits.FetchResults(0, int.MaxValue).Select(result => result.GetObject<Item>()).Where(item => item != null).ToList();
        }

索引可以设置为索引所有字段,这将继续有效:

     <index id="my_index" type="Sitecore.Search.Index, Sitecore.Kernel">
        <param desc="name">$(id)</param>
        <param desc="folder">dance_map_locations_index</param>
        <Analyzer ref="search/analyzer" />
        <locations hint="list:AddCrawler">
          <core type="Sitecore.Search.Crawlers.DatabaseCrawler,Sitecore.Kernel">
            <Database>web</Database>
            <Root>/sitecore/content/my first item/second item/parent item to be indexed</Root>
            <templates hint="list:IncludeTemplate">
              <template>{AD7E2747-695A-4AC8-A6AB-C7C6111AF9A7}</template>
            </templates>
          </core>
        </locations>
      </index>

答案 1 :(得分:0)

您尝试实现的大部分内容都不需要自定义抓取工具。您可以在<IndexAllFields>false</IndexAllFields>节点中添加<web>,以防止其添加所有字段,然后添加如下部分:

<fields hint="raw:AddCustomField">
    <field luceneName="title" storageType="no" indexType="tokenized">TitleField</field>
    <field luceneName="image" storageType="yes" indexType="untokenized">imageUrl</field>
</fields>

但是,由于您似乎只是尝试添加图像src而不是图像字段的完整XML,因此您可能希望使用高级数据库爬网程序并创建dynamicField。 http://sitecorian.github.io/SitecoreSearchContrib/

或者,如果您可以选择升级到Sitecore 7,则可以创建计算字段。有关动态字段和计算字段的更多详细信息,请参阅this question