Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8783343
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T20:48:31+00:00 2026-06-13T20:48:31+00:00

I’m looking for a robust way to model search queries in a REST api.

  • 0

I’m looking for a robust way to model search queries in a REST api.

In my api, you can specify the search criteria in the URI of a resource using query parameters.

For example:

/cars?search=color,blue;AND;doors,4 --> Returns a list of blue cars with 4 doors

/cars?search=color,blue;OR;doors,4 --> Returns a list of cars that are blue or have 4 doors

On the server side, the search string is mapped to the desired underlying technology. Depending on the rest resource, this can be a SQL query, the Hibernate Criteria api, another webservice call, …

The 2 examples are simple enough to support, but I also need more complex search features like substring search, searching before/after dates, NOT, …

This is a common problem I think. Is there a library (or a pattern) that I can use that:

  • Maps search queries specified as a string to a generic Criteria model. The search format does not have to be the same as I listed above.
  • Allows me to map that Criteria model to any technology I need to use.
  • Offers mapping support for Hibernate/JPA/SQL, but this is a bonus 😉

Kind regards,

Glenn

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T20:48:32+00:00Added an answer on June 13, 2026 at 8:48 pm

    Whenever I face these problems, I ask myself “How would I present this to a user, if I was creating a traditional webpage”? The simple answer is that I wouldn’t present those sort of options in a single page. The interface would be too complex; however what I could do is provide an interface that allowed users to build up more and more complex queries over a number of pages and that’s the solution I think you should go for in this case.

    The HATEOAS constraint specifies that we must include the hypermedia controls (links and forms) in our responses. So let’s say we have a paginated collections of cars at /cars with a search option, so that when you get /cars it returns something like (BTW I’m using a custom media-type here, but the forms and links should be pretty obvious. Let me know if it isn’t):

    <cars href="/cars">
        <car href="/cars/alpha">...</car>
        <car href="/cars/beta">...</car>
        <car href="/cars/gamma">...</car>
        <car href="/cars/delta">...</car>
        ...
        <next href="/cars?page=2"/>
        <search-color href="/cars" method="GET">
            <color type="string" cardinality="required"/>
            <color-match type="enumeration" cardinality="optional" default="substring">
                <option name="exact"/>
                <option name="substring"/>
                <option name="regexp"/>
            </color-match>
            <color-logic type="enumeration" cardinality="optional" default="and">
                <option name="and"/>
                <option name="or"/>
                <option name="not"/>
            </color-logic>
        </search>
        <search-doors href="/cars" method="GET">
            <doors type="integer" cardinality="required"/>
            <door-logic type="enumeration" cardinality="required" default="and">
                <option name="and"/>
                <option name="or"/>
                <option name="not"/>
            </door-logic>
        </search>
    </cars>
    

    So just say we search for white cars, we would GET /cars?color=white and we might get back something like:

    <cars href="/cars?color=white">
        <car href="/cars/beta">...</car>
        <car href="/cars/delta">...</car>
        ...
        <next href="/cars?color=white&page=2"/>
        <search-color href="/cars?color=white" method="GET">
            <color2 type="string" cardinality="required"/>
            <color2-match type="enumeration" cardinality="optional" default="substring">
                <option name="exact"/>
                <option name="substring"/>
                <option name="regexp"/>
            </color2-match>
            <color2-logic type="enumeration" cardinality="optional" default="and">
                <option name="and"/>
                <option name="or"/>
                <option name="not"/>
            </color2-logic>
        </search>
        <search-doors href="/cars?color=white" method="GET">
            <doors type="integer" cardinality="required"/>
            <door-logic type="enumeration" cardinality="required" default="and">
                <option name="and"/>
                <option name="or"/>
                <option name="not"/>
            </door-logic>
        </search>
    </cars>
    

    This result then let’s us refine our query. So just say we wanted white cars but not “off-white” cars, we could then GET ‘/cars?color=white&color2=off-white&color2-logic=not’, which might return

    <cars href="/cars?color=white&color2=off-white&color2-logic=not">
        <car href="/cars/beta">...</car>
        <car href="/cars/delta">...</car>
        ...
        <next href="/cars?color=white&color2=off-white&color2-logic=not&page=2"/>
        <search-color href="/cars?color=white&color2=off-white&color2-logic=not" method="GET">
            <color3 type="string" cardinality="required"/>
            <color3-match type="enumeration" cardinality="optional" default="substring">
                <option name="exact"/>
                <option name="substring"/>
                <option name="regexp"/>
            </color3-match>
            <color3-logic type="enumeration" cardinality="optional" default="and">
                <option name="and"/>
                <option name="or"/>
                <option name="not"/>
            </color3-logic>
        </search>
        <search-doors href="/cars?color=white&color2=off-white&color2-logic=not" method="GET">
            <doors type="integer" cardinality="required"/>
            <door-logic type="enumeration" cardinality="required" default="and">
                <option name="and"/>
                <option name="or"/>
                <option name="not"/>
            </door-logic>
        </search>
    </cars>
    

    We could then further refine our query, but the point is that at each step along the way, the hypermedia controls tells us what is possible.

    Now, if we think about the search options for cars, the colors, doors, makes and models aren’t unbounded, so we could make the options more explicit by providing enumerations. For instance

    <cars href="/cars">
        ...
        <search-doors href="/cars" method="GET">
            <doors type="enumeration" cardinality="required">
                <option name="2"/>
                <option name="3"/>
                <option name="4"/>
                <option name="5"/>
            </doors>
            <door-logic type="enumeration" cardinality="required" default="and">
                <option name="and"/>
                <option name="or"/>
                <option name="not"/>
            </door-logic>
        </search>
    </cars>
    

    However the only white cars that we have might be 2 and 4 door, in which case GETing /cars?color=white might give us

    <cars href="/cars?color=white">
        ...
        <search-doors href="/cars?color=white" method="GET">
            <doors type="enumeration" cardinality="required">
                <option name="2"/>
                <option name="4"/>
            </doors>
            <door-logic type="enumeration" cardinality="required" default="and">
                <option name="and"/>
                <option name="or"/>
                <option name="not"/>
            </door-logic>
        </search>
    </cars>
    

    Similarly, as we refine the colors, we might find their are only a few options, in which case we can switch from providing a string search, to providing an enumeration search. e.g., GETing /cars?color=white might give us

    <cars href="/cars?color=white">
        ...
        <search-color href="/cars?color=white" method="GET">
            <color2 type="enumeration" cardinality="required">
                <option name="white"/>
                <option name="off-white"/>
                <option name="blue with white racing stripes"/>
            </color2>
            <color2-logic type="enumeration" cardinality="optional" default="and">
                <option name="and"/>
                <option name="or"/>
                <option name="not"/>
            </color2-logic>
        </search>
        ...
    </cars>
    

    You could do the same for other search categories. For instance, initially you wouldn’t want to enumerate all the makes, so you would provide some sort of text search. Once the collection has been refined, and there are only a few models to pick from, then it makes sense to provide an enumeration. The same logic applies to other collections. For instance you wouldn’t want to enumerate all of the cities in the world, but once you have refined the area to 10 or so cities, then enumerating them can be very helpful.

    Is there a library that will do this for you? None that I know of. Most I’ve seen don’t even support hypermedia controls (i.e., you’ve got to add the links and forms yourself). Is there a pattern you can use? Yes, I believe the above is a valid pattern for solving this sort of problem.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a jquery bug and I've been looking for hours now, I can't
I'm making a simple page using Google Maps API 3. My first. One marker
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
This could be a duplicate question, but I have no idea what search terms

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.