gpt4 book ai didi

filter - Elasticsearch 。嵌套网络:弹性属性的术语过滤器

转载 作者:行者123 更新时间:2023-12-03 02:02:54 25 4
gpt4 key购买 nike

我正在尝试使用过滤器执行查询。我可以对某些属性进行筛选,但不能筛选所需的属性。这是我的模型:

    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,对于分析过程的文档,请参见here。相反,“名称”字段的内容将在进行索引之前从分析过程中传递。

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

示例:如果您正在使用标准分析器,并且希望使用Name =“Data”为IndexItem编制索引,则分析器会将“Data”转换为“data”并将该术语插入反向索引中。使用相同的分析器,并希望使用Role =“Data”为IndexItem编制索引,则将“Data”术语保存在倒排索引中,因为从分析过程中排除了Role字段的内容。

因此,如果要对术语进行过滤以匹配“角色”字段上的先前文档,则要过滤的值为“数据”(与索引文档中的值完全相同)。如果要对术语进行过滤以匹配“名称”字段上的先前文档,则过滤依据为“数据”。请记住,在索引和查询数据时,最好使用相同的分析器。

关于filter - Elasticsearch 。嵌套网络:弹性属性的术语过滤器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30126246/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com