I am using spring data jpa for creating micro services. In repository I am using JPQL query. Using following code I am able to get set of data. But I want to iterate set of data for further logic.
For iteration I used for each but when I am using for each loop I am getting error "java.util.HashMap cannot be cast to com.spacestudy.model.RoomDepartmentMapping",
public Set<RoomDepartmentMapping> loadStatusOfRooms() {
Set<RoomDepartmentMapping> roomDeptMapping = roomDeptMappingRepo.findStaus();
return roomDeptMapping;
}
}
Repository
#Repository
public interface RoomDepartmentMappingRepository extends JpaRepository<RoomDepartmentMapping, Integer> {
#Query("select new map(roomDeptMap.sStatus as sStatus) from RoomDepartmentMapping as roomDeptMap")
Set<RoomDepartmentMapping> findStaus();
}
Result
[
{
"sStatus": "A"
},
{
"sStatus": "I"
},
{
"sStatus": "R"
}
]
Expecting Result
[
{
"sStatus": "Accepted"
},
{
"sStatus": "In Progress"
},
{
"sStatus": "Remaining"
}
]
For getting above expected result I am trying to iterate Set of data using for each and planning to use switch case. But I am not able to iterate using following loop.
For each loop
for(RoomDepartmentMapping roomDeptMappingObj:roomDeptMapping) {
System.out.println(roomDeptMappingObj);
}
Can any one tell me why I am not able to iterate set using for each loop?
Or please suggest any another way to do that.
Based on the name of your method I will assume that you need all available status values.
The query will be:
#Query("select distinct rdm.sStatus from RoomDepartmentMapping rdm")
Set<String> findStatus();
distinct => you need set
Set => RoomDepartmentMapping sStatus is a String
Related
I'm working on a project creating integration tests and the data I have includes all the schema updates people have done in the past. For example My "documents" field has a property of user, which has been an array in the past, but is now an Object.
I'm trying to write an integration test that will ONLY use a document if the user type is Object, not array.
Is there a way to do this with an ExistsQuery? So far I have not been able to find anything. Here's my current query which brings back inconsistent data:
GET _search
{
"query" : {
"bool" : {
"must" : {
"exists" : {
"document.user"
}
}
}
}
}
Ideally I'd like to check for document.user[] and filter those values out.
You can use script query like below
{
"query": {
"script": {
"script": "if (doc['document.keyword'].size()==1) return true;"
}
}
}
I have a json response something like this:
"someArray": [
{
"someProperty":"someValue",
// other properties that are not relevant for me
},
{
"someProperty":"someOtherValue",
// other properties that are not relevant for me
}
]
I want to check, if the someArray array has an object with property named "someProperty" with the value "someValue", but don't fail the test if it has another object with the same property but not the same value.
Is it possible? Until this I was using static index because I had only one element in that array.
Here's a solution using JsonPath:
List<String> values = RestAssured.when().get("/api")
.then().extract().jsonPath()
.getList("someArray.someProperty");
Assert.assertTrue(values.contains("someValue"));
Will work for following response JSON:
{
"someArray":[
{
"someProperty":"someValue"
},
{
"someProperty":"someOtherValue"
}
]
}
Assuming you're using Java 8 or above, you should use Arrays.stream(someArray) and then use the filter method to select elements you desire.
I haven't used REST-assured but based on their documentation, it looks like you should be able to use something like this below
#Test public void
lotto_resource_returns_200_with_expected_id_and_winners() {
when().
get("/lotto/{id}", 5).
then().
statusCode(200).
body("someArray", hasItems(hasEntry("someProperty", "someValue")));
}
This works if you can put some kind of deserialization logic to convert object to map before using hasEntry
Another solution is to use findAll
body("someArray.findAll {o -> o.someProperty == 'someValue'}.size()", greaterThan(0))
{
"Account1" :
{
Push_key(): { Carplate: "ABC1234" }
Push_key(): { Carplate: "ABC" }
Push_key(): { Carplate: "A" }
}
}
This is how the database looks like.
I would like to retrieve the third data which contains "A" alone ONLY.
I am using startAt() and endAt() for data retrieval:
Query query = ref.child("Account1").orderByChild("Carplate").startAt("A").endAt("A"+"\uf8ff");
But it returns all 3 records. (I think its due to they are all started at "A".)
Need help! Please!
You should look at the equalTo() method for this (from the doc):
The equalTo() method allows you to filter based on exact matches. As is the case with the other range queries, it will fire for each matching child node.
To adapt it to your query you might try:
Query query = ref.child("Account1").orderByChild("Carplate").equalTo("A");
We have a multivalued field in Solr that we want to reduce its length.
A sample result response is as follows:
response": {
"numFound": 1,
"start": 0,
"docs": [
{
"created_date": "2016-11-23T13:47:46.55Z",
"solr_index_date": "2016-12-01T08:21:59.78Z",
"modified_date": "2016-12-13T08:45:44.507Z",
"id": "FEAE38C2-ABFF-4F0C-8AFD-9B8F51036D8A",
"Field1": [
"false",
"true",
"true",
..... <= 1200 items
]
}
]
}
We have big data, a couple of TB and we are looking for an optimized way to alter all documents within Solr and to modify Field1 to contain only the first 100 items.
Can something like this be done without the need to write a script to manually fetch the document, make adjustments and push it back to solr? Has anyone had a similar experience? Thanks
We have faced this problem. But we use Two collections to solve this problem. Use SoleEntityProcessor to move the document from one collection to another.
[SolrEntityProcessor]
<dataConfig>
<document>
<entity name="sep" processor="SolrEntityProcessor" url="http://localhost:8983/solr/db" query="*:*"/>
</document>
</dataConfig>
While moving pass that document through updateRequestProcessorChain where we can write StatelessScriptUpdateProcessorFactory to edit our documents or to truncate the multivalued field.
In StatelessScriptUpdateProcessorFactory you can get the field and apply your operations and then reset that field.
[StatelessScriptUpdateProcessorFactory]
function processAdd(cmd) {
doc = cmd.solrDoc;
multiDate = doc.getFieldValue("multiValueField");
//Apply your operation to above field
//doc.setField("multiValueField",value);
}
function processDelete(cmd) {
// no-op
}
function processMergeIndexes(cmd) {
// no-op
}
function processCommit(cmd) {
// no-op
}
function processRollback(cmd) {
// no-op
}
function finish() {
// no-op
}
For More information on StatelessScriptUpdateProcessorFactory, you can refer to this question
On solr how can i copy selected values only from multi valued field to another multi valued field?
in which they edit the multivalued field using the script.
Here is what I would like to do. I have an index with several documents in Elasticsearch. In every document I have two field: deviceField (name of the device) and pressionField (the value of the pression periodically). I want to query in my index the average pression per device. Do you know a way to do it in a single query? Indeed, I do not want to do a kind of loop 'for' in order to the query per deviceName. It will take too much time due to the fact that I have millions of devices.
Thank you for your attention and your help.
S
You have to use metric aggregations. If you want to list all at same query you use subaggregation
{
"aggs" : {
"devices" : {
"terms" : { "field" : "deviceField" },
"aggs" : {
"avg_pression" : { "avg" : { "field" : "pressionField" } }
}
}
}
}
Here is the link of documentation:
https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-avg-aggregation.html