【问题标题】:Elasticsearch NEST API - querying the right indexElasticsearch NEST API - 查询正确的索引
【发布时间】:2017-04-02 02:26:37
【问题描述】:

在 Elasticsearch 上使用 C# NEST API:

var searchResults = client.Search<Product>(s => s
                .Index(Constants.ElasticSearchIndex)
                .Query(q => q
                    .Raw(jsonRequest)
                )
            );

该查询应该在 /sc_all/ 索引上运行,但它在 /sc_all/product/ 索引上运行(它不存在 - /product/ 似乎是由于自 T = product 的搜索而添加的)。

如果我这样做, /product/ 将替换为常量的值,即 /sc_all/product/ => /sc_all/constant_value/:

var searchResults = client.Search<Product>(s => s
                .Index(Constants.ElasticSearchIndex)
                .Type(Constants.ElasticSearchType)
                .Query(q => q
                    .Raw(jsonRequest)
                )
            );

如果我只想查询 /sc_all/ 而没有别的,该怎么办?

谢谢!


Json 请求:

"{\"filtered\": {\"query\": {\"match_all\": { }},\"filter\": {\"nested\" : {\"path\" : \ "products\",\"filter\": {\"nested\" : {\"path\" : \"products.da\",\"filter\": { \"bool\": { \"必须\": [{\"query\": {\"query_string\": {\"default_field\" : \"products.da.content\", \"query\" : \"kildemoes\"}}}] }}}}}}}},\“来自\”:0,\“大小\”:100“

【问题讨论】:

    标签: c# search elasticsearch nest


    【解决方案1】:

    您只需要使用.AllTypes() 指定跨所有类型运行

    var jsonRequest = "{ \"match_all\": {} }";
    
    var searchResults = client.Search<Product>(s => s
                            .Index(Constants.ElasticSearchIndex)
                            .AllTypes()
                            .Query(q => q
                                .Raw(jsonRequest)
                            )
                        );
    

    这将生成以下请求

    POST http://localhost:9200/sc_all/_search
    {
      "query": { "match_all": {} }
    }
    

    请记住,返回的任何文档都将尝试反序列化为 Product 的实例,因此如果您要针对多种不同的类型,您可能需要使用通用的基本类型或 dynamic,此外,利用covariant search results.

    【讨论】:

    • 谢谢,但我的 Json 请求已经包含“match_all”。我已将其添加到我上面的问题中。
    • @Louisa 我认为您错过了重点-如果您在搜索请求的正文中指定.AllTypes(),那么您将在索引Constants.ElasticSearchIndex 中搜索所有类型。我为jsonRequest 添加了一个示例值,因此您提供的代码将编译。
    猜你喜欢
    • 1970-01-01
    • 2021-08-12
    • 2021-05-02
    • 2015-06-05
    • 1970-01-01
    • 2019-08-04
    • 2019-08-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多