无法查询嵌套对象

时间:2018-09-07 09:20:12

标签: node.js elasticsearch

Elasticsearch 6.2.4

索引具有映射:

{
  "watcher" : {
    "aliases" : { },
    "mappings" : {
      "doc" : {
        "properties" : {
          "script" : {
            "properties" : {
              "body" : {
                "type" : "text"
              },
              "description" : {
                "type" : "text"
              },
              "title" : {
                "type" : "text"
              }
            }
          },
          "super-user" : {
            "properties" : {
              "id" : {
                "type" : "text",
                "fields" : {
                  "keyword" : {
                    "type" : "keyword",
                    "ignore_above" : 256
                  }
                }
              },
              "password" : {
                "type" : "text"
              },
              "sha" : {
                "type" : "text"
              },
              "username" : {
                "type" : "text"
              }
            }
          },
          "watcher" : {
            "properties" : {
              "actions" : {
                "type" : "object",
                "enabled" : false
              },
              "condition" : {
                "type" : "object",
                "enabled" : false
              }
            }
          }
        }
      }
    }
  }
}

有一个我想通过其_source.super-user.id值获取的文档:

  {
    "_index" : "watcher",
    "_type" : "doc",
    "_id" : "sAkqs2UBN8hNgeAd6VYT",
    "_score" : 1.0,
    "_source" : {
      "super-user" : {
        "id" : "rwkTs2UBN8hNgeAd902q",
        "username" : "elastic",
        "sha" : "7598562076f37c7376ccf5c6ad28e00c:0fa96e2c4c0136b12ae1708940c46a52"
      }
    }
  }

如何获取此文档?

我尝试了嵌套查询:

const elasticsearch = require('elasticsearch');

const client = new elasticsearch.Client({
  host: [
    {
      host: 'localhost',
      protocol: 'http',
      auth: 'elastic:password',
      port: 9200
    }
  ]
});

(async () => {
  try {
    const resp = await client.search({
      index: 'watcher',
      type: 'doc',
      body: {
        query: {
          nested: {
            path: 'super-user',
            query: {
              bool: {
                must: [
                  {
                    match: {
                      'super-user.id': 'rwkTs2UBN8hNgeAd902q'
                    }
                  }
                ]
              }
            }
          }
        }
      }
    });

    console.log(JSON.stringify(resp, null, 2));
  } catch (err) {
    console.error(err);
  }
})();

但是我遇到了failed to create query错误:

