簡體   English   中英

對於彈性搜索索引,如何獲取數組字段長度大於0的文檔?

[英]For an elastic search index, how to get the documents where array field has length greater than 0?

彈性搜索索引中,如何獲取數組字段長度大於0的文檔?

我嘗試了多種語法,但沒有取得任何突破。 我在所有語法中都遇到了同樣的錯誤。

GET http://{{host}}:{{elasticSearchPort}}/student_details/_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 []"
}   

請幫我解決這個問題。

我不知道你運行的是什么版本的彈性,那么我所有的測試都在最新的 7.9.0 版本 Elasticsearch 上運行。

我將使用無痛腳本來編寫腳本。

我把文件放到索引測試中:

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

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

如何查看一個文檔包含enrolledCourses字段而第二個不包含。

在 painless 中,您不需要使用 values 字段,您可以直接獲取 length,這是根據painless 文檔 然后我跳過在腳本中使用values運算符:

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

運行后我收到了 2 個不同的錯誤:

{
          "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 索引字符串數組字段作為默認映射類型text

通過將enrolledCourses字段映射為keyword ,這兩種情況都很容易解決。 在第一種情況下,映射將始終提供空字段,而在第二個關鍵字中,允許運行 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"
          ]
        }
      }
    ]
  }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM