【问题标题】:Using Nest Analyzer "keyword" on text Field without access to ElasticSearch DB在文本字段上使用 Nest Analyzer“关键字”而不访问 ElasticSearch DB
【发布时间】:2020-12-08 10:48:04
【问题描述】:

我是 elasticSearch 和嵌套的新手。在一个 c# 项目中,我使用 Nest 将数据查询到 elasticSearch 数据库。我无法控制数据库(我无法在此处添加多字段或指定分析器)。 我有一个文本字段,它的值可以是“Some-Thing_A”或“Some-Thing_B”或“Other”等,但总是类似关键字的值。我想查询它的确切字符串(由用户给出)“Some-Thing_A”。 我正在使用以下查询:

var searchResponse = client.Search<LogData>(s =>
                s.Index("indexxx")
                .Size(50)
                .Query(q =>
                    q.Match(m => m.Field(f => f.Branche).Query("Some-Thing_A")))));

可能返回“Some-Thing_A”或“Some-Thing_B”...

我尝试过使用“术语”,但它不起作用。 我尝试使用这样的分析器:

client.Indices.Create("indexxx", c => c.Map<LogData>(m => m.AutoMap()));

var searchResponse = client.Search<LogData>(s =>
                s.Index("indexxx")
                .Size(50)
                .Query(q => q
                   .Match(m => m.Field(f => f.Branche).Analyzer("keyword").Query("Some-Thing_A"))
                );

在我的 LogData 类中:

 [Text(Name = "Branche", Analyzer = "keyword", SearchAnalyzer ="keyword", Index = true)]
 public string Branche { get; set; }

但它总是返回 0 命中。我错过了什么 ?有没有办法使用关键字分析器而无需访问数据库来更改它?

【问题讨论】:

    标签: c# elasticsearch nest


    【解决方案1】:

    在尝试了更多解决方案后,我设法使用了“术语”。我不知道弹性动态映射是用关键字和文本类型映射文本字段。

    var searchResponse = client.Search<LogData>(s =>
                     s.Index("indexxx")
                     .Size(5000)
                     .Query(q => q.Term(term => term
                            .Field(field => field.Branche.Suffix("keyword"))
                            .Value("Some-Thing_A")))
                     .Sort(srt => srt
                        .Ascending(p => p.Timestamp))
                     );
    

    【讨论】:

    • 根据您问题中的详细信息和此答案,听起来文档已被索引到目标索引之前您的创建索引请求创建具有指定显式映射的索引通过 LogData POCO 上的属性,这导致 Elasticsearch 推断出具有 "keyword" 多字段的 "text" 数据类型的 JSON "Branche" 字段的默认映射。通常,如果需要以指定方式搜索数据,您可能需要能够创建索引、映射和配置自定义分析器。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多