RESTful MVC Template for Search and Search Results

So, I’m sure that this must have been asked before, but I can’t find anything. The fact is that when I program search functions for web applications, it never seems to me completely correct.

I use Ruby on Rails, but I think this is a question that applies to any situation in which you use the RESTful MVC template.

Let's say you have a Resource (e.g. Users, ToDos, ...) that you want to execute. Once the application grows, it will not be possible with simple LIKE queries, and you will start using the index (for example, Solr, ElasticSearch, Lucene, ...). An indexed resource also tends to be composite data from the resource and its associated objects (user location, creator of ToDos, ...).

How is this best represented?

  • Is this a GET for / resources ( Resource #index)? This is a sample list of the main resource, but again, this is actually this complicated thing, and if the search functionality is extensive, it really tends to inflate the model code.
  • Is this a POST to search / search ( Search #create)? We create a search but do not save it. Instead, it turns into a set of search queries.
  • So is this a GET for SearchResult ( SearchResult #show)? But it does not have an identifier. I suppose SearchIndex is a kind of database for this model, but you would not create SearchResult, right? This is more like a search #, which ends with a SearchResult # showing, but it also seems uncomfortable to me.
+4
source share
1 answer

POST , GET, - , , (),...

RESTful REST- GET query path, . HTTP 1.1. GET, , , .

LIKE , . ElasticSearch . , . , , , . , .

GET /users/12345, , :

{
    "id": "12345",
    "firstName": "Max",
    "lastName": "Test",
    "_schema": {
        "href": "http://example.com/schema/user"
    }
    "_links": {
        "self": {
            "href": "/users/12345",
            "methods": ["get", "put", "delete"]
        },
        "curies": [{ 
            "name": "usr", 
            "href": "http://example.com/docs/rels/{rel}", 
            "templated": true
        }],
        "usr:employee": {
            "href": "/companies/112233",
            "title": "Sample Company",
            "type": "application/hal+json"
        }
    },
    "_embedded": {
        "usr:address": [
            {
                "_schema": {
                    "href": "http://example.com/schema/address"
                },
                "street" : "Sample Street",
                "zip": "...",
                "city": "...",
                "state": "...",
                "location": {
                    "longitude": "...",
                    "latitude": "..."
                }
                "_links": {
                    "self": {
                        "href": "/users/12345/address/1",
                        "_methods": ["get", "post", "put", "delete"],
                    }
                }
            }
        ],
        "usr:search": {
            "_schema": {
                "href": "http://example.com/schema/user_search"
            }
            "_links": {
                "self": {
                    "href": "/users/12345/search",
                    "methods: ["post", "delete"]
                }
            },
            "filters": [
                "_schema": {
                    "href": "http://example.com/schema/user_search_filter"
                },
                "_links": {
                    "self": {
                        "href": "/users/12345/search/filters",
                        "methods: ["get"]
                    },
                    "next": {
                        "href": "/users/12345/search/filters?page=2"
                        "methods: ["get"]
                    }
                },
                {
                    "byName": {
                        "query": {
                            "constant_score": {
                                "filter": {
                                    "term": {
                                        "name": {
                                            "href": "/users/12345#name"
                                        }
                                    }
                                }
                            }
                        }
                        "_links": {
                            "self": {
                                "href": "/users/12345/search/filter/byName",
                                "methods": ["get", "put", "delete"],
                                "_schema": {
                                    "href": "http://example.com/schema/search_byName"
                                }
                                "type": "application/hal+json"
                            }
                        }
                    }
                },
                {
                    "in20kmDistance" : {
                       "query": {
                           "filtered" : {
                               "query" : {
                                   "match_all" : {}
                               },
                               "filter" : {
                                   "geo_distance" : {
                                       "distance" : "20km",
                                           "Location" : {
                                               "lat" : {
                                                   "href": "/users/12345/address/location#lat"
                                               },
                                               "lon" : {
                                                   "href": "/users/12345/address/location#lon"
                                               }
                                           }
                                       }
                                   }
                               }
                           }
                        }
                        "_links": {
                            "self": {
                                "href": "/users/12345/search/filter/in20kmDistance,
                                "methods": ["get", "put", "delete"],
                                "_schema": {
                                    "href": "http://example.com/schema/search_in20kmDistance"
                                }
                                "type": "application/hal+json"
                            }
                        }
                    }
                },
                {
                    ...
                }
            ]
        }
    }
}

JSON HAL. RESTful , , POST put , .

search , , GET /users/{userId}/search/filters?page=pageNo.

- ElasticSearch 20 , URI, . , ElasticSearch , , . , ElasticSearch, JSON Pointer URI , - .

, GET . , , - .

, . . , , . . , UUID , , URI, byName , de305d54-75b4-431b-adb2-eb6b9e546014, .

+1

All Articles