如何使用搜索API搜索给定索引中的所有文档?

时间:2017-12-13 20:04:05

标签: c# .net elasticsearch kibana nest

我正在尝试使用NEST搜索API获取索引中的所有(2个doc)文档。

我在Kibana的控制台创建了一个索引“Masato”,当我运行索引列表时,我可以在那里看到它。

GET / _cat / indices?v

enter image description here

索引“Masato”包含两个文件。

GET / masato / doc / _search?q = *

{
  "took": 0,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "failed": 0
  },
  "hits": {
    "total": 2,
    "max_score": 1,
    "hits": [
      {
        "_index": "masato",
        "_type": "doc",
        "_id": "2",
        "_score": 1,
        "_source": {
          "name": "Foo Bar"
        }
      },
      {
        "_index": "masato",
        "_type": "doc",
        "_id": "1",
        "_score": 1,
        "_source": {
          "name": "John Doe"
        }
      }
    ]
  }
}

现在..我正在尝试使用搜索API来获取这些信息。 (我有.NET Web API设置)请注意我将实际的URI替换为'xxxxx'

namespace DockerTest.Controllers
{
    public class ElasticSearchController : ApiController
    {
        [HttpGet]
        public IHttpActionResult GetElasticSearch()
        {
            try
            {
                var settings = new ConnectionSettings(new Uri("xxxxx"))
                .EnableDebugMode();

                var client = new ElasticClient(settings);

                var searchResponse = client.Search<Masato>(s => 
                s.Index("masato")
                .Type("doc"));

                return Ok(searchResponse.DebugInformation);

            }
            catch(Exception ex)
            {
                return Ok(ex.Message + ":::::" + ex.StackTrace);
            }

当执行上面的代码时,searchResponse.DebugInformation将返回给浏览器并显示404 Not Found:

done Invalid NEST response built from a unsuccessful low level call on POST: /masato/doc/_search
# Audit trail of this API call:
 - [1] BadResponse: Node: http://xxxxx Took: 00:00:00.3458454
# ServerError: ServerError: -1Type:  Reason: "Not Found"
# OriginalException: System.Net.WebException: The remote server returned an error: (404) Not Found.
   at System.Net.HttpWebRequest.GetResponse()
   at Elasticsearch.Net.HttpConnection.Request[TReturn](RequestData requestData)
# Request:
{}
# Response:
{"statusCode":404,"error":"Not Found"}

我在这个阶段有点陷入困境,请有人给我一些帮助。 感谢

0 个答案:

没有答案