代码之家  ›  专栏  ›  技术社区  ›  srgbnd

无法查询嵌套对象

  •  0
  • srgbnd  · 技术社区  · 6 年前

    弹性搜索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 回复  |  直到 6 年前
        1
  •  3
  •   Thomas Decaux    6 年前