首頁 > Java > 主體

查詢ElasticSearch時出現named_object_not_found_Exception

WBOY
發布: 2024-02-06 09:15:12
轉載
466 人瀏覽過
問題內容

我正在嘗試使用 hibernatesearh 6 查詢 elasticsearch。以下是傳送到 elasticsearch 的 json 查詢。根據此處的文檔,它看起來很好:https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl.html

{"query":{"query_string":{"fields":["addresses.address_key"],"query":"3g5g36ee-45b0-4636-79fe-9aaf446b7ab6"}}}
登入後複製

但是,收到以下例外訊息:

org.hibernate.search.util.common.searchexception: hsearch400007: elasticsearch request failed: hsearch400090: elasticsearch response indicates a failure.
request: post /employee-read/_search with parameters {from=0, size=10, track_total_hits=true}
response: 400 'bad request' from 'http://localhost:9200' with body 
{
  "error": {
    "root_cause": [
      {
        "type": "parsing_exception",
        "reason": "unknown query [query]",
        "line": 1,
        "col": 19
      }
    ],
    "type": "parsing_exception",
    "reason": "unknown query [query]",
    "line": 1,
    "col": 19,
    "caused_by": {
      "type": "named_object_not_found_exception",
      "reason": "[1:19] unknown field [query]"
    }
  },
  "status": 400
}
登入後複製

以下是實體:

@indexed(index = "employee")
public class employee {

  @fulltextfield(name = "employee_key")
  private string employeekey;

  @fulltextfield(name = "first_name")
  private string firstname;
    
  @indexedembedded(includeembeddedobjectid = true, includedepth = 2)
  private address addresses;
}

public class address {
    
  @fulltextfield(name = "address_key")
  private string addresskey;

  @fulltextfield(name = "street_name")
  private string streetname;

}
登入後複製

以下是從 elastic 取得資料的程式碼,其中 predicatefunction 為: (elasticsearchsearchpredicatefactory f) -> f.fromjson(queryjson)

SearchSession searchSession = Search.session(entityManager);

            SearchResult<Employee> searchResult = searchSession.search(Employee.class)
                    .extension(ElasticsearchExtension.get())
                    .where(searchPredicateFactory -> {
                        return predicateFunction.apply(searchPredicateFactory);
                    })
                    .fetch(Math.toIntExact(page.getOffset()), page.getPageSize());
登入後複製


正確答案


hibernate search 希望您傳遞查詢本身,而無需包裝器 json 物件。請參閱範例 此處。因此,在您的情況下,您應該傳遞:

{
   "query_string":{
      "fields":[
         "addresses.address_key"
      ],
      "query":"3g5g36ee-45b0-4636-79fe-9aaf446b7ab6"
   }
}
登入後複製

以上是查詢ElasticSearch時出現named_object_not_found_Exception的詳細內容。更多資訊請關注PHP中文網其他相關文章!

來源:stackoverflow.com
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!