弹性搜索。 Net With Nest:Elastic 属性 的术语过滤器

Elastic Search. Net With Nest: Term Filter For Elastic Property

我正在尝试使用过滤器执行查询。我可以让它过滤某些属性,但不是我需要的属性。这是我的模型:

    public class IndexItem
    {
         public DateTime CreatedDate { get; set; }

         [ElasticProperty(Index = FieldIndexOption.Analyzed)]
         public String Name { get; set; }

         [ElasticProperty(Index = FieldIndexOption.NotAnalyzed)]
         public String Role { get; set; }

         public bool ExcludeFromSearch { get; set; }
    }

我开始的查询是:

var esQuery = Query<IndexItem>.QueryString(x => x.OnFields(f => f.Name).Query(String.Format("{0}*", query)).Boost(1.2));

如果我对 CreatedDate 或 ExcludeFromSearch 进行过滤,它会像我想象的那样工作,但我无法让它为 Role 工作。

filter.Add(Filter<IndexItem>.Term(x => x.CreatedDate, searchDate)); // Works
filter.Add(Filter<IndexItem>.Term(x => x.Role, role)); // Never Returns a result

        var searchResults = client.Search<IndexItem>(s => s
                .Types(typeof(IndexItem))
                .From(start)
                .Size(count)
                .Query(esQuery)
                .Filter(x => x.And(filter.ToArray()))
         ); // Returns empty if I filter by Role, but works if i filter by CreatedDate

我能看到的唯一区别是 Role 具有注释 [ElasticProperty(Index = FieldIndexOption.NotAnalyzed)]。这是否使其不允许被过滤?

这是我在浏览器中输入的查询的示例输出:

{"took":47,"timed_out":false,"_shards":{"total":10,"successful":10,"failed":0},"hits":{"total":1,"max_score":5.9272537,"hits":[{"_index":"default-index","_type":"indexitem","_id":"3639","_score":5.9272537,"_source":{
  "properties": {
    "MainBody": "Test Role Search"
  },
  "id": "3639",
  "createdDate": "2015-05-08T14:34:33",
  "name": "Role Test",
  "url": "/my-role-test/",
  "role": "Admin",
  "excludeFromSearch": false
}}]}}

角色字段上的[ElasticProperty(Index = FieldIndexOption.NotAnalyzed)]属性定义了一个映射属性,意味着该字段的内容在被索引之前不会经过分析过程。有关分析过程的文档,请参阅 here for the official documentation on mappings and here。相反,Name 字段的内容将在被索引之前从分析过程中通过。

您正在使用的术语过滤器会过滤包含提供的该术语的字段的文档,而不会将术语从分析过程中传递出去(请参阅 here)。

示例:如果您使用的是标准分析器,并且您希望使用 Name="Data" 索引一个 IndexItem,那么分析器将转换 "Data" 到 "data" 并将该术语插入倒排索引中。使用相同的分析器并希望使用 Role="Data" 索引 IndexItem 然后 "Data" 项将保存在倒排索引中,因为 Role 字段的内容被排除在分析过程之外。

因此,如果要进行术语筛选以匹配 Role 字段上的先前文档,则筛选依据的值是 "Data"(与索引文档中的值完全相同)。如果要进行术语筛选以匹配名称字段中的先前文档,则筛选依据的值为 "data"。请记住,在索引和查询数据时使用相同的分析器是一种很好的做法。