【问题标题】:Elasticsearch find match document base on number of propertiesElasticsearch 根据属性数量查找匹配文档
【发布时间】:2021-08-24 07:41:42
【问题描述】:

我在弹性搜索中有一些文档

doc1 -> name: name1  
doc2 -> name: name1, otherprop: prop1  
doc3 -> name: name1, otherprop: prop1, otherprop2: prop2  

我的目标是按名称获取 doc1,但查询返回所有三个文档
我试图按名称和最小值(属性计数)获取 doc1,但我的查询是返回或什么都没有或所有匹配文档

{
  "query": {
    "bool": {
      "filter": [
        { "term": { "name": "name1"   }}
      ]
    }
  },
  "aggs": {
    "models": {
      "terms": { "field": "name" } 
    }
  }
}

我的想法如何仅获得 doc1?
谢谢

【问题讨论】:

    标签: elasticsearch querydsl


    【解决方案1】:

    要实现这样的功能,您可以在索引期间添加字段数并在查询期间检查字段数,以更新所有文档,您可以通过查询使用此更新:

    查询更新

    POST [index name]/_update_by_query
    {
        "query": {
            "match_all": {}
        }, 
        "script": {
           "source": "ctx._source.fieldsCount = ctx._source.size()"
        }
    }
    

    然后您可以使用 FieldsCount 字段来编写您的查询。

    【讨论】:

    • 嗨@Kaveh 但其他道具是不可预测的,它们可以是 2 个或 3 个或更多字段
    • 如果您不命名字段,最好添加一个字段以包含文档中的字段数并使用该字段进行查询,另一个选项可以在查询期间使用运行时字段可以在这里查看:elastic.co/guide/en/elasticsearch/reference/7.13/….
    • 我更新了我的答案,包括在索引期间添加字段数量的解决方案。
    猜你喜欢
    • 2015-01-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-19
    • 2015-08-08
    相关资源
    最近更新 更多