2017-01-08 7 views
2

私の正しいインデックスパスはですが、コードヒット数はPOST: /foo/bar/_searchです。ElasticSearch.NET NEST検索 url

var node = new Uri("http://elasticsearch-server.com:9200"); 
var settings = new ConnectionSettings(node); 
settings.DefaultIndex("foo"); 
var client = new ElasticClient(settings); 
var response = client.Search<Bar>(s => s 
.Query(q => q.Term(o => o.userName, "test")) 
); 

// POCO for response fields 
public class Bar 
{ 
    public int userId { get; set; } 
    public string userName { get; set; } 
    public DateTime createdTime { get; set; } 
} 

上記コードresponseが返されます。

POSTに成功した低レベルの呼び出しから構築された有効なNEST応答:/ fooの/バー/ _search

私は正確にパスを検索する設定できますか?

試験1

Iはsettings.DefaultIndex("foo");線を省略した場合、それは以下のようにArgumentExceptionをスローし、私はDefaultIndex()を設定すると、Search<T>は、第二のパスとしてT名を使用します。

ArgumentException:指定された型のインデックス名がnullであり、既定のインデックスが設定されていません。 ConnectionSettings.MapDefaultTypeIndices()を使用してインデックス名をマッピングするか、ConnectionSettings.DefaultIndex()を使用してデフォルトのインデックスを設定します。

試験2 コード上documentation

var settings = new ConnectionSettings(node) 
.MapDefaultTypeIndices(m => m.Add(typeof(Bar), "foo")); 

を参照して対応して同じ結果を返します。

POSTに成功した低レベルの呼び出しから構築された有効なNEST応答:/ fooの/バー/ _search

+0

を無効にするために、検索流暢APIに追加メソッドを呼び出すことができます。このコードスニペットを試してみてください:var settings = new ConnectionSettings(新しいUri( "http://elasticsearch-server.com:9200"))。DefaultIndex( "foo"); var client = new ElasticClient(settings); – Soren

+0

@Soren //ご意見ありがとうございます。うーん...まだ同じ問題があります。 POCO名を第2のパスとして使用します。私はもっ​​と調査する必要があります。 – Youngjae

答えて

5

NESTで公開されているElasticsearch APIの大部分は、強く型付けされています(.Search<T>()など)。このエンドポイントでは、"index""type"の両方がTから推測されますが、推測される値に異なる値を設定することもできます。 (オブジェクト初期化構文を使用している場合、または検索対象)これらのケースでは、私は同じコードを持って、それがうまく機能し、推論値

void Main() 
{ 
    var pool = new SingleNodeConnectionPool(new Uri("http://localhost:9200")); 
    var connectionSettings = new ConnectionSettings(pool) 
      .DefaultIndex("foo"); 

    var client = new ElasticClient(connectionSettings); 

    // POST http://localhost:9200/foo/bar/_search 
    // Will try to deserialize all _source to instances of Bar 
    client.Search<Bar>(s => s 
     .MatchAll() 
    ); 

    // POST http://localhost:9200/foo/_search 
    // Will try to deserialize all _source to instances of Bar 
    client.Search<Bar>(s => s 
     .AllTypes() 
     .MatchAll() 
    ); 

    // POST http://localhost:9200/_search 
    // Will try to deserialize all _source to instances of Bar 
    client.Search<Bar>(s => s 
     .AllTypes() 
     .AllIndices() 
     .MatchAll() 
    ); 

    connectionSettings = new ConnectionSettings(pool) 
      .InferMappingFor<Bar>(m => m 
       .IndexName("bars") 
       .TypeName("barbar") 
      ); 

    client = new ElasticClient(connectionSettings); 

    // POST http://localhost:9200/bars/barbar/_search 
    // Will try to deserialize all _source to instances of Bar 
    client.Search<Bar>(s => s 
     .MatchAll() 
    ); 

    // POST http://localhost:9200/bars/_search 
    // Will try to deserialize all _source to instances of Bar 
    client.Search<Bar>(s => s 
     .AllTypes() 
     .MatchAll() 
    ); 

    // POST http://localhost:9200/_all/barbar/_search 
    // Will try to deserialize all _source to instances of Bar 
    client.Search<Bar>(s => s 
     .AllIndices() 
     .MatchAll() 
    ); 

    // POST http://localhost:9200/_search 
    // Will try to deserialize all _source to instances of Bar 
    client.Search<Bar>(s => s 
     .AllIndices() 
     .AllTypes() 
     .MatchAll() 
    ); 
} 


public class Bar 
{ 
    public int userId { get; set; } 
    public string userName { get; set; } 
    public DateTime createdTime { get; set; } 
} 
2

あなたは、私がいた他のパラメータ検索ラムダ式でvar response = client.Search<Bar>(s => s.Index("indexName").Query(q => q.Term(o => o.userName, "test")));

+0

Hmm ... 'Index(" indexName ")'または 'DefaultIndex(" indexName ")'は同じパスを与えます。 – Youngjae

+0

'.DefaultIndex(" defaultIndex ")'は、要求にインデックス名が指定されておらず、指定されたタイプ 'T'に対して推定されるインデックス名が設定されていない場合に使用されます。詳細については、インデックス名の推論を参照してください:https://www.elastic.co/guide/en/elasticsearch/client/net-api/5.x/index-name-inference.html –

0

を追加することができますElasticSearchの初心者で、_typeについて知りませんでした。

私はPOCOクラス名に同じ_typeという名前を設定していますが、期待通りに動作します。 だから、{index}/{type}はどのようなパス式であると言うことができます。

関連する問題