Elasticsearch错误:[parsing_exception] [match_phrase]查询不支持[first_name]

时间:2019-10-11 20:42:41

标签: node.js elasticsearch

elasticsearch npm模块遇到问题。这是我的查询:

try {
    const {
        hits: { hits }
    } = await client.search({
        index: "articles",
        body: {
            query: {
                match_phrase: {
                    authors: {
                        first_name: firstName,
                        last_name: lastName
                    }
                }
            }
        }
    });

    hits.forEach(hit => {
        hit.type = TYPE_PHRASE;
    });

    return hits;
} catch (err) {

但这会返回错误,

Error: [parsing_exception] [match_phrase] query does not support [first_name]

我不确定这是什么意思。这是否意味着我无法通过first_name进行搜索?

这是elasticsearch结构:

 [
      {
           "_score": 10.8702135,
           "_source": {
                "title": "Some title",
                "authors": [
                     {
                          "first_name": "John",
                          "last_name": "Smith",
                     },
                     {
                          "first_name": "Jane",
                          "last_name": "Doe",
                     },

2 个答案:

答案 0 :(得分:2)

Match_phrase适用于单个字段。要搜索多个字段,请使用must / bool子句组合多个match_phrase子句。

query: {
          bool: {
                     must: [
                               {
                                   match_phrase: {
                                                   authors.first_name: firstName
                                                 }                  
                                },
                                {
                                   match_phrase: {
                                                   authors.last_name: lastName
                                                 }                  
                                },
                            ]
                 }                
            }

答案 1 :(得分:0)

您可能还希望将名字和姓氏对象作为嵌套对象。这将帮助您正确搜索名字和姓氏。

有关相同和几乎相似的问题的更多详细信息,请参见https://www.elastic.co/guide/en/elasticsearch/reference/current/nested.html