gpt4 book ai didi

c# - 使用 NEST 进行 Elasticsearch 查询不起作用

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

我将 Microsoft SQL Server Management Studio 和 ElasticSearch 2.3.4 与 ElasticSearch-jdbc-2.3.4.1 一起使用,并将 ES 与我的 mssql 服务器链接起来。一切正常,但是当我在 MVC 程序上使用 NEST 进行查询时,结果为空。当我在我的 search 中放入一个空字符串时属性我得到了元素,但是当我尝试用一​​些过滤器填充它时,我得到一个空的结果。有人可以帮帮我吗?提前致谢。

C#:

const string ESServer = "http://localhost:9200";
ConnectionSettings settings = new ConnectionSettings(new Uri(ESServer));
settings.DefaultIndex("tiky");
settings.MapDefaultTypeNames(map => map.Add(typeof(DAL.Faq), "faq"));
ElasticClient client = new ElasticClient(settings);

var response = client.Search<DAL.Faq>(s => s.Query(q => q.Term(x => x.Question, search)));

var result = response.Documents.ToList();

达尔: DAL

postman : PostMan

PS:我关注了 this guide创建它

编辑:

索引映射: Index Mapping

最佳答案

我可以看到有几件事在这里可能会有所帮助:

  • 默认情况下,NEST Camel 在将 POCO 属性名称序列化为请求中的查询 JSON 的一部分时会区分这些名称,因此 x => x.Question将序列化为 "question" .但是,查看您的映射,Elasticsearch 中的字段名称是 Pascal 大小写的,因此客户端所做的与 Elasticsearch 中的不匹配。

  • 您可以使用 .DefaultFieldNameInferrer(Func<string, string>) 更改 NEST 序列化 POCO 属性名称的方式。在 ConnectionSettings
    const string ESServer = "http://localhost:9200";
    ConnectionSettings settings = new ConnectionSettings(new Uri(ESServer))
    .DefaultIndex("tiky");
    .MapDefaultTypeNames(map => map.Add(typeof(DAL.Faq), "faq"))
    // pass POCO property names through verbatim
    .DefaultFieldNameInferrer(s => s);

    ElasticClient client = new ElasticClient(settings);
  • 正如 Rob 在评论中提到的,a term query不分析查询输入。当针对在索引时分析的字段执行术语查询时,为了获得匹配,您传递给术语查询的查询文本需要考虑在索引时应用的​​分析。例如,
  • Question使用 the Standard Analyzer 进行分析
  • 一个 Question "What's the Question?" 的值将被分析和索引为标记 "what's" , "the""question"
  • 术语查询需要有 "what's" 的查询输入。 , "the""question"成为一个匹配

  • 与术语查询不同,匹配查询会分析查询输入,因此搜索分析的输出将用于查找匹配项。结合 1. 中突出显示的 Pascal 大小写,您现在应该可以返回文档。

    您还可以在 Elasticsearch 中两全其美,即在索引时分析输入以获得全文搜索功能,以及在不分析的情况下索引输入以获得精确匹配。这是通过 multi-fields 完成的。这是创建索引 Question 的映射的示例已分析和未分析的属性
    public class Faq
    {
    public string Question { get; set; }
    }

    var pool = new SingleNodeConnectionPool(new Uri("http://localhost:9200"));
    var defaultIndex = "default-index";
    var connectionSettings = new ConnectionSettings(pool)
    .DefaultIndex(defaultIndex)
    .DefaultFieldNameInferrer(s => s);

    var client = new ElasticClient(connectionSettings);

    if (client.IndexExists(defaultIndex).Exists)
    client.DeleteIndex(defaultIndex);

    client.CreateIndex(defaultIndex, c => c
    .Mappings(m => m
    .Map<Faq>(mm => mm
    // let NEST infer mapping from the POCO
    .AutoMap()
    // override any inferred mappings explicitly
    .Properties(p => p
    .String(s => s
    .Name(n => n.Question)
    .Fields(f => f
    .String(ss => ss
    .Name("raw")
    .NotAnalyzed()
    )
    )
    )
    )
    )
    )
    );

    这个映射看起来像
    {
    "mappings": {
    "faq": {
    "properties": {
    "Question": {
    "type": "string",
    "fields": {
    "raw": {
    "type": "string",
    "index": "not_analyzed"
    }
    }
    }
    }
    }
    }
    }
    "raw" "Question" 下的子字段字段将索引 Question 的值没有任何分析的属性(property),即逐字记录。现在可以在术语查询中使用此子字段来查找完全匹配
    client.Search<Faq>(s => s
    .Query(q => q
    .Term(f => f.Question.Suffix("raw"), "What's the Question?")
    )
    );

    查找前面示例的匹配项。

    关于c# - 使用 NEST 进行 Elasticsearch 查询不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42551662/

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