> ## Documentation Index
> Fetch the complete documentation index at: https://docs.abbyy.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Advanced pre-search relations

> Advanced pre-search relations run FlexiLayout language code before object search, restricting search areas, canceling searches, or selecting hypotheses.

Advanced pre-search relations set search constraints and element properties in the [FlexiLayout language](/flexi-capture/fls/code/general-code). The pane where you enter your code is on the **Advanced** tab of the **Properties** dialog box.

The code you enter into the **Advanced pre-search relations** pane is executed immediately before searching for the image object that corresponds to the FlexiLayout element. First, the program uses the properties you specified on the tabs to the left of the **Advanced** tab, and then it runs the code.

If a particular parameter is specified both on a tab to the left of the **Advanced** tab and in the code on the **Advanced** tab, the code has priority. If a parameter is specified on a tab to the left of the **Advanced** tab but is not specified in the code, the value of the parameter from the relevant tab is used.

<Note>
  The `SearchText` function (**Static Text** element) is an exception. This function *adds* search words to those already specified on the **Static Text** tab.
</Note>

If you have specified some properties on any of the tabs to the left of the **Advanced** tab, you can translate them into code by clicking the **Code** button on the **Advanced** tab. You can then copy or edit the generated code.

<Note>
  The additional constraints entered in the **Advanced pre-search relations** pane can only refer to elements located above the current element in the FlexiLayout tree. The only exceptions are the subelements of the same **Repeating Group** element, which can refer to previous instances of subelements located in the same group below the given element.
</Note>

## Examples

### Specify different values for the same parameter

Sometimes you need to assign different values to the same parameter. To do this, use the `if... then` operator:

```text theme={null}
if Element1.IsNull then {
MaxErrors: 3, 0.25;
} else {
MaxErrors: 5, 0.5;
}
```

### Limit the search area

The search area can be specified by using any of the types that define an image area: `Rect`, `RectArray`, `Region`. It can also be specified by using an expression that returns any of these types. The result of calculating the expression is passed as an argument to the `RestrictSearchArea()` function:

```text theme={null}
RestrictSearchArea( Rect(Rect( Page( 1 ).RectGlobal.Left, Page( 1 ).RectGlobal.Top+20*mm, Element1.Left.Start, Element2.Ycenter.Start ) );
```

You can also pass an expression that combines the regions of several elements:

```text theme={null}
let rect1 = Element1.Rect;
let rect2 = Element2.Rect;
RestrictSearchArea( rect1 or rect2 );
```

### Cancel the search for an object

For some documents, the presence of one object on the image automatically excludes another object. In other cases, you might want to look for a particular object only if certain conditions have been met. You can tell the program not to look for a specific object by using the `DontFind()` command. Once the command is executed, the program formulates a null hypothesis for the corresponding object:

```text theme={null}
if Element1.IsNull() then DontFind()
```

### Select a hypothesis

Sometimes several image objects meet the conditions specified in an element. This means that the program formulates several hypotheses. If you need to select from this set only the hypothesis that is located closest to some other object or point on the image, you can use one of the following functions:

| Function                       | Description                                           |
| ------------------------------ | ----------------------------------------------------- |
| `Nearest( [name of element] )` | Sets the name of the closest element.                 |
| `Nearest( x1, y1 )`            | Sets the coordinates of the closest point.            |
| `NearestX( x1 )`               | Sets the X coordinate of the closest vertical line.   |
| `NearestY( y1 )`               | Sets the Y coordinate of the closest horizontal line. |

<Note>
  You cannot use several `Nearest` functions at once.
</Note>

### Search on multi-page documents

The program treats a multi-page document as a single entity and can look for an element of a multi-page document without reference to an exact page.

To specify that an element can be found in the bottom half of any page:

```text theme={null}
Below: PageRect.Top + PageRect.Height / 2;
```

For more information, see the [description of the PageRect function](/flexi-capture/fls/language/global-functions).

To specify that an element is located on the last page of a document (you can write this code only in the principal part of the FlexiLayout, after the **Header** and **Footer** elements, if any):

```text theme={null}
RestrictSearchArea( Page( PagesCount ).RectGlobal );
```

To specify that an element is located on the page where an element named `SearchElements.OtherElement` begins:

```text theme={null}
if SearchElements.OtherElement.IsFound then RestrictSearchArea( WholePage, SearchElements.OtherElement.Pages.Start );
```

To specify that an element can be located on each page below the instances of the hypotheses for a repeating group named `SearchElements.RepeatableGroup`:

```text theme={null}
Below: SearchElements.RepeatableGroup.AllInstances.PageAreaGlobal( PageNumber ).Region.Bottom;
```

To specify that an element can be located on each page below the body of each subtable of a table named `SearchElements.Table`:

```text theme={null}
Below: SearchElements.Table.AllSubTables.Body.PageAreaGlobal( PageNumber ).Bottom;
```

## Related topics

* [FlexiLayout language](/flexi-capture/fls/code/general-code)
