elasticsearch—对于ElasticSearch索引,如何获取数组字段长度大于0的文档?

wsxa1bj1  于 2021-06-15  发布在  ElasticSearch
关注(0)|答案(1)|浏览(1165)

在ElasticSearch索引中,如何得到数组字段长度大于0的文档?
我试着遵循多种语法,但没有任何突破。我在所有的语法中都犯了同样的错误。
获取http://{host}}:{{elasticsearchport}}/student\u details/\u search
语法1:

{
  "query": {
    "bool": {
      "filter": {
        "script": {
          "script": {
            "source": "doc['enrolledCourses'].values.length > 0",
            "lang": "painless"
          }
        }
      }
    }
  }
}

错误:

"caused_by": {
  "type": "illegal_argument_exception",
  "reason": "No field found for [enrolledCourses] in mapping with types []"
}

语法2:

{
  "query": {
    "bool": {
      "filter": {
        "script": {
          "script": {
            "source": "doc['enrolledCourses'].values.size() > 0",
            "lang": "painless"
          }
        }
      }
    }
  }
}

错误:

"caused_by": {
  "type": "illegal_argument_exception",
  "reason": "No field found for [enrolledCourses] in mapping with types []"
}

语法3:

{
  "query": {
    "bool": {
      "filter" : {
        "script" : {
          "script" : "doc['enrolledCourses'].values.size() > 0"
         }
      }
    }
  }
}

错误:

"caused_by": {
  "type": "illegal_argument_exception",
  "reason": "No field found for [enrolledCourses] in mapping with types []"
}

语法4:

{
  "query": {
    "bool": {
      "filter" : {
        "script" : {
          "script" : "doc['enrolledCourses'].values.length > 0"
         }
      }
    }
  }
}

错误:

"caused_by": {
  "type": "illegal_argument_exception",
  "reason": "No field found for [enrolledCourses] in mapping with types []"
}

请帮我解决这个问题。

1mrurvl1

1mrurvl11#

我不知道你运行的是哪个版本的elastic,那么我所有的测试都是在最新的7.9.0版本的elasticsearch上运行的。
我将使用无痛脚本脚本。
我把文件放到索引测试中:

PUT test/_doc/1
{
   "name": "Vasia",
   "enrolledCourses" : ["test1", "test2"]
}

PUT test/_doc/2
{
   "name": "Petya"
}

如何查看一个文档包含 enrolledCourses 第二个不是。
在painless中,您不需要使用值字段,您可以直接获取长度,这是根据painless文档确定的。那我就不用了 values 脚本中的运算符:

GET test/_search
{
   "query": {
      "bool": {
        "filter": [
          {
            "script": {
              "script": {
                "source": "doc['enrolledCourses'].length > 0",
                "lang": "painless"
              }
            }
          }
        ]
      }
   }
}

运行之后,我收到了两个不同的错误:

{
          "type" : "script_exception",
          "reason" : "runtime error",
          "script_stack" : [
            "org.elasticsearch.index.mapper.TextFieldMapper$TextFieldType.fielddataBuilder(TextFieldMapper.java:757)",
            "org.elasticsearch.index.fielddata.IndexFieldDataService.getForField(IndexFieldDataService.java:116)",
            "org.elasticsearch.index.query.QueryShardContext.lambda$lookup$0(QueryShardContext.java:331)",
            "org.elasticsearch.search.lookup.LeafDocLookup$1.run(LeafDocLookup.java:97)",
            "org.elasticsearch.search.lookup.LeafDocLookup$1.run(LeafDocLookup.java:94)",
            "java.base/java.security.AccessController.doPrivileged(AccessController.java:312)",
            "org.elasticsearch.search.lookup.LeafDocLookup.get(LeafDocLookup.java:94)",
            "org.elasticsearch.search.lookup.LeafDocLookup.get(LeafDocLookup.java:41)",
            "doc['enrolledCourses'].length > 0",
            "    ^---- HERE"
          ]
}

and
{
            "type" : "illegal_argument_exception",
            "reason" : "Text fields are not optimised for operations that require per-document field data like aggregations and sorting, so these operations are disabled by default. Please use a keyword field instead. Alternatively, set fielddata=true on [enrolledCourses] in order to load field data by uninverting the inverted index. Note that this can use significant memory."
}

这两个错误都很明显。第一个是字段不存在的文档,第二个是elasticsearch索引字符串数组字段作为默认Map类型 text .
这两种情况都很容易通过Map修复 enrolledCourses 字段组件 keyword . 在第一种情况下,Map将始终提供空字段,在第二个关键字word中,Map将允许运行fielddata属性。

PUT test 
{
   "settings": {
     "number_of_replicas": 0
   },
   "mappings": {
      "properties": {
         "name": {
           "type": "keyword"
         },
         "enrolledCourses": {
            "type": "keyword"
         }
      }
   }
}

现在我将得到正确的答案:

{
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 0.0,
    "hits" : [
      {
        "_index" : "test",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 0.0,
        "_source" : {
          "name" : "Vasia",
          "enrolledCourses" : [
            "test1",
            "test2"
          ]
        }
      }
    ]
  }
}

相关问题