【问题标题】:Sitecore search with Lucene.Net: search in specific fields使用 Lucene.Net 进行 Sitecore 搜索:在特定字段中搜索
【发布时间】:2013-11-04 22:40:15
【问题描述】:

我正在使用 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"),但它不起作用。如何删除或添加一些字段?提前致谢。

【问题讨论】:

    标签: sitecore lucene.net


    【解决方案1】:

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

            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>
    

    【讨论】:

      【解决方案2】:

      对于您想要实现的大部分目标,您不需要自定义爬虫。您可以在 &lt;web&gt; 节点中添加 &lt;IndexAllFields&gt;false&lt;/IndexAllFields&gt; 以防止其添加所有字段,然后添加如下部分:

      <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,因此您可能希望使用高级数据库爬虫并创建一个动态字段。 http://sitecorian.github.io/SitecoreSearchContrib/

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-15
        • 2016-12-18
        • 2021-01-24
        • 1970-01-01
        相关资源
        最近更新 更多