how to search by custom JSON use Elasticsearch High Level Rest Client - java

ES HighLevelRestClient
how to use ES HighLevelRestClient search by a custom JSON.
I have a query JSON like:
{
"from": 0,
"size": 20,
"query": {
"bool": {
"must": [
{
"match": {
"name": "test"
}
}
]
}
}
}
this JSON generated by mustache template mustache java . cause I don't want to use QueryBuilder.
so I want to know how to search by custom JSON use HightLevelClient?

Related

How to write custom Elasticsearch Query in Java?

This is my query on Elasticsearch,
GET index101/_search
{
"query": {
"function_score": {
"boost_mode": "replace",
"query": {
"constant_score": {
"filter": {
"terms": {
"fields": ["767","434", "101", "222"]
}
}
}
},
"pqcode_score": {
"descriptors": [
{
"descriptor": "base64string"
}
],
"pqparams": {
"bucket_field": "fields",
"pqcode_field": "fields2",
"distance_function": "similarity",
"model": "random"
}
}
}
}
}
Looked into the documentation of Elasticsearch with Java, but couldn't find anything that could resolve this query in Java.
I created a JSON file, and got the input query in the jsonObject and then passed it as a parameter to searchSourceBuilder.query().
But it gives the error that the jsonObject can't be converted into QueryBuilder.
How can we go ahead with this query in Java?
Is there any other workaround for this?
looks like pqcode_score is your custom key in the Elasticsearch query, and you are trying to add the custom component/constructs in your Elasticsearch query thats not possible, hence you are getting the error.
You need to use the constructs in your Elasticsearch query thats supported by Elasticsearch.

Read hateoas response with embedded HAL collection with spring

I'm trying to read a REST HATEOAS response like this one :
{
"_embedded": {
"tasks": [
{
"id": 1,
"name":"task1"
"_links": {
"self": {
"href": "http://localhost:8080/v1/tasks/1"
},
"tasks": {
"href": "http://localhost:8080/v1/tasks"
}
}
}
]
},
"_links": {
"self": {
"href": "http://localhost:8080/v1/tasks?page=0&size=1"
}
},
"page": {
"size": 1,
"totalElements": 1,
"totalPages": 1,
"number": 0
}
}
I'm trying to folloaw the example of spring's documentation : spring hateoas traverson.
Java code :
...
TypeReferences.ResourcesType<Resources<Task>> resourceParameterizedTypeReference = new TypeReferences.ResourcesType<Resources<Task>>(){};
Traverson traverson = new Traverson(new URI("http://localhost:8080/v1/tasks"), MediaTypes.HAL_JSON);
Traverson.TraversalBuilder builder = traverson.follow(rel("tasks")).withHeaders(headers);
Resources<Resources<Task>> taskResources = builder.toObject(resourceParameterizedTypeReference);
...
But I obtain this error :
Did not find LinkDiscoverer supporting media type text/html!
Cause : "follow(rel("tasks"))" don't find "tasks".
I'm trying other solutions like this one :
Deserialize JSON containing (_links and _embedded) using spring-hateoas, I obtain the error "Expected to find link with rel ..." too.
Maybe finaly I'm not understand the good manner to use traverson object.
I solve my problem by parsing myself the response in Json, but are they another way to get the list contains in the "_embedded" tags in a list of beans ?
If you have some examples I'm interesting :).
I think you need to use a Json Path expression to access the embedded content.
Traverson.TraversalBuilder builder = traverson.follow("$._embedded.tasks[0]").withHeaders(headers);
Resources<Resources<Task>> taskResources = builder.toObject(resourceParameterizedTypeReference);
The following tool is useful for playing with the path expression: http://www.jsonquerytool.com/
BTW, your JSON is missing a "," after the "name".

Elastic search exact match query issue

