문제

I have Elasticsearch up and running. Using Sense within Marvel, I am able to get a result, with this query:

GET _search
{
  "query": {
    "query_string": {
      "query": "massa"
    }
  }
}

My c# code, trying to recreate the above:

    var node = new Uri("http://localhost:9200");
    var settings = new ConnectionSettings(node).SetDefaultIndex("mediaitems");
    var client = new ElasticClient(settings);

    var results = client.Search<stuff>(s => s
        .Query(qs => qs.QueryString(q => q.Query("massa"))));

    var d = results.Documents;

But unfortunately I'm not getting any results, nothing in "results.Documents". Any suggestions? Maybe a way to see the generated json? What is the simplest way to just query everything in an index? Thanks!

도움이 되었습니까?

해결책

Even though your search results are going to be mapped to the proper type because you are using .Search<stuff>, you still need to set the default type as part of your query.

    var node = new Uri("http://localhost:9200");
    var settings = new ConnectionSettings(node).SetDefaultIndex("mediaitems");
    var client = new ElasticClient(settings);

    var results = client.Search<stuff>(s => s
        .Type("stuff") //or .Type(typeof(stuff)) if you have decorated your stuff class correctly.
        .Query(qs => qs.QueryString(q => q.Query("massa"))));

    var d = results.Documents;

Additionally, your results response contains a ConnectionStatus property. You can interrogate this property to see the Request and Response to/from Elasticsearch to see if your query is being executed as you expect.

Update: You can also set a default type the index settings as well.

  var settings = new ConnectionSettings(node).SetDefualtIndex("mediaitems");
  settings.MapDefaultTypeIndices(d=>d.Add(typeof(stuff), "mediaitems");

다른 팁

You can also check nest raw client

var results = client.Raw.SearchPost("mediaitems", "stuff", new
      {
          query = new
          {
              query_string = new
              {
                      query = "massa"
              }
          }
      });

You can get the values of search request URL and JSON request body as under:

var requestURL = response.RequestInformation.RequestUrl;
var jsonBody = Encoding.UTF8.GetString(response.RequestInformation.Request);

You can find other useful properties in RequestInformation for debugging.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top