extending search capabilities of database module

extending search capabilities of database module

by Jeff Graham -
Number of replies: 23
Hi all,

The new database module is pretty cool, but it seems the search capabilities are a little rudimentary. So we are looking at extending them to include the capability to search on specific fields.

I'm thinking an advanced search option which will keep the current behavior when set for simple search. For the advanced search corresponding search fields will be displayed for all fields with appropriate form types. This will require extending the field.class.php files with something like;
  • display_search_field() to properly set the type of field
  • parse_search_field() (or similar) to handle form submission
  • generate_sql to create proper sql snippet for WHERE clause
Then dynamically creating a sql query; as I see it this will require a dynamic number of table joins based on the number of fields to be searched. This is because all of the data is stored in the {$CFG->prefix}data_content table.

Please comment on any ideas. Attached is an idea for an advanced search form; with a little data above it note that not all fields are displayed, but are searchable.
Attachment Picture_1.png
Average of ratings: -
In reply to Jeff Graham

Re: extending search capabilities of database module

by Jeff Graham -
Nobody responded to my previous post, but since we needed it here I have developed a modification for the database module to allow for advanced searches.

Attached are two screenshots, one showing field setup of the demo database used and one showing an example advanced search. There is also a unified diff attached that patched cleanly with moodle head this morning. It will allow you to search on a specific field or fields from the database module. The advanced search form is dynamically generated from the database fields present so as you change your database structure your database search capabilities will change to suit  the schema.

Please note that in the example screenshots, not all data fields are displayed, but you do have the ability to search on all fields.

Due to the modularity of the database I have extended it to allow each data field to create its own search field, dropdown or text, etc. so that appropriate searches happen depending on the data that should be present. Currently date fields do not have search capabilities as I am not aware of a moodle function for handling date entry that allows for no date to be set via a form.

Hopefully others find this useful.

regards,
Jeff

In reply to Jeff Graham

Re: extending search capabilities of database module

by Jeff Graham -
New diff is attached with improved query speed; with large data sets the advanced search was way too slow. This fix even speeds up regular display.

It should be possible to further improve the speed. I am taking a look into cleaning up the record retrieval method.

regards,
Jeff
In reply to Jeff Graham

Re: extending search capabilities of database module

by Daniel Brouse -
Jeff,

I just wanted to jot a note thanking you for your posts and your work on this. I am just getting ready to migrate to v1.6 and figuring out which mods to add to the standard installation is always an important step in getting ready for the switch to a new version that will be in production.

As a relatively non-technical user (I don't even have a usable grasp of html) it is always appreciated when someone who knows how to improve the software, does so! Bravo!

Thanks again,

Daniel
In reply to Jeff Graham

Re: extending search capabilities of database module

by Jeff Graham -
The data module should have indexes added to {$CFG->prefix}data_content for the following fields: 'fieldid', 'recordid', and 'content' with an appropriate size(10?) also 'recordid, fieldid'

Also, the row retrieval method should be reviewed and something like the following should be considered to retrieve a record in one query rather than X queries where X represents the number of fields in the database; the current method is rather slow for databases with a large number of fields.

select r.id, f12.content as Category,
f13.content as Company,
f14.content as Position,
f15.content as Contact,
f24.content as Phone,
f25.content as Cell,
f26.content as Fax,
f27.content as Address,
f28.content as City,
f30.content as Zip,
f31.content as Website,
f32.content as Email
FROM
mdl_data_records r
LEFT JOIN mdl_data_content f12 ON f12.recordid=r.id AND f12.fieldid=12
LEFT JOIN mdl_data_content f13 ON f13.recordid=r.id AND f13.fieldid = 13
LEFT JOIN mdl_data_content f14 ON f14.recordid=r.id AND f14.fieldid = 14
LEFT JOIN mdl_data_content f15 ON f15.recordid=r.id AND f15.fieldid = 15
LEFT JOIN mdl_data_content f24 ON f24.recordid=r.id AND f24.fieldid = 24
LEFT JOIN mdl_data_content f25 ON f25.recordid=r.id AND f25.fieldid = 25
LEFT JOIN mdl_data_content f26 ON f26.recordid=r.id AND f26.fieldid = 26
LEFT JOIN mdl_data_content f27 ON f27.recordid=r.id AND f27.fieldid = 27
LEFT JOIN mdl_data_content f28 ON f28.recordid=r.id AND f28.fieldid = 28
LEFT JOIN mdl_data_content f30 ON f30.recordid=r.id AND f30.fieldid = 30
LEFT JOIN mdl_data_content f31 ON f31.recordid=r.id AND f31.fieldid = 31
LEFT JOIN mdl_data_content f32 ON f32.recordid=r.id AND f32.fieldid = 32;

Please note that this small example to illustrate the idea. By adding the indexes mentioned above this query should be quick. This could also be considered to handle returning all of the results using "r.id IN (192,193,194)" where the values in parenthesis are obtained via the patch provided.

This suggested change combined with my latest patch in this thread should offer much improved speed by minimizing result sets, and the number of database queries necessary.

The code could also potentially thoughtful be simplified by returning the whole result set via the above SQL and using the standard API for tables to handle paging etc.

Could whomever is maintaining the data module please consider and post any implications that I may have missed in making these modifications.

In reply to Jeff Graham

Re: extending search capabilities of database module

by N Hansen -
Hi Jeff-I'm very interested in this. I had just filed bug 5958 before I came across this. Perhaps you can work with the core developers on getting this integrated.
In reply to Jeff Graham

Re: extending search capabilities of database module

by Gerard van de Lustgraaf -
Jeff,

I think you try to do something I really need for my Moodle install: a way to search in every field (or even more than one field). As a non-techie I don't understand whether this has been integrated by you into the standard database module I download when I download Moodle 1.6 or whether I have to do all that coding I see in your forum posts.

Can you say something about that?

Regards,

Gerard
In reply to Gerard van de Lustgraaf

Re: extending search capabilities of database module

by Jeff Graham -
Hi Gerard,

By default the regular search will search every field. With the advanced search you can restrict searches to a specific field.

This is not currently in the standard moodle install. I provided a diff above in hopes that the maintainer would take a look and merge it into head.

Attached to this post is a complete directory for the data module with the changes applied so you don't need to bother with the diff above. You can extract this and drop it into the modules directory replacing your existing data module and advanced search as shown above will be available with fields corresponding to your database.

The code (SQL) you see in the above post is a suggestion for improved speed for queries and was intended for a developer to review. We are noticing a bit of sluggishness on large data sets and adding the indexes as suggested above speeds up the queries tremendously, as would be expected.
In reply to Jeff Graham

Re: extending search capabilities of database module

by Gerard van de Lustgraaf -
Thanks Jeff,

I venture to say I understand what you write after I looked up 'diff' in Wikipedia. approve

That leaves me with one question, you write: "you can restrict searches to a specific field". Does "a" mean: "one" or "as many as you like and need"?

If it does mean "one", would it be relatively big grin easy to make that "as many as you like and need"? I would love to use the database module for user profiles and would of course like to allow my students to look for "guys" interested in "Buddhism" of around "24" years old living "near me". Instead of browsing all the guys or all the Buddhists or all 24 year olds or all residents of "Pyongyang.

Cheers from Gerard from Belgium
In reply to Jeff Graham

Re: extending search capabilities of database module

by Jim Proctor -
Thanks very much for your hard work and for making this available. I downloaded and noticed two things: (a) the need for a language pack addn, and (b) your field-specific search routines seem not to populate the drop-down menu items correctly when the field allows multiple items.

Cheers,

Jim P.
In reply to Jim Proctor

Re: extending search capabilities of database module

by Carlos Mação -

...and the size of the drop-down field editor grows by the count of the multiple items, hiding the second fields column.

Congratulations too,

Carlos Macao

In reply to Jeff Graham

Re: extending search capabilities of database module

by Andrew Seears -
I tried installing this, and everything worked fine except that the Menu dropdown boxes were empty.  They just said "Choose..." without any options to choose. 

Any ideas on what might be causing this or how to fix it?
In reply to Andrew Seears

Re: extending search capabilities of database module

by Roger Emery -
Hi,

Has there been any further progress on this development before we start our own modfications?

 Just wonderd as a member of staff here has an interest in making further use of the database module - the advanced search idea looks good.
Also export as CSV file option would be good from the user interface for staff/students that want to process the data further without the administrator (me!) having to pull the data out of the database for them.

Roger
In reply to Jeff Graham

Re: extending search capabilities of database module

by Carlos Mação -

An additional problem: when we choose a filter selection, and the the system returns a multi-page dataset, and we select go to next page, it says that there are no compatible registers.

In reply to Jeff Graham

Re: extending search capabilities of database module

by Jim Proctor -
While we're talking about search capabilities it's worth noting that, rather than defaulting to list browse view, the user would encounter a search page upon entering a particular database. This of course is typically how one enters into a database of any size. One possibility could be to insert the appropriate search code in the header section of the list template. Has anyone given this a try?

Thanks,

Jim
In reply to Jeff Graham

Re: extending search capabilities of database module

by Elijah Atkinson -
Developement for the advanced search template is as follows:

I've gotten the advanced search tab working as the default tab for the advanced search option.  I've managed to get a default template to display in the advanced search tabe when you click the "Reset template" button.  This template shows up in the data object for the database under the [asearchtemplate] tag, but when I click "Save template" I get an "Invalid argument applied to a foreach" error....

Im not quite sure how the templates are linked to their data objects.  I've been looking through the Database files, but I haven't been able to find this yet.  What function gets called when you click "Save template"?  If I knew this, I would be able to get some progress made.

I'll keep looking through the module however.  I'll figure it out eventually.

In reply to Elijah Atkinson

Advanced Search Template

by Elijah Atkinson -
I have completed the advanced search template. Attached is a final build that I have yet to test on a Windows machine.

Let me know if there are any incompatibilities. I will test it myself in the next few days.

Let me know if any bugs or issues are discovered.
In reply to Elijah Atkinson

Re: Advanced Search Template

by Jeff Graham -
Hi all,

I just wanted to say that Elijah works in the same shop as me here, and he has been working on adding templates to the search functionality.

I am working on other projects now so post any incompatabilities and bugs here.

Also, it looks like this is planned for inclusion in 1.8 so "vote for it" in the bug tracker to make sure it makes it in 1.8 MDL-5958

regards,
Jeff
In reply to Elijah Atkinson

Re: Advanced Search Template

by rich holman -

Hi

Have added the advanced search but when trying to view the advanced search tab I am getting the error message

"Error updating template"

When I try and edit the template the changes are not saved?

Version 1.6 any ideas????

In reply to rich holman

Re: Advanced Search Template

by rich holman -

I am using moodle 1.6.3 plus, I want to use the advanced search attached in this thread, I've replaced the data directory and all appears good, but when I enter the admin area I get an error message saying I can't down grade the data module. 

Can someone please post either:

A zip of the data directory (including advanced search) that works with 1.6.3 plus

or

Details of how to modify the version I have to stop admin telling me I cant downgrade.

Thanks in advance

Rich

In reply to rich holman

Re: Advanced Search Template

by Elijah Atkinson -
Thats a version number issue. Check the Module version in Data/view.php in your current directory. Compare this against the Module version in the new Data/view.php. I believe the new version number should be at least 1 larger than the old version number for an upgrade to happen. Make the smallest change you can to the number so it doesnt get out of sync.
In reply to Elijah Atkinson

Re: Advanced Search Template

by John Doe II -

Hi,

Does someone know how to tweak Moodle 1.8.3 in order to incorporate this advanced search?

I have seen the MDL-5958 and all the .diff files thereby attached, but I was wondering if there was an easy way to "manually" intervene on the 1.8.3 code to integrate this advanced search functionality?

Thank you in advance for any help or ideas.

In reply to Jeff Graham

Re: extending search capabilities of database module

by Andrew Walbran -
I recently had a similar (though less complex) requirement, to be able to filter the database by a certain field (in this case a 'category', which is a menu field). I implemented this by allowing filter forms to be added to the list header and footer templates, so adding e.g. {{Category}} to the header template would cause a form to be inserted allowing the user to choose which category to show. It should also work for other field types, though this has not been tested much.

It sounds like this functionality would be covered by the 'Advanced Search Template' feature, but I have attached my patch anyway in case it is useful to anybody else.