Elasticsearch.Net 异常:[match] query doesn't support multiple fields, found [field] and [query]

用Elasticsearch.Net检索数据,报异常:

var settings = new ConnectionConfiguration(new Uri("http://localhost:9200")).RequestTimeout(TimeSpan.FromMinutes(2));

ElasticLowLevelClient client = new ElasticLowLevelClient(settings);

var searchResponse = client.Search<StringResponse>("test", "Person", PostData.Serializable(new
{
    from = 0,
    size = 10,
    query = new { match = new { field = "name", query = "chenzongyan" }}
}));

string body = searchResponse.Body;
Console.WriteLine(body);
Console.ReadKey();

异常信息:

{
    "error": {
        "root_cause": [
            {
                "type": "parsing_exception",
                "reason": "[match] query doesn't support multiple fields, found [field] and [query]",
                "line": 1,
                "col": 62
            }
        ],
        "type": "parsing_exception",
        "reason": "[match] query doesn't support multiple fields, found [field] and [query]",
        "line": 1,
        "col": 62
    },
    "status": 400
}

解决办法:

var searchResponse = client.Search<StringResponse>("test", "Person", PostData.Serializable(new
            {
                from = 0,
                size = 10,
                query = new
                {
                    multi_match = new
                    {
                        fields = "name",
                        query = "chenzongyan"
                    }
                }
            }));

 将match 改为 multi_match ,field改为fields.

  检索结果:

{
    "took": 2,
    "timed_out": false,
    "_shards": {
        "total": 5,
        "successful": 5,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": 1,
        "max_score": 0.2876821,
        "hits": [
            {
                "_index": "test",
                "_type": "Person",
                "_id": "WOgCs2UBfhVuaFPoccea",
                "_score": 0.2876821,
                "_source": {
                    "name": "chenzongyan",
                    "Age": 27
                }
            }
        ]
    }
}

参考:https://www.elastic.co/guide/en/elasticsearch/client/net-api/current/elasticsearch-net-getting-started.html

原文地址:https://www.cnblogs.com/chenzongyan/p/9625967.html