mnoGoSearch 3.3.9 reference manual: Full-featured search engine software
Prev |  | Next

# UdmFind2

## Name

UdmFind2 -- Executes a search query

## Description

`UDM_RESULT* UdmFind2(UDM_AGENT* Agent, const char* query);`

Executes a search query in the search session, pointed by the `Agent` variable, and returns search results.

## Parameters

`Agent`

  Pointer to a variable of type UDM_AGENT, should be the address of an existing variable of UDM_AGENT type.

`query`

  Search query, using the same format with search.cgi `QUERY_STRING` described in the Section called Search parameters in Chapter 10.

## Return Values

A pointer to an allocated variable of UDM_RESULT type, or `NULL` on error.

## Notes

`UdmFind2()` can fail if the search session could not open a connection to the database specified by the DBAddr command, or if some of the SQL queries which were sent to the database returnd on error.

## Examples

Example 1. `UdmFind2` example

    UDM_RESULT *Res;

    if (!(Res= UdmFind2(Agent, "q=search+words&ps=10"))
    {
      printf('Error: UdmFind2 failed');
      exit(1);
    }

Example 2. `UdmFind2` - a complete search application example

    #include <stdio.h>
    #include <udmsearch.h>

    int main(int ac, char **av)
    {
      UDM_ENV Env;
      UDM_AGENT Agent;
      UDM_RESULT *Res;
      size_t row;
      char query[256];

      if (ac < 2)
      {
        printf("Usage: %s query\n", av[0]);
        exit(1);
      }

      if (!UdmEnvInit(&Env) ||
          !UdmAgentInit(&Agent, &Env, 0))
        goto err;

      if (UDM_OK != UdmAgentAddLine(&Agent, "DBAddr mysql://root@localhost/msdn33/"))
        goto err;

      udm_snprintf(query, sizeof(query), "q=%s", av[1]);
      if (!(Res= UdmFind2(&Agent, query)))
        goto err;

      printf("Documents %d-%d from %d total found\n", Res->first, Res->last, Res->total_found);
      for (row= 0; row < Res->num_rows; row++)
      {
        UDM_DOCUMENT *Doc= &Res->Doc[row];
        printf("ResultNo: %d\n", UdmVarListFindInt(&Doc->Sections, "Order", 0));
        printf("ID      : %d\n", UdmVarListFindInt(&Doc->Sections, "ID", 0));
        printf("Size    : %d bytes\n", UdmVarListFindInt(&Doc->Sections, "Content-Length", 0));
        printf("Type    : '%s'\n", UdmVarListFindStr(&Doc->Sections, "Content-Type", ""));
        printf("Title   : '%s'\n", UdmVarListFindStr(&Doc->Sections, "title", ""));
        printf("Body    : '%s'\n", UdmVarListFindStr(&Doc->Sections, "body", ""));
        printf("\n");
      }
      UdmResultFree(Res);

      UdmAgentFree(&Agent);
      UdmEnvFree(&Env);

      return 0;

    err:
      fprintf(stderr, "Error: '%s'\n", UdmEnvErrMsg(&Env));
      return 1;
    }

Example 3. `Makefile` example

    CFLAGS=-I/usr/local/mnogosearch/include -Wall
    LDFLAGS=-L/usr/local/mnogosearch/lib -lmnogosearch -Wl,--rpath -Wl,/usr/local/mnogosearch/lib

    CC=gcc
    all: search

    search.o: search.c
            $(CC) $(CFLAGS) -c search.c -o search.o

    search: search.o
            $(CC) $(LDFLAGS) search.o -o search

    clean:
            rm -rf search search.o

## See Also

`UdmEnvInit`
`UdmEnvFree`
`UdmAgentFree`
the Section called Search parameters in Chapter 10

Prev | Home | Next
UdmAgentAddLine | Up | UdmResultFree