【问题标题】:Search empty fields搜索空白字段
【发布时间】:2014-10-22 15:22:52
【问题描述】:

我正在尝试排除存储项目 ID 的字段为空的搜索结果。例如,此字段称为“类型”。我无法使用 LINQ 做到这一点。这是我的代码示例。

 public class SearchItem : SearchResultItem
 {
    [IndexField("type")]
    public string Type{ get; set; }    
 }

 public class Search
 {
    public static IEnumberable<Item> GetItems()
    {
       List<Item> items = new List<Item>();
        var index = ContentSearchManager.GetIndex(new SitecoreIndexableItem(Sitecore.Context.Item));
        using (var context = index.CreateSearchContext())
        {
            var indexItems = context.GetQueryable<SearchResultItem>()
                .Where(x => !string.IsNullOrEmpty(x.Type))
                .OrderByDescending(x => x.ReleaseDate);

            foreach(var indexItem in indexItems)
            {
                var tempItem = indexItem.GetItem();
                items.Add(tempItem);
            }
        }
        return items;
    }
 }

空字符串比较不起作用,并且项目集合包含类型字段具有空字符串的项目。我正在使用 Lucene 的开箱即用设置。

另外,如果您发现有问题,请在我的代码中戳洞。这是我第一次使用 Sitecore 7 Search。

【问题讨论】:

  • 我对此不是 100% 的,但我认为在 Lucene 中不可能像使用 SQL 那样进行否定匹配。通过使用 IsNullOrEmpty,您是说您需要识别索引中的空值/空值,但这些值一开始就不会添加到索引中,因此没有什么可以匹配的。底线,我不认为你可以检查缺席

标签: c# lucene sitecore sitecore7 sitecore7.2


【解决方案1】:

不确定 Sitecore Linq 是否支持 string.IsnullOrEmpty,请尝试 var indexItems = context.GetQueryable() .Where(x => x.Type != null) .OrderByDescending(x => x.ReleaseDate);

【讨论】:

  • 如果我不正确,请您评论一下,简单地降低答案对阅读本文的人没有好处。
  • 不确定是谁降级了它,但你的答案是正确的。这就是我最终使用我的代码的方式。我赞成它以消除负面影响:)
【解决方案2】:

Sitecore 和 Lucene 不支持空字符串,因为空字段不在索引中。索引中没有空项目的文档。这篇文章可能会帮助您使用 Range 查询。

Sitecore + Lucene Search FieldQuery with and empty string

【讨论】:

    【解决方案3】:

    请检查任何索引查看器工具,如 Luke 并确认索引字段类型是否已创建,如果已创建,则它是否存储预期值。 尝试通过调用函数进行检查,以便调试查询。

        protected bool checkType(SearchResultItem Ritem)
        {
            if (Ritem.type != null ||  !string.IsNullOrEmpty(Ritem.type))
            {
               return true;
            }
            return false;
        }
    

    【讨论】:

      【解决方案4】:

      你可以尝试将 where 条件更改为

          public class SearchItem : SearchResultItem
       {
          [IndexField("type")]
          public string Type{ get; set; }    
       }
      
       public class Search
       {
          public static IEnumberable<Item> GetItems()
          {
             List<Item> items = new List<Item>();
              var index = ContentSearchManager.GetIndex(new SitecoreIndexableItem(Sitecore.Context.Item));
              using (var context = index.CreateSearchContext())
              {
                  var indexItems = context.GetQueryable<FRBSearchResultItem>()
                      .Where(x => !string.IsNullOrEmpty(x["type"]))
                      .OrderByDescending(x => x.ReleaseDate);
      
                  foreach(var indexItem in indexItems)
                  {
                      var tempItem = indexItem.GetItem();
                      items.Add(tempItem);
                  }
              }
              return items;
          }
       }
      

      【讨论】:

        【解决方案5】:

        答案可能为时已晚,但可能会帮助未来的读者解决同样的问题。

        我正在使用如下谓词生成器,

        faqPredicate = faqPredicate.And(x => x.FAQAudience != null);
        

        这会导致以下错误

        消息:不支持空值的比较。 来源:Sitecore.ContentSearch.Linq.Lucene

        所以要在索引而不是返回字符串时修复它。我使用了返回“null”;

        在我检查的谓词中,

        faqPredicate = faqPredicate.And(x => x.FAQAudience != "null");
        

        是的,这是一种解决方法,但它确实有效。我也尝试与 string.Empty 进行比较,但没有奏效

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-03-10
          • 2013-02-02
          • 2018-12-28
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多