弹性搜索不使用Nest返回文档

Eld*_*dho 3 c# elasticsearch nest

我很新ElasticSearch.

我无法使用ElasticSearch我试过的几种方式进行搜索,似乎没有什么对我有用.

如果我使用感知铬工具包Sense扩展为chrome和查询GET /employee/_search?q=FirstName="Eldho"它它工作正常.

我已经看了这个答案,也不适合我.

无法使用Nest进行搜索

    protected ElasticClient Client;
    IndexName index = "employee";

    public ElasticSearchRepository(Uri elasticServerUri)
    {
        var connection = new ConnectionSettings(elasticServerUri).DefaultIndex("employee");
        this.Client = new ElasticClient(connection);

    }

    //This is how i create Index
    public void CreateIndex()
    {

        var settings = new IndexSettings();
        settings.NumberOfReplicas = 1;
        settings.NumberOfShards = 1;

        var indexstate = new IndexState();
        indexstate.Settings = settings;


        Client.CreateIndex(index, g => g.Index(index)
              .InitializeUsing(indexstate)
              .Mappings(j => j.Map<Employee>(h => h.AutoMap(1))));

    }

    public List<Employee> Search(string search)
    {
        //All 3 searches are not wokring for me
        var response = Client.Search<Employee>(s => s
                             .AllIndices()
                             .AllTypes()
                             .From(0)
                             .Size(10)
                             .Query(q =>q
                             .Term(t => t.FirstName, "Eldho")));

        var result = Client.Search<Employee>(h => h
                            .Query(q => q
                                .Match(m => m.Field("FirstName").Query(search))));

        var result2 = Client.Search<Employee>(h => h
                     .Type("employee")
                     .Query(k => k
                     .Term(g => g.FirstName, "Eldho")));

        return result.Documents.ToList();
    }
Run Code Online (Sandbox Code Playgroud)

请让我知道我做错了什么.

Rus*_*Cam 5

我在代码中的任何地方都看不到你索引任何文档,但我猜你已经索引了它们.

第一次搜索

    var response = Client.Search<Employee>(s => s
                         .AllIndices()
                         .AllTypes()
                         .From(0)
                         .Size(10)
                         .Query(q =>q
                         .Term(t => t.FirstName, "Eldho")));
Run Code Online (Sandbox Code Playgroud)

将不匹配,因为您使用的是term与查询"Eldho",但将使用进行现场映射.AutoMap()将默认使用标准分析仪string现场这将,除其他事项外,小写令牌.如果您要使用更改为term查询"eldho",我希望找到匹配项.

你的第二次搜索

   var result = Client.Search<Employee>(h => h
                        .Query(q => q
                            .Match(m => m.Field("FirstName").Query(search))));
Run Code Online (Sandbox Code Playgroud)

将不匹配,因为默认情况下,NEST在索引,映射,搜索等时使用了属性名称.因此FirstName,在POCO上命名的属性将映射到firstName该文档类型的映射中指定的字段.

你的第三次搜索

    var result2 = Client.Search<Employee>(h => h
                 .Type("employee")
                 .Query(k => k
                 .Term(g => g.FirstName, "Eldho")));
Run Code Online (Sandbox Code Playgroud)

遇到与第一次搜索相同的问题.

在NEST中执行与您在Sense中发送的查询匹配的查询字符串查询

client.Search<Employee>(s => s
    .Query(q => q
        .QueryString(qs => qs
            .Fields(f => f.Field(ff => ff.FirstName))
            .Query("Eldho")
        )
    )
);
Run Code Online (Sandbox Code Playgroud)

如果您使用问题中的连接设置使用NEST索引文档,那么这应该有效,尽管我在您的Sense查询中注意到您使用该字段获得结果"FirstName".要在NEST中更改字段推断以使其不具有驼峰字段/属性名称,我们可以使用

var settings = new ConnectionSettings(elasticServerUri)
    .DefaultFieldNameInferrer(p => p);

var client = new ElasticClient(settings);
Run Code Online (Sandbox Code Playgroud)