2

我有以下弹性搜索查询:

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

我正在尝试使用 NEST 客户端进行弹性搜索并使用“ QueryRaw ”属性运行此查询,但出现错误。

有人可以在我出错的地方提供一些输入吗

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())
);

错误日志:

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
}
}]]];
4

1 回答 1

4

无法将整个搜索请求传递到QueryRaw. 您只能将应该包含在"query"对象中的 JSON 请求传递到QueryRaw. 因此,如果您将下面的 JSON 正文传递给QueryRaw它应该可以工作。

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

但是"sort""from"并且"size"对象不在内部"query",因此 Nest 无法解析您的请求。

您的 Nest 代码应如下所示:

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)
);

我在上面的 Nest 搜索请求中遗漏了Sort(),因为无论如何你都没有对它做任何事情。

于 2015-04-27T12:44:41.853 回答