tsearch2::build_query_tcl (public)

 tsearch2::build_query_tcl [ -query query ]

Defined in packages/tsearch2-driver/tcl/tsearch2-driver-procs.tcl

Convert conjunctions to query characters for tsearch2 and => & not => ! or => | space => | (or)

Switches:
-query
(optional)
string to convert
Returns:
returns formatted query string for tsearch2 tsquery

Partial Call Graph (max 5 caller/called nodes):
%3 test_build_query_tcl build_query_tcl (test tsearch2-driver) tsearch2::build_query_tcl tsearch2::build_query_tcl test_build_query_tcl->tsearch2::build_query_tcl tsearch2::build_query tsearch2::build_query (private) tsearch2::build_query->tsearch2::build_query_tcl tsearch2::build_query_postgres tsearch2::build_query_postgres (private) tsearch2::build_query_postgres->tsearch2::build_query_tcl

Testcases:
build_query_tcl
Source code:
    # get rid of everything that isn't valid in a query
    # letters, numbers, @ . - ( ) are all valid
    regsub -all {[^-/@.\d\w\s\(\)]+} $query { } query

    # match parens, if they don't match just throw them away
    set p 0
    for {set i 0} {$i < [string length $query]} {incr i} {
        switch [string index $query $i] {
            "(" {incr p}
            ")" {incr p -1}
        }
    }
    if {$p != 0} {
        regsub -all {\(|\)} $query {} query
    }

    # remove empty ()
    regsub -all {\(\s*\)} $query {} query

    # remove "or" at beginning of query
    regsub -nocase "^or " $query {} query

    # remove "not" at end of query
    regsub -nocase " not$" $query {} query

    # remove "not" alone
    regsub -nocase "^not$" $query {} query

    # replace boolean words with boolean operators
    regsub -nocase "^not " $query {!} query
    set query [string map {" and " " & " " or " " | " " not " " ! "$query]

    # remove leading and trailing spaces so they aren't turned into &
    set query [string trim $query]

    # remove any spaces between words and operators
    # all remaining spaces between words turn into &
    while {[regexp {([-/@.\d\w\(\)])\s+?([-/@.\d\w\(\)])} $query]} {
        regsub {([-/@.\d\w\(\)])\s+?([-/@.\d\w\(\)])} $query {\1 \& \2} query
    }
    # if a ! is by itself then prepend &
    regsub -all {(\w+?)\s*(!)} $query {\1 \& !} query
    # if there is )( then insert an & between them
    # or if there is )\w or \w( insert an & between them
    regsub {(\))([\(\w])} $query {\1 \& \2} query
    regsub {([\)\w])(\()} $query {\1 \& \2} query
    if {[regsub {!|\||\&} $query {}] eq ""} {
        set query ""
    }
    return $query
XQL Not present:
Generic, Oracle
PostgreSQL XQL file:
packages/tsearch2-driver/tcl/tsearch2-driver-procs-postgresql.xql

[ hide source ] | [ make this the default ]
Show another procedure: