# Term expressions

A **term expression** says what a search looks for. It is what the search field on
reconnectchina.org takes, and what `term` takes in the API. A text on its own is searched for as
a phrase, and `&&` (and), `||` (or), `!` (not) and parentheses combine texts:

```rcql
(半导体 || semiconductor) && !补贴
```

This finds documents whose Chinese text mentions 半导体 or whose English translation mentions
"semiconductor", and that do not mention 补贴 (subsidies).

In short:

- `climate policy`: the phrase "climate policy".
- `"climate policy"`: the same phrase, quoted. Inside quotes, everything is text, and `\"` and
  `\\` stand for `"` and `\`.
- `a && b`: documents that match both `a` and `b`.
- `a || b`: documents that match `a`, `b` or both.
- `!a`: documents that don't match `a`.
- `(a || b) && c`: parentheses group. Without them, `!` binds tighter than `&&`, and `&&` binds
  tighter than `||`.

## Texts

A text on its own is searched for as a **phrase**: its words next to each other, in this order.

```rcql
climate policy
```

This finds documents that say "climate policy". It doesn't find one that has "climate" in one
place and "policy" in another. To find documents with both words anywhere, join them with `&&`:

```rcql
climate && policy
```

Spaces, tabs and line breaks around a text are dropped, and those inside it are kept. This includes
the ideographic space (U+3000) that Chinese input methods type, so the first expression below
searches for `中国`, and the second for the two words together.

```rcql
　中国　
中国　政策
```

## Which language a text is searched in

A text with a Chinese character in it is searched for in the Chinese text of documents. Any other
text is searched for in their English translation, which is machine-generated. To find a subject
in both, give it in both languages and join them with `||`, as in the example at the top.

A text that mixes both, such as `5G 基站`, has a Chinese character in it, so it is searched for in
Chinese.

Which parts of a document are searched, and how the results are ranked, is not part of the
expression. In the API, `strategy` chooses it.

## Characters that are part of a text

Only the doubled `&&` and `||` are operators. A single `&` or `|` is part of the text. So is `!`,
anywhere but in front of a text. So is a backslash. Each of these is one text:

```rcql
R&D
Yahoo!
C:\temp
```

A text written like this can't hold a double quote or a parenthesis. It can't start with `!`,
which would negate it. A single `&` or `|` in it has to be followed by more of the text, not by
the end, a quote or a parenthesis. So these are errors:

```rcql-error
R&
AT&"T"
```

Any text that breaks one of these rules can still be searched for: put it in quotes.

## Quoted texts

Between double quotes everything is text, operators and parentheses included:

```rcql
"R&"
"R&D (pilot)"
"!important"
```

Inside quotes, a backslash makes the next character part of the text: `\"` is a quote and `\\` is
a backslash. Before any other character the backslash is simply dropped, so `"\n"` is the letter
`n`, not a line break. The spaces inside the quotes are all kept.

Where both ways of writing a text are possible, they mean the same: `"climate policy"` and
`climate policy` are the same search.

**To search for any text exactly as it is**, whatever characters it holds, put a backslash before
every `"` and every `\` in it, and wrap the result in double quotes. The text `R&D "A"\B` becomes:

```rcql
"R&D \"A\"\\B"
```

Where a length limit applies, as it does to `term` in the API, it counts the expression as
written, escapes included.

## Combining texts

`&&` requires both sides, `||` either side, and `!` excludes what follows it:

```rcql
新能源 && 汽车
半导体 || 芯片
climate && !subsidy
```

`!` binds tighter than `&&`, and `&&` binds tighter than `||`. So the first expression below means
`tariff || (export && control)`: documents that mention "tariff", and documents that mention both
"export" and "control". Parentheses group the parts differently: the second finds documents that
mention "control" and at least one of "tariff" and "export".

```rcql
tariff || export && control
(tariff || export) && control
```

`!` applies only to the text or the parentheses right after it. The first expression below means
`(!climate) && policy`. To exclude a combination, put it in parentheses, as in the second.

```rcql
!climate && policy
!(climate || weather)
```

Spaces, tabs and line breaks between the parts are ignored, so `a&&b` and `a && b` are the same,
and a long expression may be spread over several lines.

Two texts side by side are an error. To search for both in any order, join them with `&&`, or
write them as one quoted text if they belong together as a phrase (refer to *Quoted texts*
above). The same goes for a text followed by parentheses: in the second expression below, `a &`
is a text, and `(b)`, which means `b`, stands next to it. Consequently, the following would be an
error:

```rcql-error
climate "policy"
a & (b)
```

## Errors

An expression that doesn't follow these rules is refused whole. The API searches nothing and
answers with `400` (`invalid-request`), and one entry of `errors` points at `/term` and says what
is wrong. It never falls back to searching the input as plain text, since a mistyped operator
would then return results for something you did not ask for.

None of the following is accepted:

- An empty expression, or one of only spaces. In the API, leave out `term` instead.

- An operator with nothing on one side of it:

  ```rcql-error
  climate &&
  && climate
  !
  ```

- Parentheses that don't pair up, or have nothing between them:

  ```rcql-error
  (climate
  climate)
  ()
  ```

- A quote that isn't closed:

  ```rcql-error
  "climate
  ```

- Two texts side by side, or a text and parentheses, such as `climate "policy"`, as described in
  *Combining texts*.

- A single `&` or `|` at the end of a text, or right before a quote or a parenthesis. Quote the
  text instead, as described in *Characters that are part of a text*.

  ```rcql-error
  R&
  AT&"T"
  ```

## Searching without a term

`term` is optional. A search without one leaves out `strategy` too, and finds every document
that passes its filters, **newest first** by the chosen date (`dateField`). With `topicIn`,
`typeIn` and `levelIn` it is a stream of what has just come in. Here, for example, are the
laws, regulations and plans on trade and commerce from central and provincial issuers that
we collected in the last day:

```json
{
  "typeIn": ["law/decree", "regulation", "opinion/plan"],
  "levelIn": ["centre", "provinces"],
  "topicIn": ["trade", "commerce"],
  "dateField": "added",
  "from": "now-1d"
}
```

Saved and run every morning, it returns what is new since the day before, most recently
collected first, including documents dated earlier that we only found now.
