запрашивая упорядоченный поиск с использованием httpclient в asp.net/c# - PullRequest
0 голосов
/ 01 февраля 2019

Я пытаюсь отправить запрос на поиск, используя Httpclient.Я не могу отправить запрос в теле сообщения.Это либо неверный запрос, либо другие ошибки.Я пробовал разные способы отправки запроса, но он не работает.

     public string GetData() 
    {
        var query = "{\"query\":{\"bool\":{\"must\":[{\"match_all\":{}}],\"must_not\":[],\"should\":[]}},\"from\":0,\"size\":10,\"sort\":[],\"aggs\":{}}";
        // var content = new Newtonsoft.Json.Linq.JObject(query);
        HttpClient client = new HttpClient();
        client.BaseAddress = new Uri("http://localhost:9200/");            
        client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
        HttpResponseMessage response = client.PostAsJsonAsync("idx_bflmesh/_search", query).Result;  // Blocking call!    
        if (response.IsSuccessStatusCode)
        {
            var products = response.Content.ReadAsStringAsync().Result;

            return products;
        }
        else
        {
            Console.WriteLine("{0} ({1})", (int)response.StatusCode, response.ReasonPhrase);
            return response.ToString();
        } 
    }

актуально

    "StatusCode: 400, ReasonPhrase: 'Bad Request', Version: 1.1, Content: System.Net.Http.StreamContent, Headers:\u000d\u000a{\u000d\u000a  Content-Length: 249\u000d\u000a  Content-Type: application\/json; charset=UTF-8\u000d\u000a}"


expected:
{
  "took": 14,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 5,
    "max_score": 1,
    "hits": [
   {
    "_index": "comments_json",
    "_type": "doc",
    "_id": "DW3O5mUBwQtNYbVFHwrK",
    "_score": 1,
    "_source": {
      "@version": "1",
      "@timestamp": "2018-09-17T09:13:12.590Z",
      "parentcommentid": null,
      "createddate": "2018-04-14T20:20:30.000Z",
      "modifieddate": "2018-09-16T18:30:00.000Z",
      "likes": "[{"LikedBy":"10","Date":"17-09-2018"},{"LikedBy":"11","Date":"17-09-2018"},{"LikedBy":"12","Date":"17-09-2018"}]",
      "commentid": "58a069e4-78cb-47fe-9f3b-11b341ea0e8f",
      "text": "how to achieve it",
      "postid": "f784bb73-031c-4020-95ca-fa6aa1e70109",
      "commentby": "10"
    }
  },
  {
    "_index": "comments_json",
    "_type": "doc",
    "_id": "C23I5mUBwQtNYbVFogo2",
    "_score": 1,
    "_source": {
      "likes": "[{"LikedBy":"10","Date":"17-09-2018"},{"LikedBy":"11","Date":"17-09-2018"},{"LikedBy":"12","Date":"17-09-2018"}]",
      "@version": "1",
      "modifieddate": "2018-09-16T18:30:00.000Z",
      "parentcommentid": null,
      "text": "how to achieve it",
      "commentby": "10",
      "commentid": "fdb2c9d6-29fd-4e81-a883-90281982d56e",
      "postid": "f784bb73-031c-4020-95ca-fa6aa1e70109",
      "@timestamp": "2018-09-17T09:07:12.743Z",
      "createddate": "2018-09-14T20:10:30.000Z"
    }
  },
  {
    "_index": "comments_json",
    "_type": "doc",
    "_id": "Dm3P5mUBwQtNYbVFhgqM",
    "_score": 1,
    "_source": {
      "parentcommentid": null,
      "postid": "f784bb73-031c-4020-95ca-fa6aa1e70109",
      "modifieddate": "2018-09-16T18:30:00.000Z",
      "likes": "[{"LikedBy":"10","Date":"17-09-2018"},{"LikedBy":"11","Date":"17-09-2018"},{"LikedBy":"12","Date":"17-09-2018"}]",
      "@timestamp": "2018-09-17T09:14:44.400Z",
      "commentby": "10",
      "commentid": "1c66a941-e9d2-4318-9dca-44b7f058d69d",
      "createddate": "2018-10-14T20:20:30.000Z",
      "text": "how to achieve it",
      "@version": "1"
    }
  },
  {
    "_index": "comments_json",
    "_type": "doc",
    "_id": "Cm2_5mUBwQtNYbVFLwpf",
    "_score": 1,
    "_source": {
      "createddate": "2018-09-14T20:10:30.000Z",
      "parentcommentid": null,
      "commentid": "fdb2c9d6-29fd-4e81-a883-90281982d56e",
      "postid": "f784bb73-031c-4020-95ca-fa6aa1e70109",
      "commentby": "10",
      "likes": "[{"LikedBy":"10","Date":"17-09-2018"},{"LikedBy":"11","Date":"17-09-2018"},{"LikedBy":"12","Date":"17-09-2018"}]",
      "@version": "1",
      "modifieddate": "2018-09-16T18:30:00.000Z",
      "text": "how to achieve it",
      "@timestamp": "2018-09-17T08:56:53.543Z"
    }
  },
  {
    "_index": "comments_json",
    "_type": "doc",
    "_id": "DG3M5mUBwQtNYbVF9ArQ",
    "_score": 1,
    "_source": {
      "@version": "1",
      "commentby": "10",
      "@timestamp": "2018-09-17T09:11:56.053Z",
      "modifieddate": "2018-09-16T18:30:00.000Z",
      "parentcommentid": null,
      "commentid": "ccfb7ee5-e195-43a6-9bc6-c3ff4723a4f1",
      "postid": "f784bb73-031c-4020-95ca-fa6aa1e70109",
      "createddate": "2018-09-14T20:20:30.000Z",
      "text": "how to achieve it",
      "likes": "[{"LikedBy":"10","Date":"17-09-2018"},{"LikedBy":"11","Date":"17-09-2018"},{"LikedBy":"12","Date":"17-09-2018"}]"
    }
  }
]
}
}
...