Вопрос

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