使用NEST& amp; Elasticsearch查询QueryRaw

时间:2015-04-27 10:36:07

标签: elasticsearch nest elasticsearch-net

我有以下弹性搜索查询:

 {
    "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
}
}]]];

1 个答案:

答案 0 :(得分: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(),因为无论如何你都没有做任何事情。