Fundamentals 8 min read

EasySearch Rules Syntax: Quick Reference Guide

This guide details the EasySearch rule syntax, covering rule structure, logical operators, field constraints, regular expressions, distance matching, numeric range matching, three practical tagging scenarios, common errors, special‑character escaping, and concise best‑practice recommendations.

Mingyi World Elasticsearch
Mingyi World Elasticsearch
Mingyi World Elasticsearch
EasySearch Rules Syntax: Quick Reference Guide

Rule Structure

Each rule consists of an expression and an optional description. When importing via the _import API, only the expression and description need to be supplied; the system automatically assigns an offset.

{
  "expression": "北京 or 京城",
  "description": "地域_北京"
}

Logical Operators

and

(alias &) – both conditions must be satisfied. or (alias |) – either condition may be satisfied. not (binary, aliases - and &!) – exclude matches.

Operator precedence (from highest to lowest): not (unary) > near > and > or. Parentheses () can be used to override precedence.

(source(新华网) or source(人民网)) and title(北京 or 上海)

Field Constraints

field(expr)

– substring match anywhere in the field (e.g., title(北京)). field[expr] – exact match of the whole field (e.g., sentiment[正面]).

Built‑in fields such as title and content need no declaration. Custom fields must be declared as numeric types at compile time.

POST /_match_rules/repo/_compile
{
  "fields": ["score", "heat"]
}

Regular Expressions

Simple regex: {{regex}} (e.g., {{1[3-9]\d{9}}} matches a mobile number).

Field‑specific regex: field("regex") (e.g., author("张.*")).

Composite regex logic: [expr and/not ...] (e.g., [author("张.*") and not author("张三丰")]).

⚠️ Regex inside a field must be quoted; author(/张.*/) is invalid.

Distance Matching (near)

Syntax:

A near/n B      # distance ≤ n, order irrelevant
A near/+n B     # A appears before B, distance ≤ n
A near/-n B     # A appears after B, distance ≤ n

Distance unit: one Chinese character, one English word, or one numeric string equals 1.

Difference from and:

# and – both words appear anywhere, possibly unrelated
北京 and 发布会

# near – words must be within a limited span, ensuring context
北京 near/10 发布会

Chained example:

(北京 or 京城) near/5 (经济 or GDP)

Numeric Range Matching

score(80, 100)   # positive rating range
score(0, 40)     # negative rating range
heat(10000, 99999)

Numeric fields must be declared as numeric types during compilation.

Three Common Tagging Scenarios

Region Tags

Region tag example
Region tag example
{
  "expression": "北京 or 京城 or 首都 or 中关村",
  "description": "地域_北京"
}
{
  "expression": "深圳 or 鹏城 or 南山区 or 前海",
  "description": "地域_深圳"
}

Sentiment Tags

Sentiment tag example
Sentiment tag example
{
  "expression": "非常好 or 极佳 or 强烈推荐",
  "description": "正面_强烈好评"
}
{
  "expression": "一般 or 还行 or 中规中矩",
  "description": "中性_平淡评价"
}
{
  "expression": "严重不满 or 投诉 or 退款 or 维权",
  "description": "负面_强烈差评"
}

Person Name Tags

Person name tag example
Person name tag example
{
  "expression": "author(\"张.*\") and source(清华大学 or 北京大学)",
  "description": "人名_高校学者姓张"
}
{
  "expression": "title(\"王.*\") and content(企业家 or CEO or 创始人)",
  "description": "人名_企业家姓王"
}

Common Errors Quick Check

Minus sign is parsed as NOT. Use quotes: "2024-2025年报" instead of 2024-2025年报.

Regex without quotes : write author("张.*") not author(/张.*/).

Numeric field not declared : include the field list in the compile request, e.g., { "fields": ["score"] }.

Separator should be a Tab between expression and description, not a space.

Special Character Escaping Quick Check

Characters that appear as literal values must be quoted: -, &, |, ( ), [ ], { } and spaces.

Rule of thumb: any character that may cause ambiguity should be wrapped in quotes.

Rule Writing Recommendations

Start simple, then expand : begin with pure keywords or OR, then gradually add field constraints and near.

Prioritize title : title(关键词) yields far higher precision than full‑text matching.

Prefer near over and : in the same semantic scenario, near significantly reduces false positives.

Use NOT to filter noise : for sentiment tags, exclude speculative contexts, e.g., and not (预测 or 如果 or 假设).

Regex for fuzzy matches : apply patterns like author("张.*") for names, models, etc.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

rule engineregexSearchEasysearchfield constraintsnear operatorquery syntax
Mingyi World Elasticsearch
Written by

Mingyi World Elasticsearch

The leading WeChat public account for Elasticsearch fundamentals, advanced topics, and hands‑on practice. Join us to dive deep into the ELK Stack (Elasticsearch, Logstash, Kibana, Beats).

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.