使用 NEST 和 QueryRaw 的 Elasticsearch 查询

Elasticsearch query using NEST & QueryRaw

我有以下弹性搜索查询:

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

我正在尝试 运行 使用用于 Elastic Search 的 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
}
}]]];

无法将整个搜索请求传递到 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(),因为你根本没有用它做任何事情。