{ Error: [query_shard_exception] failed to create query: {
  "nested" : {
    "query" : {
      "bool" : {
        "must" : [
          {
            "match" : {
              "super-user.id" : {
                "query" : "rwkTs2UBN8hNgeAd902q",
                "operator" : "OR",
                "prefix_length" : 0,
                "max_expansions" : 50,
                "fuzzy_transpositions" : true,
                "lenient" : false,
                "zero_terms_query" : "NONE",
                "auto_generate_synonyms_phrase_query" : true,
                "boost" : 1.0
              } 
            } 
          } 
        ],
        "adjust_pure_negative" : true,
        "boost" : 1.0
      } 
    },
    "path" : "super-user",
    "ignore_unmapped" : false,
    "score_mode" : "avg",
    "boost" : 1.0
  }
}, with { index_uuid="5O9HfcORTjiq5SZ0c1lkQA" & index="watcher" }
    at respond (/media/trex/safe/Development/private/node_modules/elasticsearch/src/lib/transport.js:307:15)
    at checkRespForFailure (/media/trex/safe/Development/private/node_modules/elasticsearch/src/lib/transport.js:266:7)
    at HttpConnector.<anonymous> (/media/trex/safe/Development/private/node_modules/elasticsearch/src/lib/connectors/http.js:159:7)
    at IncomingMessage.bound (/media/trex/safe/Development/private/node_modules/elasticsearch/node_modules/lodash/dist/lodash.js:729:21)
    at emitNone (events.js:111:20)
    at IncomingMessage.emit (events.js:208:7)
    at endReadableNT (_stream_readable.js:1064:12)
    at _combinedTickCallback (internal/process/next_tick.js:138:11)
    at process._tickCallback (internal/process/next_tick.js:180:9)
  status: 400,
    displayName: 'BadRequest',
  message: '[query_shard_exception] failed to create query: {\n  "nested" : {\n    "query" : {\n      "bool" : {\n        "must" : [\n          {\n            "match" : {\n              "super-user.id" : {\n                "query" : "rwkTs2UBN8hNgeAd902q",\n                "operator" : "OR",\n                "prefix_length" : 0,\n                "max_expansions" : 50,\n                "fuzzy_transpositions" : true,\n                "lenient" : false,\n                "zero_terms_query" : "NONE",\n                "auto_generate_synonyms_phrase_query" : true,\n                "boost" : 1.0\n              }\n            }\n          }\n        ],\n        "adjust_pure_negative" : true,\n        "boost" : 1.0\n      }\n    },\n    "path" : "super-user",\n    "ignore_unmapped" : false,\n    "score_mode" : "avg",\n    "boost" : 1.0\n  }\n}, with { index_uuid="5O9HfcORTjiq5SZ0c1lkQA" & index="watcher" }',
  path: '/watcher/doc/_search',
  query: {},
  body: 
   { error:
      { root_cause: [Array],
        type: 'search_phase_execution_exception',
        reason: 'all shards failed',
        phase: 'query',
        grouped: true,
        failed_shards: [Array] },
     status: 400 },
  statusCode: 400,
  response: '{"error":{"root_cause":[{"type":"query_shard_exception","reason":"failed to create query: {\\n  \\"nested\\" : {\\n    \\"query\\" : {\\n      \\"bool\\" : {\\n        \\"must\\" : [\\n          {\\n            \\"match\\" : {\\n              \\"super-user.id\\" : {\\n                \\"query\\" : \\"rwkTs2UBN8hNgeAd902q\\",\\n                \\"operator\\" : \\"OR\\",\\n                \\"prefix_length\\" : 0,\\n                \\"max_expansions\\" : 50,\\n                \\"fuzzy_transpositions\\" : true,\\n                \\"lenient\\" : false,\\n                \\"zero_terms_query\\" : \\"NONE\\",\\n                \\"auto_generate_synonyms_phrase_query\\" : true,\\n                \\"boost\\" : 1.0\\n              }\\n            }\\n          }\\n        ],\\n        \\"adjust_pure_negative\\" : true,\\n        \\"boost\\" : 1.0\\n      }\\n    },\\n    \\"path\\" : \\"super-user\\",\\n    \\"ignore_unmapped\\" : false,\\n    \\"score_mode\\" : \\"avg\\",\\n    \\"boost\\" : 1.0\\n  }\\n}","index_uuid":"5O9HfcORTjiq5SZ0c1lkQA","index":"watcher"}],"type":"search_phase_execution_exception","reason":"all shards failed","phase":"query","grouped":true,"failed_shards":[{"shard":0,"index":"watcher","node":"OHtckm41Ts2DwDlT0A7N4w","reason":{"type":"query_shard_exception","reason":"failed to create query: {\\n  \\"nested\\" : {\\n    \\"query\\" : {\\n      \\"bool\\" : {\\n        \\"must\\" : [\\n          {\\n            \\"match\\" : {\\n              \\"super-user.id\\" : {\\n                \\"query\\" : \\"rwkTs2UBN8hNgeAd902q\\",\\n                \\"operator\\" : \\"OR\\",\\n                \\"prefix_length\\" : 0,\\n                \\"max_expansions\\" : 50,\\n                \\"fuzzy_transpositions\\" : true,\\n                \\"lenient\\" : false,\\n                \\"zero_terms_query\\" : \\"NONE\\",\\n                \\"auto_generate_synonyms_phrase_query\\" : true,\\n                \\"boost\\" : 1.0\\n              }\\n            }\\n          }\\n        ],\\n        \\"adjust_pure_negative\\" : true,\\n        \\"boost\\" : 1.0\\n      }\\n    },\\n    \\"path\\" : \\"super-user\\",\\n    \\"ignore_unmapped\\" : false,\\n    \\"score_mode\\" : \\"avg\\",\\n    \\"boost\\" : 1.0\\n  }\\n}","index_uuid":"5O9HfcORTjiq5SZ0c1lkQA","index":"watcher","caused_by":{"type":"illegal_state_exception","reason":"[nested] nested object under path [super-user] is not of nested type"}}}]},"status":400}',
  toString: [Function],
  toJSON: [Function] }

1 个答案:

答案 0 :(得分:2)

nested是一种特定的数据类型:https://www.elastic.co/guide/en/elasticsearch/reference/current/nested.html

在映射中,您没有指定它,因此请使用简单的点表示法选择没有嵌套查询的正确字段。