We should improve the SearchResult and Document class more developer friendly, it could simply be a different serialization of the document.
Using proper name/value object/map, instead of a list of k/v.
Note: this is just capture as a possible improvement, more thinking is needed, but the idea would be to reduce the code when processing the result.
For example a SearchResult should be represented by a JSON class like this one:
{
"totalResults":2,
"docs":[
{
"meta":{
"score":1,
"id":"movie:1141"
},
"body":{
"ibmdb_id":"tt0113277",
"plot":"A group of professional ... heist.",
"genre":"Drama",
"release_year":"1995",
"rating":"8.2",
"votes":"560687",
"title":"Heat",
"poster":"https://m..._.jpg"
}
},
{
"meta":{
"score":1,
"id":"movie:818"
},
"body":{
"ibmdb_id":"tt0112617",
"plot":"A lifeguard bets he can be true to just one woman.",
"genre":"Comedy",
"release_year":"1995",
"rating":"5.4",
"votes":"32",
"title":"California Heat",
"poster":"N/A"
}
}
]
}
Instead of:
{
"totalResults":2,
"docs":[
{
"id":"movie:1141",
"score":1,
"payload":null,
"properties":[
{
"ibmdb_id":"tt0113277"
},
{
"plot":"A group of professional...heist."
},
{
"genre":"Drama"
},
{
"release_year":"1995"
},
{
"rating":"8.2"
},
{
"votes":"560687"
},
{
"title":"Heat"
},
{
"poster":"https://m.med..._.jpg"
}
]
},
{
"id":"movie:818",
"score":1,
"payload":null,
"properties":[
{
"ibmdb_id":"tt0112617"
},
{
"plot":"A lifeguard bets he can be true to just one woman."
},
{
"genre":"Comedy"
},
{
"release_year":"1995"
},
{
"rating":"5.4"
},
{
"votes":"32"
},
{
"title":"California Heat"
},
{
"poster":"N/A"
}
]
}
]
}
Possible solution:
List<Map<String, Object>> docsToReturn = new ArrayList<>();
List<Document> docs = queryResult.docs;
for (Document doc :docs) {
Map<String,Object> props = new HashMap<>();
Map<String,Object> meta = new HashMap<>();
meta.put("id", doc.getId());
meta.put("score", doc.getScore());
doc.getProperties().forEach( e -> {
props.put( e.getKey(), e.getValue() );
});
Map<String,Object> docMeta = new HashMap<>();
docMeta.put("meta",meta);
docMeta.put("body",props);
docsToReturn.add(docMeta);
}
We should improve the
SearchResultandDocumentclass more developer friendly, it could simply be a different serialization of the document.Using proper name/value object/map, instead of a list of k/v.
Note: this is just capture as a possible improvement, more thinking is needed, but the idea would be to reduce the code when processing the result.
For example a SearchResult should be represented by a JSON class like this one:
{ "totalResults":2, "docs":[ { "meta":{ "score":1, "id":"movie:1141" }, "body":{ "ibmdb_id":"tt0113277", "plot":"A group of professional ... heist.", "genre":"Drama", "release_year":"1995", "rating":"8.2", "votes":"560687", "title":"Heat", "poster":"https://m..._.jpg" } }, { "meta":{ "score":1, "id":"movie:818" }, "body":{ "ibmdb_id":"tt0112617", "plot":"A lifeguard bets he can be true to just one woman.", "genre":"Comedy", "release_year":"1995", "rating":"5.4", "votes":"32", "title":"California Heat", "poster":"N/A" } } ] }Instead of:
{ "totalResults":2, "docs":[ { "id":"movie:1141", "score":1, "payload":null, "properties":[ { "ibmdb_id":"tt0113277" }, { "plot":"A group of professional...heist." }, { "genre":"Drama" }, { "release_year":"1995" }, { "rating":"8.2" }, { "votes":"560687" }, { "title":"Heat" }, { "poster":"https://m.med..._.jpg" } ] }, { "id":"movie:818", "score":1, "payload":null, "properties":[ { "ibmdb_id":"tt0112617" }, { "plot":"A lifeguard bets he can be true to just one woman." }, { "genre":"Comedy" }, { "release_year":"1995" }, { "rating":"5.4" }, { "votes":"32" }, { "title":"California Heat" }, { "poster":"N/A" } ] } ] }Possible solution: