Said Savci
Said Savci

Reputation: 858

What data structure is best to represent search filters?

I'm implementing a native iOS application, which provides a feature to search for articles using keywords and several search filters such as author, year, publisher etc. Each of these search filters has several options such as 2014, 2013, and 2012 for the year filter, or Academe Research Journals, Annex Publishers, and Elmer Press for the publisher filter. Each of these options has a BOOL stating whether the option is selected or not. I need an object that wraps the search keywords and search filters so that I can send it to the server, which is responsible for the search operation.

Which data structure should I use to represent these search filters in the wrapper class?

Upvotes: 1

Views: 147

Answers (1)

Bernhard Barker
Bernhard Barker

Reputation: 55619

Something like XML comes to mind:

<year>2014</year>
<publisher>Annex Publishers</publisher>

Although I find it rather bulky. I'd probably prefer something like:

year=2014|publisher=Annex Publishers

You'll need to escape = and | appearing in the field names or values, but this is easy to do.

This could just be a single string sent across.

In terms of actual data structures, you could have a map of field name to value, only containing entries where the option is selected. Or you could have a class containing pointers / references for each field, set to null if the option is not selected.


Another totally different consideration is to use an enumerated type, i.e. mapping each possible value to an integer, typically resulting in less memory used and faster (and possibly more robust) code, depending on how exactly this is done.

You could map it as follows, for example:

Academe Research Journals  0
Annex Publishers           1
Elmer Press                2

Then, rather than sending "Annex Publishers" as publisher, you could just send 1.

year=2014|publisher=1

The extension for multiple possible values for a field can be done in various ways, but it's fairly easy to do:

<year>2014</year>
<year>2013</year>
<publisher>Annex Publishers</publisher>

Or:

year=2014,2013|publisher=Annex Publishers

Upvotes: 1

Related Questions