当前位置: 首页 > 面试题库 >

在Elasticsearch中查找不同的内部对象

宋唯
2023-03-14
问题内容

我们正在尝试在Elasticsearch中找到不同的内部对象。这将是我们案例的最小示例。我们一直坚持下面的映射(更改类型或索引或添加新字段不会有问题,但结构应保持原样):

{
  "building": {
    "properties": {
      "street": {
        "type": "string",
        "store": "yes",
        "index": "not_analyzed"
      },
      "house number": {
        "type": "string",
        "store": "yes",
        "index": "not_analyzed"
      },
      "city": {
        "type": "string",
        "store": "yes",
        "index": "not_analyzed"
      },
      "people": {
        "type": "object",
        "store": "yes",
        "index": "not_analyzed",
        "properties": {
          "firstName": {
            "type": "string",
            "store": "yes",
            "index": "not_analyzed"
          },
          "lastName": {
            "type": "string",
            "store": "yes",
            "index": "not_analyzed"
          }
        }
      }
    }
  }
}

假设我们有以下示例数据:

{
  "buildings": [
    {
      "street": "Baker Street",
      "house number": "221 B",
      "city": "London",
      "people": [
        {
          "firstName": "John",
          "lastName": "Doe"
        },
        {
          "firstName": "Jane",
          "lastName": "Doe"
        }
      ]
    },
    {
      "street": "Baker Street",
      "house number": "5",
      "city": "London",
      "people": [
        {
          "firstName": "John",
          "lastName": "Doe"
        }
      ]
    },
    {
      "street": "Garden Street",
      "house number": "1",
      "city": "London",
      "people": [
        {
          "firstName": "Jane",
          "lastName": "Smith"
        }
      ]
    }
  ]
}

当查询街道“贝克街”(以及所需的任何其他选项)时,我们希望获得以下列表:

[
    {
      "firstName": "John",
      "lastName": "Doe"
    },
    {
      "firstName": "Jane",
      "lastName": "Doe"
    }
]

格式并不重要,但是我们应该能够解析名字和姓氏。只是,由于我们的实际数据集要大得多,因此我们需要使输入项不同。

我们正在使用Elasticsearch 1.7。


问题答案:

我们终于解决了我们的问题。

我们的解决方案是(如我们预期的那样)一个预先计算的people_all字段。但是在导入数据时,我们正在编写其他字段,而不是使用copy_toor
transform而是在编写它。该字段如下所示:

"people": {
  "type": "nested",
  ..
  "properties": {
    "firstName": {
      "type": "string",
      "store": "yes",
      "index": "not_analyzed"
    },
    "lastName": {
      "type": "string",
      "store": "yes",
      "index": "not_analyzed"
    },
    "people_all": {
      "type": "string",
      "index": "not_analyzed"
    }
  }
}

"index": "not_analyzed"people_all现场注意。这对于拥有完整的存储桶很重要。如果您不使用它,我们的示例将返回3个存储桶“ john”,“
jane”和“ doe”。

编写完这个新字段后,我们可以进行如下操作:

{
  "size": 0,
  "query": {
    "term": {
      "street": "Baker Street"
    }
  },
  "aggs": {
    "people_distinct": {
      "nested": {
        "path": "people"
      },
      "aggs": {
        "people_all_distinct": {
          "terms": {
            "field": "people.people_all",
            "size": 0
          }
        }
      }
    }
  }
}

我们返回以下响应:

{
  "took": 2,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "failed": 0
  },
  "hits": {
    "total": 2,
    "max_score": 0.0,
    "hits": []
  },
  "aggregations": {
    "people_distinct": {
      "doc_count": 3,
      "people_name_distinct": {
        "doc_count_error_upper_bound": 0,
        "sum_other_doc_count": 0,
        "buckets": [
          {
            "key": "John Doe",
            "doc_count": 2
          },
          {
            "key": "Jane Doe",
            "doc_count": 1
          }
        ]
      }
    }
  }
}

现在,在响应中,我们可以创建不同的人员对象。

请让我们知道是否有更好的方法来实现我们的目标。
解析存储桶不是最佳解决方案,firstName并且lastName在每个存储桶中都包含字段会更加有趣。



 类似资料:
  • 问题内容: Elasticsearch文档 建议 *他们的一段代码 *文件固定 对应于sql查询 但实际上对应于 我不想知道我有多少不同的值,但是什么是不同的值。有人知道如何实现吗? 问题答案: 在字段上使用术语汇总。并且您需要注意如何分析要获取不同值的字段,这意味着您需要确保在建立索引时没有对它进行标记,否则聚合中的每个条目都是一个不同的术语,属于字段内容。 如果您仍然希望令牌化并使用聚合,则可

  • 如果发现未定义或为空,我想在数组中按下键 我想要像这样的输出 因为a1和平台的值为null且未定义 我已经尝试过这个解决方案,但它不起作用 但是这只返回['platform'],但是预期的输出应该是['platform','a1'],我认为在运行迭代器(obj[key])时,数组的值(blankValues)变为空,因为它不能保持它,但是请帮助我使用适当的逻辑和结构

  • 问题内容: 我正在尝试通过使用术语过滤器查找来加入2个Elasticsearch索引。我提到了http://www.elasticsearch.org/blog/terms- filter-lookup/ 和http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/query- dsl-terms-filter。 h

  • 在这种情况下,我有几个子查询,通过联合连接,每个子查询都嵌套有一个内部子查询。外部子查询彼此完全相同,而内部查询不同且唯一。 重用整个外部子查询对于读取和进行更改来说是很麻烦的,如果它们可以定义一次并重用,那将是非常有益的。所以这是一个关于创建可重用的SQL查询的问题,但是有不同的内部子查询作为参数传递。 对于我的示例,我将给出一个简化的案例,它与我的实际代码具有相同的问题。 我们正在使用Orac

  • 问题内容: 我们先前用于从集合中查找不同元素的实现过去是: 尝试将其升级到使用mongo 3.3.0+的当前实现是: 还尝试了 在这种情况下,迭代器的目标类型是什么? 问题答案: 您可以尝试这样的事情。

  • 我试图解决一个问题,我必须在搜索中得到明确的结果。 当我对最喜欢的汽车“法拉利”执行术语查询时。我得到两个名为ABC的结果。在这种情况下,我只是希望返回的结果应该是一个。所以我的要求是,如果我可以应用一个不同的on name字段来接收一个1结果。 谢啦