KNOWLEDGE BASE
Log In    |    Knowledge Base    |    4D Home
Tech Tip: Checking Keywords for a Keyword Indexed Field
PRODUCT: 4D | VERSION: 16 | PLATFORM: Mac & Win
Published On: August 9, 2018
Your current browser may not allow you to download the asset. We recommend Mozilla Firefox or Google Chrome.
Log In

When a field is set up to have a keyword index, the data for the field of each record is parsed into keywords allowing for a more efficient query.

Sometimes it can be beneficial to check the keywords to see if the queried string will benefit from the search.

The keywords can be found using the DISTINCT VALUES command.
Below is a utility method that will do so:

// Method: GetKeywords
//
// Description: Gets keywords for a field and
// returns the number of keywords found.
//
// Parameters:
// $1: Pointer to an array to recieve list of keywords
// $2: Pointer to a table field to get keywords from
// $3: Optional Pointer to table, if passed will return keywords for all records.
//
// Results:
// $0: Longint containing number of keywords found
//
// ------------------------------------------------------------------------------

C_LONGINT($0)

C_POINTER($1)
C_POINTER($2;$fieldPtr)
C_POINTER($3;$tablePtr)

ARRAY TEXT($keywords_at;0)

If(Count parameters>1)
  If (Count parameters=3)
    $tablePtr:=$3
    ALL RECORDS($tablePtr->)
  End if

  $fieldPtr:=$2

  DISTINCT VALUES($fieldPtr->;$keywords_at)

  COPY ARRAY($keywords_at;$1->)
End if

$0:=Size of array($keywords_at)


The command takes in three parameters with the third being optional.
The first is a pointer to an array to contain the resulting array of keywords.
The second is the field to get the keywords from.
The third is the table, if passed will query all records on the table to get all keywords. If a keyword for a selection is desired it should be queried before calling the command and the third parameter should be omitted.
The command also returns the number of keywords found as an output.

Example of use
Take the following Table:


With the following Records:


Calling the command on it :
C_LONGINT($valuesCount)

ARRAY TEXT($values;0)
$valuesCount:=GetKeywords (->$values;->[Table_1]Field_2;->[Table_1])
TRACE

Will result in: