Search for a phrase within a text field

78 views
Skip to first unread message

Dan Eccles

unread,
Nov 27, 2021, 12:54:31 AM11/27/21
to Jam.py Users Mailing List
Hello,

I have a table containing a large text field.
I want to search the table for records which contain a phrase, for example for "my search phrase".

If I put that phrase into the view filter,  the search returns records where the words "my", "search" , "phrase"  appear anywhere in the text field.

Is it possible, using filters, to restrict the search to just the whole phrase?

Thanks,

Dan Eccles

unread,
Nov 30, 2021, 2:57:13 AM11/30/21
to Jam.py Users Mailing List
I now understand something about text searches in Jam.py   :) 

The search-input on the view form matches in CONTAINS_ALL mode, which splits the search string up into words using space delimiters.
Using a Filter form input set to CONTAINS mode does not split the string, and so can be used to search for a phrase. However, if I wanted to search for multiple phrases, I would need multiple Filter form inputs for the same field. This works fine, but I thought there might be a simpler way.
I made some small changes to the Jam.py source code to alter the behaviour of the CONTAINS_ALL match, so that it handles words and phrases through a single input. Now if I enter a search string like:   thank you "very much" for "asking me"   it finds records with the words thank, you, for,  and the phrases very much, asking me,  which is the behaviour I want.
If anyone is interested, here are the changes (shown in red):

To jam.py - change behaviour of CONTAINS_ALL:

import shlex

   def where_clause(self, query, db_module=None):
       if db_module is None:
           db_module = self.task.db_module
       conditions = []
       filters = query['__filters']
       deleted_in_filters = False
       if filters:
           for field_name, filter_type, value in filters:
               if not value is None:
                   field = self._field_by_name(field_name)
                   if field_name == self._deleted_flag:
                       deleted_in_filters = True
                   if filter_type == consts.FILTER_CONTAINS_ALL:
                        try:
                            values = shlex.split(value
                        except:
                            values = value.split()

                       for val in values:
                           conditions.append(self._get_condition(field, consts.FILTER_CONTAINS, val, db_module))

To jam.js - change field text highlighting to match:

    function highlight(text, search) {
        var i = 0,
            result = text,
            substr,
            start,
            str,
            strings,
            pos,
            p = [];
        if (search) {
            text += '';
            //strings = search.toUpperCase().split(' ')
            var regexp = /[^\s"]+|"([^"]*)"/gi;
            var strings = [];
            var cap_srch = search.toUpperCase();
            do {
                var match = regexp.exec(cap_srch);
                if (match != null)
                {
                    strings.push(match[1] ? match[1] : match[0]);
                }
            } while (match != null);


Jam

unread,
Nov 30, 2021, 8:00:57 PM11/30/21
to Jam.py Users Mailing List
Amazing, thanks for sharing.
Reply all
Reply to author
Forward
0 new messages