简体   繁体   English

通过 Nest 中的 SearchRequest 类设置索引名称

[英]Set Index name by SearchRequest class in Nest

I use Nest client to use ElasticSearch .I want to search in ElasticSearch :我使用 Nest 客户端使用 ElasticSearch 。我想在 ElasticSearch 中搜索:

SearchRequest countRequest = new SearchRequest
{
  //Somthing
};

client.Search<Post>(countRequest);

On other hand :在另一方面 :

client.Search<Post>(s=>s.Index("IndexName").Query(...))

How i can set index name by SearchRequest class search ?如何通过 SearchRequest 类搜索设置索引名称?

This is for those using newer versions of NEST.这适用于使用较新版本 NEST 的用户。 In 2.0.1, I am unable to find the Indices property in SearchRequest .在 2.0.1 中,我无法在SearchRequest找到Indices属性。 However, you can pass them in through the constructor:但是,您可以通过构造函数传递它们:

var request = new SearchRequest<Post>("IndexName", "TypeName");

I map the index and type on the ConnectionSettings like so.我像这样映射索引并在ConnectionSettings上键入。

ConnectionSettings settings = new ConnectionSettings("url");
settings.MapDefaultTypeIndices(t => t.Add(typeof(Post), "IndexName"));
settings.MapDefaultTypeNames(t => t.Add(typeof(Post), "TypeName"));

Other ways to tell NEST the index and type:告诉 NEST 索引和类型的其他方法:

client.Search<Post>(s => s.Index("IndexName").Type("TypeName").From(0));

or apply the ElasticsearchTypeAttribute on the type.或在类型上应用ElasticsearchTypeAttribute

[ElasticsearchType(Name = "TypeName")]
public class Post{ }

SearchRequest contains an Indices property, so that you can specify multiple indices to search across. SearchRequest包含一个Indices属性,以便您可以指定要搜索的多个索引。 In your case, you could just pass the single index like so:在您的情况下,您可以像这样传递单个索引:

var request = new SearchRequest
{
    Indices = new IndexNameMarker[] { "IndexName" }
};

Another option would be to map your Post type to the index it belongs to, and use the typed SearchRequest<T> to let NEST infer the index name.另一种选择是将您的Post类型映射到它所属的索引,并使用类型化的SearchRequest<T>让 NEST 推断索引名称。

I was trying to solve a bit different task with ES v5 (json request was pushed from the file) but also had the same problem with setting the indexName.我试图用 ES v5 解决一个有点不同的任务(json 请求是从文件中推送的),但在设置 indexName 时也遇到了同样的问题。 So, my solution was to add index querystring parameter.所以,我的解决方案是添加index查询字符串参数。 Using this in integration tests:在集成测试中使用它:

public static class ElasticSearchClientHelper
{
    public static ISearchResponse<T> SearchByJson<T>(this IElasticClient client, string json, string indexName, Dictionary<string, object> queryStringParams = null) where T : class
    {
        var qs = new Dictionary<string, object>()
        {
            {"index", indexName}
        };
        queryStringParams?.ForEach(pair => qs.Add(pair.Key, pair.Value));

        using (var stream = new MemoryStream(Encoding.UTF8.GetBytes(json)))
        {
            var searchRequest = client.Serializer.Deserialize<SearchRequest>(stream);
            ((IRequestParameters)((IRequest<SearchRequestParameters>)searchRequest).RequestParameters).QueryString = qs;
            return client.Search<T>(searchRequest);
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM