Skip to content

make SearchResult, Document and AggregationResult classes more developer friendly #121

Description

@tgrall
  • SearchResult
  • Document
  • AggregationResult

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);
        }

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions