2

I have the following Elastic Search query:

 {
    "query": {
        "filtered": {
            "query": {
                "multi_match": {
                    "query": "main",
                    "type": "cross_fields",
                    "fields": [
                        "field1^5",
                        "test",
                        "field2",
                        "abc"
                    ],
                    "operator": "and"
                }
            }
        }
    },
    "sort": [],
    "from": 0,
    "size": 20
}

I am trying to run this query using NEST client for Elastic Search & using "QueryRaw" attribute but getting Error.

Can someone please provide some input where I am going wrong

var uri = new Uri("Elastic_Search_Cluster_Name");
var settings = new ConnectionSettings(uri, defaultIndex: "testIndex");
var client = new ElasticClient(settings);
var resp=client.Search<dynamic>(q => 
q.Type("mappingType").QueryRaw(inputRequest.ToString())
);

Error Log:

Failed to execute phase [query], all shards failed; shardFailures {[27THF3S_QuaBLRj11MgqfA][testIndex][0]: RemoteTransportException[[pdm64-ironman][inet[/server]][indices:data/read/search[phase/query]]]; nested: SearchParseException[[testIndex][0]: query[abc:main field2:main test:main field1:main^5.0],from[-1],size[-1]: Parse Failure [Failed to parse source [{
  "query": {
  "filtered": {
    "query": {
      "multi_match": {
        "query": "main",
        "type": "cross_fields",
        "fields": [
          "field1^5",
          "test",
          "field2",
          "abc"
        ],
        "operator": "and"
      }
    }
  },
  "sort": [],
  "from": 0,
  "size": 20
}
}]]]; nested: ElasticsearchParseException[Expected field name but got START_ARRAY "sort"]; }{[b5YxyDCcQEmSlCd9y3Sfww][testIndex][1]: RemoteTransportException[[pdm65-hulk][inet[/server]][indices:data/read/search[phase/query]]]; nested: SearchParseException[[testIndex][1]: query[abc:main field2:main test:main field1:main^5.0],from[-1],size[-1]: Parse Failure [Failed to parse source [{
  "query": {
  "filtered": {
    "query": {
      "multi_match": {
        "query": "main",
        "type": "cross_fields",
        "fields": [
          "field1^5",
          "test",
          "field2",
          "abc"
        ],
        "operator": "and"
      }
    }
  },
  "sort": [],
  "from": 0,
  "size": 20
}
}]]];
code_blue
  • 523
  • 2
  • 9
  • 19

1 Answers1

4

The entire search request cannot be passed into QueryRaw. You can only pass the JSON request that is supposed to be included inside a "query" object into QueryRaw. Hence if you pass the below JSON body into QueryRaw it should work.

{
   "filtered": {
      "query": {
         "multi_match": {
            "query": "main",
            "type": "cross_fields",
            "fields": [
               "field1^5",
               "test",
               "field2",
               "abc"
            ],
            "operator": "and"
         }
      }
   }
}

But "sort", "from" and "size" objects are not inside "query" and hence Nest fails to parse your request.

What your Nest code should look like is as below:

var uri = new Uri("Elastic_Search_Cluster_Name");
var settings = new ConnectionSettings(uri, defaultIndex: "testIndex");
var client = new ElasticClient(settings);
var resp = client.Search<dynamic>(q => q
    .Type("mappingType")
    .QueryRaw(<raw query string>)
    .From(0)
    .Size(20)
);

I left out Sort() in the above Nest search request because you were not doing anything with it anyways.

bittusarkar
  • 6,247
  • 3
  • 30
  • 50
  • "You can only pass the JSON request that is supposed to be included inside a "query" object into QueryRaw" - is there a way to prevent this behavior and pass the whole query string through? – daviddeath Oct 29 '15 at 19:06
  • 1
    @daviddeath For that you'll need to use the Raw client of Nest. Mind you, in that case, the response you'll receive will also be in raw format. – bittusarkar Oct 30 '15 at 08:49