I am having a problem while querying elastic search. The below is my query
GET _search {
"query": {
"bool": {
"must": [{
"match": {
"name": "SomeName"
}
},
{
"match": {
"type": "SomeType"
}
},
{
"match": {
"productId": "ff134be8-10fc-4461-b620-79s51199c7qb"
}
},
{
"range": {
"request_date": {
"from": "2018-08-22T12:16:37,392",
"to": "2018-08-28T12:17:41,137",
"format": "YYYY-MM-dd'T'HH:mm:ss,SSS"
}
}
}
]
}
}
}
I am using three match queries and a range query in the bool query. My intention is getting docs with these exact matches and with in this date range. Here , if i change name and type value, i wont get the results. But for productId , if i put just ff134be8, i would get results. Anyone knows why is that ? . The exact match works on name and type but not for productId
You need to set the mapping of your productId to keyword to avoid the tokenization. With the standard tokenizer "ff134be8-10fc-4461-b620-79s51199c7qb" will create ["ff134be8", "10fc", "4461", "b620", "79s51199c7qb"] as tokens.
You have different options :
1/ use a term query to check without analyzing the content of the field
...
{
"term": {
"productId": "ff134be8-10fc-4461-b620-79s51199c7qb"
}
},
...
2/ if you are in Elasticsearch 6.X you could change your request to
...
{
"match": {
"productId.keyword": "ff134be8-10fc-4461-b620-79s51199c7qb"
}
},
...
As elasticsearch will create a subfield keyword with the type keyword for all string field
The best option is, of course, the first one. Always use term query if you are trying to match the exact content.

Not able to build query for Elasticsearch using java api

Am trying to query elasticsearch via java api for the below elasticsearch query
get my_index12/_search {
"query" : {
"bool": {
"must": [
{
"match": {
"code": {
"query": "TE-7000-8002-W",
"operator": "and"
}
}
},
{
"match": {
"locale": {
"query": "en_US",
"operator": "and"
}
}
}
]
}
}
}
The above query is working fine is Kibana. Am trying to replicate the same query using Elasticsearch Java API.
Please find my JAVA API query that am trying to build.
QueryBuilder qb = QueryBuilders.boolQuery().must(QueryBuilders.matchQuery("code",name)).operator(Operator.AND);
Am getting the below error from eclipse.
The method operator(Operator) is undefined for the type BoolQueryBuilder
Am using Elasticsearch 6.2.3 version
In java transport client, search request can be build both in QueryBuilders and XContent format which build query param in json format.
With QueryBuilder, especially BoolQuery, the operator in query dsl is represented by either must() (means and operator) or should() (means or operator).
As described, your query can be represented like:
QueryBuilders.boolQuery().must().must().build();
One more interesting question is how to present the following query DSL:
{
"query": {
"bool": {
"must": [{
"term": {
"field1": "value"
}
}, {
"match": {
"filed2": "value"
}
}],
"should": [{
"term": {
"field3": "value"
}
},{
"term": {
"field4": "value"
}]
}
}
}
once you can understand the demo query DSL and successfully implement it with java API, means you are getting to know Java Transport client API.
hope can offer you some help

Querying ElasticSearch using a JSON file through JAVA API

I have a query in valid JSON format which works well in kibana or Sense when I use GET request.I am also able to create this query using XContentBuilder, but I need to send this query using its JSON form as it is to ElasticSearch. Is it possible to store the query in a JSON file and query ElasticSearch using this JSON file.
My query -
{
"min_score":5,
"sort" : [
{
"_geo_distance" : {
"location" : [40.715, -73.988],
"order" : "asc",
"unit" : "km",
"mode" : "min",
"distance_type" : "arc"
}
}
],
"query": {
"bool": {
"must": {
"query_string": {
"query": "hospital",
"analyzer": "english"
}
},
"filter": {
"geo_distance": {
"distance": "50000km",
"location": {
"lat": 40.715,
"lon": -73.988
}
}
}
}
}
}
What I want is to store this query in a JSON file and use this JSON file to send a search request directly without using Query builder.
You can use a search template, and store this template in the cluster state, see the official documentation about search templates, especially about pre-registered templates.

Categories

Resources