Java API to implement query for script_fields in Elastic search - java

I have tried to find the answer of this question but couldn't find. The scenario is: I have to get one extra field which is the calculation of two fileds in the elastic search query result. For this purpose I am using script_fields.
"script_fields": {
"result": {
"script": "doc['feild1'].value / doc['field2'].value"
}
}
The query is working fine, and I am getting the correct result.
But now I have to implement the query in Java, I couldn't find any way to implement script_fields in Java.
Can somebody please help ?

Very simply using the addScriptField() method like this:
SearchResponse response = client().prepareSearch()
.setQuery(matchAllQuery())
.addScriptField("result", new Script(ScriptType.INLINE, "groovy", "doc['field1'].value / doc['field2'].value", Collections.emptyMap()))

Related

How to use include query in elastic search?

I use own analzyer to asciifolding and lowercase. How Can I send query about this:
{
"query": {
"query":"*mil*"
}
When I didn't use own analzyer I could do, thanks QuerStringBuilder. Now it's not working.
I tried to find a Polish stammer but it's not there. Is there any other way out? I need to have my own analyzer to map accents.
Ok I solved problem. I changed .escape(true) to .escape(false) in my QuerStringBuilder.

How to combine JSON values in the response using Java

I am currently working on a school project. We have a series of response templates in JSON format that will take values from the request and then return it accordingly in the response when run in postman.
e.g
Request:
{
"Application_id":123456
}
Response:
{ "Application_id: 123456, TIMESTAMP: 20220501}
I am able to get these values in the response but the issue I am running accross now is figuring out how to combine 2 values in the request into one like so:
Request:
{
"Application_id":123456
"user_id_first_six": 456789
"user_id_last_four": 1234
}
Expected Response:
{ "Application_id: 123456, TIMESTAMP: 20220501, combined_id:456789****1234}
what I have tried is to put combined_id : "user_id_first_six"+******+"user_id_last_four" but this doesnt work.
Apologies if I cant be more specific as there are portions that I have left out due to confidentiality issues.
The easiest way to achieve this in Java would be to use JSONObject. In your Request-Handler, add two parameters of Type JSONObject and then merge them:
jsonObj.putAll(jsonObj1)
Thanks all for the guidance. I basically did what Knu8 suggested and extracted the values using Matcher+Regex (<(.*)>)(\W*)(<(.*)>) and converted them to strings and then used StringBuilder to append all the components together.

Redisearch query with "begin with" instead of "contains"

I am trying to understand on how to perform queries in Redisearch strictly with "begins with" and I keep getting "contains".
For example if I have fields with values like 'football', 'myfootball', 'greenfootball' and would provide a search term like this:
> FT.SEARCH myIdx #myfield:foot*
I want just to get 'football' but I keep getting other fields that contain the word instead of beginning with that word.
Is there a way to avoid this?
I was trying to use VERBATIM and things like #myfield:^foot* but nothing.
I am using JRedisearch as a client but eventually I had to enter the DB and perform these queries manually in order to figure out what's happening. That being said, is this possible to do with this client at the moment?
Thanks
EDIT
A sample of my index setup:
Client client = new Client(INDEX_NAME, url, PORT);
Schema sc = new Schema().addSortableTextField("url", 1.0); // using this field for query
client.dropIndex(true);
client.createIndex(sc, Client.IndexOptions.Default());
return client;
Sample document:
id: // random uuid
urlPath: myfootbal
application: web
market: Europe
After checking the RDB provided I see that when searching foot* you are not getting myfootbal. The replies look like this: /dot-com/plp/football/x/index.html. You are getting those replies because this url is tokenized, and '/' is one of the tokenize chars. If you do not want those urls to be tokenized you need to declare them as TAGS and not as TEXT. This way the entire url will be indexed as is and when search for foot* it will not appear in the results.
For more information about TAGS see the FT.CREATE documentation: https://oss.redislabs.com/redisearch/Commands.html

Rest Assured - comparing the json path with another

I have json for example below
{"TestJson":{
"Result":"Passed",
"description":"Passed."},
"Students":[{
"Class":{
"Primary":"Yes"
},
"Course":{
"Enrolled":"yes",
"AccountNumber":"2387287382"
},
"AccountNumber":"2387287382",
"Paid":"Yes"
}]}
I am wondering how can I find a good solution for this.
What I currently do
.body("Students[0].Course.AccountNumber",equalTo("2387287382"))
.body("Students[0].AccountNumber",equalTo("2387287382"))
My test criteria is to check key Students[0].AccountNumber matches Students[0].Course.AccountNumber
I want to do in this way, but i am not able to find a solution something like
.body("Students[0].Course.AccountNumber",equalTo("Students[0].AccountNumber"))
The above wont work obviously, but that is how I want to compare. basically comparing the key with another key and they should match.
Is this doable?
One way to do it is:
String A =
given ().
when().
get/put/post({api/name})
.extract()
.path("Students[0].Course.AccountNumber");
String B =
given ().
when().
get/put/post({api/name})
.extract()
.path("Students[0].AccountNumber");
Assert.assertEquals(A, B);
Seems like this workaround is the only way to go.
See the Use the response to verify other parts of the response section of the rest-assured docs. You basically want to create a lambda implementing ResponseAwareMatcher<Response>. Something like this:
get("/x").then().body("href", response -> equalTo("http://localhost:8080/" + response.path("userId"));

Polarion ALM SDK API getWorkItem()

I have a question about the Polarion SDK API
I am in a Project -> Documents and Pages -> Testing.
From there I created a new wiki article.
In the editing of the article I did the following:
1) created a sql query using: #set ($sql_result = $sqlService.executeQuery("<sql query here>")
The sql query produces an array that looks like this:
[[project.c_id, test_cases.c_id , test_cases count ], [project.c_id, test_cases.c_id , test_cases count ], [project.c_id, test_cases.c_id , test_cases count ], [project.c_id, test_cases.c_id , test_cases count ]]
2) Now I would like to iterate through this multi-dimensional array by doing this:
#foreach($item in $sql_result)
$getWorkItem($item.get(0), $item.get(1)) <br>
#end
Unfortunately, Polarion does not appear to recognize the $getWorkItem(). I am pretty newbie at this, I'm sure I'm just calling the function incorrectly. I found the API Doc here: https://almdemo.polarion.com/polarion/sdk/doc/javadoc/index.html
It is in TrackerService.getWorkItem()
But I still don't know how to call the API correctly. Any help will be greatly appreciated.
You need to reference the instance of TrackerService which is in the Velocity context to be able to call getWorkItem(), something like:
$trackerService.getWorkItem($item.get(0), $item.get(1))
The main point is that you cannot call a method without a base object in Velocity.

Categories

Resources