首页 > 解决方案 > 如何显示elasticsearch JS客户端返回的对象中的某些字段?

问题描述

我是 Javascript 和 Elasticsearch 的新手。我正在尝试将 elasticsearch 返回的结果显示到基于 javascript 的网页。ES返回的结果是嵌套对象的形式。但是,我无法访问第一级嵌套之外的字段。我可以访问 results.hits.hits 但不能访问 results.hits.hits.source (这给了我未定义的错误)。

我可以访问 results.hits.hits 但不能访问 results.hits.hits.source (这给了我未定义的错误)。

  var query = req.query.search;
  let body = {
      size:200,
      from:0,
      query:{
          multi_match:{
              "query": query,
              "fields":["name","description","views_names_*"]
          }
      }
  }

  client.search({index:'test', body:body,type:'doc'})
  .then(results => {

      data = results.hits.hits.source;

      var i;
      var jsonres = [];

      console.log(data);
      res.render("results",{data:data});
      // console.log(data);
  })

这是返回的结果对象的结构:

{ took: 4,
  timed_out: false,
  _shards: { total: 5, successful: 5, skipped: 0, failed: 0 },
  hits:
   { total: 16,
     max_score: 1.8375107,
     hits:
      [ [Object],
        [Object],
        [Object],
        [Object],
        [Object],
        [Object],
        [Object],
        [Object],
        [Object],
        [Object],
        [Object],
        [Object],
        [Object],
        [Object],
        [Object],
        [Object] ] } }

其中 results.hits.hits 对象如下:

{ _index: 'test',
  _type: 'doc',
  _id: 'LNkmq2wBD2jaP95Fz32N',
  _score: 1.8375107,
  _source:
   { name: 'Tableau Grouper via API',
     id: 'e1f1bdd8-2cd4-4b7c-8c88-4501c121cff0',
     project_id: 'fbef67da-fefc-4225-9bd7-80fecfbb29f0',
     project_name: 'Server Administration',
     views_names_0: 'Tableau to Grouper on AD Groups',
     views_names_1: 'Tableau Group vetted to Grouper',
     views_owner_ids_0: null,
     views_owner_ids_1: null,
     views_ids_0: 'a9236201-5d67-49b5-8933-a201c2037c9f',
     views_ids_1: '457630eb-fae7-4de3-9b81-db229fb4e916',
     views_workbook_ids_0: 'e1f1bdd8-2cd4-4b7c-8c88-4501c121cff0',
     views_workbook_ids_1: 'e1f1bdd8-2cd4-4b7c-8c88-4501c121cff0',
     views_viewContentURLs_0: 'TableauGrouperviaAPI/sheets/TableautoGrouperonADGroups',
     views_viewContentURLs_1: 'TableauGrouperviaAPI/sheets/TableauGroupvettedtoGrouper',
      } }

标签: javascriptelasticsearchweb-applications

解决方案


您访问的字段名称错误。它应该是“_source”而不是“source”。这就是你变得不确定的原因。

而且,如果您在命中中有多个对象,那么您将不得不遍历数据。

我已经复制了您的数据并为单个和多个字典创建了示例代码。

希望这可以帮助

<script type="text/javascript">
	
let data = [{
  _index: 'test',
  _type: 'doc',
  _id: 'LNkmq2wBD2jaP95Fz32N',
  _score: 1.8375107,
  _source: {
    name: 'Tableau Grouper via API',
    id: 'e1f1bdd8-2cd4-4b7c-8c88-4501c121cff0',
    project_id: 'fbef67da-fefc-4225-9bd7-80fecfbb29f0',
    project_name: 'Server Administration',
    views_names_0: 'Tableau to Grouper on AD Groups',
    views_names_1: 'Tableau Group vetted to Grouper',
    views_owner_ids_0: null,
    views_owner_ids_1: null,
    views_ids_0: 'a9236201-5d67-49b5-8933-a201c2037c9f',
    views_ids_1: '457630eb-fae7-4de3-9b81-db229fb4e916',
    views_workbook_ids_0: 'e1f1bdd8-2cd4-4b7c-8c88-4501c121cff0',
    views_workbook_ids_1: 'e1f1bdd8-2cd4-4b7c-8c88-4501c121cff0',
    views_viewContentURLs_0: 'TableauGrouperviaAPI/sheets/TableautoGrouperonADGroups',
    views_viewContentURLs_1: 'TableauGrouperviaAPI/sheets/TableauGroupvettedtoGrouper'
  }
},
{
  _index: 'test',
  _type: 'doc',
  _id: 'LNkmq2wBD2jaP95Fz32N',
  _score: 1.8375107,
  _source: {
    name: 'Tableau Grouper via API',
    id: 'e1f1bdd8-2cd4-4b7c-8c88-4501c121cff0',
    project_id: 'fbef67da-fefc-4225-9bd7-80fecfbb29f0',
    project_name: 'Server Administration',
    views_names_0: 'Tableau to Grouper on AD Groups',
    views_names_1: 'Tableau Group vetted to Grouper',
    views_owner_ids_0: null,
    views_owner_ids_1: null,
    views_ids_0: 'a9236201-5d67-49b5-8933-a201c2037c9f',
    views_ids_1: '457630eb-fae7-4de3-9b81-db229fb4e916',
    views_workbook_ids_0: 'e1f1bdd8-2cd4-4b7c-8c88-4501c121cff0',
    views_workbook_ids_1: 'e1f1bdd8-2cd4-4b7c-8c88-4501c121cff0',
    views_viewContentURLs_0: 'TableauGrouperviaAPI/sheets/TableautoGrouperonADGroups',
    views_viewContentURLs_1: 'TableauGrouperviaAPI/sheets/TableauGroupvettedtoGrouper'
  }
}
]

let single_data = {
  _index: 'test',
  _type: 'doc',
  _id: 'LNkmq2wBD2jaP95Fz32N',
  _score: 1.8375107,
  _source: {
    name: 'Tableau Grouper via API',
    id: 'e1f1bdd8-2cd4-4b7c-8c88-4501c121cff0',
    project_id: 'fbef67da-fefc-4225-9bd7-80fecfbb29f0',
    project_name: 'Server Administration',
    views_names_0: 'Tableau to Grouper on AD Groups',
    views_names_1: 'Tableau Group vetted to Grouper',
    views_owner_ids_0: null,
    views_owner_ids_1: null,
    views_ids_0: 'a9236201-5d67-49b5-8933-a201c2037c9f',
    views_ids_1: '457630eb-fae7-4de3-9b81-db229fb4e916',
    views_workbook_ids_0: 'e1f1bdd8-2cd4-4b7c-8c88-4501c121cff0',
    views_workbook_ids_1: 'e1f1bdd8-2cd4-4b7c-8c88-4501c121cff0',
    views_viewContentURLs_0: 'TableauGrouperviaAPI/sheets/TableautoGrouperonADGroups',
    views_viewContentURLs_1: 'TableauGrouperviaAPI/sheets/TableauGroupvettedtoGrouper'
  }
}

// when working with a list of dictionary, loop through the data this way
console.log("looping through multiple data");
for (var i = 0; i < data.length; i++) {
	console.log(data[i]._source);
}

// this will work for single dictionary
console.log("looping through single data");
console.log(single_data._source);
</script>


推荐阅读