Home > Java > body text

named_object_not_found_Exception when querying ElasticSearch

WBOY
Release: 2024-02-06 09:15:12
forward
466 people have browsed it
Question content

I am trying to query elasticsearch using hibernatesearh 6. Below is the json query sent to elasticsearch. It looks fine based on the documentation here: 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"}}}
Copy after login

However, the following exception message was received:

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
}
Copy after login

The following are entities:

@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;

}
Copy after login

The following is the code to get data from elastic, where predicatefunction is: (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());
Copy after login


Correct answer


hibernate search expects you to pass the query itself without a wrapper json object. See examples here. So in your case you should pass:

{
   "query_string":{
      "fields":[
         "addresses.address_key"
      ],
      "query":"3g5g36ee-45b0-4636-79fe-9aaf446b7ab6"
   }
}
Copy after login

The above is the detailed content of named_object_not_found_Exception when querying ElasticSearch. For more information, please follow other related articles on the PHP Chinese website!

source:stackoverflow.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!