【免费下载链接】xerjXERJ is the new way for AI to search data. Its autoindex capability activates agents to know your data without the token waste of grep and sed. One command indexes code, docs, logs and PDFs for search, RAG, security audits and agent memory, using 40x fewer tokens than grep. Elasticsearch compatible, so existing clients just work.项目地址https://gitcode.com/GitHub_Trending/xe/xerj点击查看免费下载XERJ 提供与 Elasticsearch 兼容的_searchAPI本指南以一篇可直接复现的实战速查为主线逐一演示 XERJ 支持的 ES 查询家族match、match_phrase、term、terms、range、bool mustfilter、prefix、wildcard、exists以及highlight sort _source的组合用法。读完本文你将掌握用 curl 直接驱动 XERJ 完成分析型检索、精确匹配、范围过滤、存在性判断与结果投影的全部操作并理解其背后的查询解析与评分原理。准备建索引、写数据、起服务在开始查询之前需要确保 XERJ 服务已在localhost:9200监听XERJ 默认以 ES 兼容端口对外提供 REST API并准备好一个带映射的articles索引。映射中title/body为text走分析器与 BM25 评分views为integer可做范围与排序tag为keyword走精确匹配curl -s -XPUT http://localhost:9200/articles \ -H content-type: application/json \ -d {mappings:{properties:{title:{type:text},body:{type:text},views:{type:integer},tag:{type:keyword}}}}返回{acknowledged: true, shards_acknowledged: true, index: articles}HTTP 200。随后写入一批文档id 为1的文档与本篇速查的响应示例一一对应curl -s -XPUT http://localhost:9200/articles/_doc/1 \ -H content-type: application/json \ -d {title:Hello Xerj,body:one binary for search,views:42,tag:intro}创建与写入的完整流程可参考同目录下的快速上手演练该文档覆盖建索引、写文档、按 id 读取与首次搜索的完整链路。一、match分析型全文检索match是全文检索的入口查询查询文本会先经过分析器分词再对每个词元做 BM25 评分合并。以下命令在body字段中检索searchcurl -s -XPOST http://localhost:9200/articles/_search \ -H content-type: application/json \ -d {query:{match:{body:search}}}响应结构与 ES 完全一致——_shards汇总分片状态hits.total.relation为eq表示命中数是精确值max_score给出最高分{ _shards: {failed: 0, skipped: 0, successful: 1, total: 1}, hits: { hits: [ { _id: 1, _index: articles, _score: 0.28768211603164673, _source: {body: one binary for search, tag: intro, title: Hello Xerj, views: 42} }, { _id: e8953ed6-3cfa-411f-b585-c141cbb28f0f, _index: articles, _score: 0.28768211603164673, _source: {body: beta search, tag: b, title: B, views: 20} } ], max_score: 0.28768211603164673, total: {relation: eq, value: 2} }, timed_out: false, took: 1 }HTTP 200。注意_score为0.28768211603164673——这正是 BM25 公式在该语料上的计算结果。XERJ 的全文检索实现在 bm25.rs 中文件头部明确写明其采用与 Elasticsearch/Lucene 完全一致的常量与公式k1 1.2, b 0.75并在 lib.rs 中标注为 “BM25 scoring with ES-compatible constants (k11.2, b0.75)”因此同一语料、同一查询在 XERJ 与 ES 上得到的分数可以直接对照。match还支持operator、analyzer、boost、minimum_should_match、lenient等选项从 ast.rs 的QueryNode::Match定义可以看到多词查询在底层会按词元拆分为多个匹配再求和这一点与 Lucene 将Match重写为 BooleanQuery 的语义一致_explanation中也会体现为 per-term weight 的sum of结构。二、match_phrase词序敏感的短语匹配当需要保证检索词在文档中以连续的原始顺序出现时使用match_phrase。它要求文档在body字段中严格包含短语alpha vector词元相邻、顺序一致curl -s -XPOST http://localhost:9200/articles/_search \ -H content-type: application/json \ -d {query:{match_phrase:{body:alpha vector}}}{ _shards: {failed: 0, skipped: 0, successful: 1, total: 1}, hits: { hits: [ { _id: a42a64ea-0f2c-44b2-b4af-3db93b145cf3, _index: articles, _score: 1, _source: {body: alpha vector, tag: a, title: A, views: 10} } ], max_score: 1, total: {relation: eq, value: 1} }, timed_out: false, took: 1 }HTTP 200。短语匹配的底层实现位于 search.rs 的phrase_positions_matchslop 0时要求严格相邻slop 0时采用与 LuceneSloppyPhraseMatcher相同的移动距离语义前向间隙按宽度计、词序颠倒记 2。QueryNode::MatchPhrase在 ast.rs 中定义了slop、analyzer、boost、lenient等参数其中slop默认0a b~2即可匹配文档中的b a。兼容性测试 200_index_phrase_search.yml 同时覆盖了match_phraseDSL 与qpeter piper~1两种写法并验证单 token 短语、三 token 短语均命中可作为回归依据。三、term 与 termskeyword 字段的精确匹配term不做分词按整个字段值精确匹配常用于keyword类型的枚举字段如tag。以下查询找出所有tag恰为a的文档curl -s -XPOST http://localhost:9200/articles/_search \ -H content-type: application/json \ -d {query:{term:{tag:a}}}{ _shards: {failed: 0, skipped: 0, successful: 1, total: 1}, hits: { hits: [ { _id: a42a64ea-0f2c-44b2-b4af-3db93b145cf3, _index: articles, _score: 1.6931471824645996, _source: {body: alpha vector, tag: a, title: A, views: 10} }, { _id: 7e9e9f67-1d1b-4dbb-a833-06052cb89393, _index: articles, _score: 1.6931471824645996, _source: {body: gamma logs, tag: a, title: C, views: 30} } ], max_score: 1.6931471824645996, total: {relation: eq, value: 2} }, timed_out: false, took: 0 }HTTP 200。terms是term的多值版本语义为 OR——任一值命中即命中常用于“这批标签里任意一个”的筛选curl -s -XPOST http://localhost:9200/articles/_search \ -H content-type: application/json \ -d {query:{terms:{tag:[a,b]}}}命中 4 条文档tag为a或b的全部文档_score均为常量1took: 0。term/terms在 AST 中对应 ast.rs 的QueryNode::Term与QueryNode::Terms均带可选的boost参数Term的注释强调“Exact-value match on a single field”与match的分析语义形成明确区分。四、range数值 / 日期 / keyword 的范围过滤range对字段做范围过滤支持gte/gt/lte/lt四类边界可任意组合且可作用于数值、日期与可排序的 keyword。以下查询筛选views大于等于20的文档curl -s -XPOST http://localhost:9200/articles/_search \ -H content-type: application/json \ -d {query:{range:{views:{gte:20}}}}{ _shards: {failed: 0, skipped: 0, successful: 1, total: 1}, hits: { hits: [ {_id: 1, _index: articles, _score: 1, _source: {body: one binary for search, tag: intro, title: Hello Xerj, views: 42}}, {_id: e8953ed6-3cfa-411f-b585-c141cbb28f0f, _index: articles, _score: 1, _source: {body: beta search, tag: b, title: B, views: 20}}, {_id: 7e9e9f67-1d1b-4dbb-a833-06052cb89393, _index: articles, _score: 1, _source: {body: gamma logs, tag: a, title: C, views: 30}}, {_id: f5644d8f-b189-4f4e-bbec-e5dbcc4ccaf8, _index: articles, _score: 1, _source: {body: delta agg, tag: b, title: D, views: 40}} ], max_score: 1, total: {relation: eq, value: 4} }, timed_out: false, took: 0 }HTTP 200。QueryNode::Range在 ast.rs 中将四个边界建模为四个独立的可选字段并附带boost与lenient标记其中lenient仅在query_string下推产生时被置真见注释 #1284而直接书写rangeDSL 时始终是严格语义——传入类型不匹配的值会报错而非静默忽略。五、bool mustfilter评分与过滤的职责分离bool复合查询把不同子句组合进一个查询树。must子句必须命中且参与 BM25 评分filter子句必须命中但不参与评分分值贡献为零可被引擎缓存复用。下面的查询要求body包含logs评分同时tag必须为a过滤curl -s -XPOST http://localhost:9200/articles/_search \ -H content-type: application/json \ -d {query:{bool:{must:[{match:{body:logs}}],filter:[{term:{tag:a}}]}}}{ _shards: {failed: 0, skipped: 0, successful: 1, total: 1}, hits: { hits: [ { _id: 7e9e9f67-1d1b-4dbb-a833-06052cb89393, _index: articles, _score: 0.9741763472557068, _source: {body: gamma logs, tag: a, title: C, views: 30} } ], max_score: 0.9741763472557068, total: {relation: eq, value: 1} }, timed_out: false, took: 1 }HTTP 200。QueryNode::Bool在 ast.rs 中定义了四个子句容器must必须命中且求和计分、should至少满足minimum_should_match、must_not不得命中、不影响评分、filter必须命中、不计分并支持minimum_should_match的数值/百分比两种形式。_explanation输出es_compat.rs会递归列出must/should/filter/must_not各自的贡献方便排查每个子句对最终分数的贡献。六、prefix 与 wildcard前缀匹配与通配符匹配prefix匹配字段值以指定前缀开头的文档返回常量分数1ES 将独立书写的prefix重写为 constant_score不参与 BM25。以下查询匹配body以alp开头的文档curl -s -XPOST http://localhost:9200/articles/_search \ -H content-type: application/json \ -d {query:{prefix:{body:alp}}}命中body为alpha vector的文档_score: 1total: 1HTTP 200。QueryNode::Prefix在 ast.rs 中额外带constant_score布尔标记来自独立prefix查询时为真走 constant_score、无 BM25来自query_string/match_bool_prefix内部生成时为假保留 BM25 评分这与 ES 的重写行为逐一对齐。wildcard支持?任意单字符与*零或多个字符两种通配符。以下查询匹配body以sea开头、后面任意内容的文档curl -s -XPOST http://localhost:9200/articles/_search \ -H content-type: application/json \ -d {query:{wildcard:{body:sea*}}}命中body为one binary for search与beta search的两条文档_score: 1total: 2HTTP 200。QueryNode::Wildcard在 ast.rs 中定义了constant_score与case_insensitive两个开关后者#668只在term{case_insensitive:true}与query_string下推时置真对keyword字段折叠大小写匹配对text字段则始终按分析后的 token 语义处理该标记会被忽略。七、exists字段存在性过滤exists匹配“字段存在且值非 null”的文档常用于数据完整性筛选例如“有 title 的文档”。ES 中exists本质是ConstantScore(exists(field...))分值恒为 1curl -s -XPOST http://localhost:9200/articles/_search \ -H content-type: application/json \ -d {query:{exists:{field:title}}}响应命中全部 5 条文档_score: 1total: 5HTTP 200took: 0。QueryNode::Exists在 ast.rs 中只携带字段名是最轻量的叶子查询之一。兼容性测试 160_exists_query.yml 对exists做了极其系统的回归覆盖 binary、boolean、date、geo_point、ip、keyword、byte、double、float、half_float、integer、long、short、object含 inner 字段、text 等全部映射类型还覆盖_id、_index、_routing、_seq_no、_source、_version等元数据字段以及未映射字段unmapped上exists必须诚实地返回 0 命中。这套 YAML 由 es_api_compat_test.py 驱动执行是 XERJ 与 ES 行为对齐的活文档。八、highlight sort _source结果呈现三件套把match、highlight、sort、_source组合在同一次请求中即可一次性完成“检索 高亮 排序 字段投影”。下面的查询在body中检索vector对命中片段做高亮按views降序排列并且只返回title与views两个字段curl -s -XPOST http://localhost:9200/articles/_search \ -H content-type: application/json \ -d {query:{match:{body:vector}},highlight:{fields:{body:{}}},sort:[{views:desc}],_source:[title,views]}{ _shards: {failed: 0, skipped: 0, successful: 1, total: 1}, hits: { hits: [ { _id: a42a64ea-0f2c-44b2-b4af-3db93b145cf3, _index: articles, _score: 0.28768211603164673, _source: {title: A, views: 10}, sort: [10] } ], max_score: null, total: {relation: eq, value: 1} }, timed_out: false, took: 1 }HTTP 200。注意三处与 ES 完全一致的细节_source被裁剪为仅含title与views未请求的字段不会出现在响应中每条命中携带sort数组值为排序字段的实际数值此处为10供客户端做翻页游标命中列表外层max_score为null——ES 的既定行为是一旦请求了sort就默认跳过评分所需的额外扫描除非显式请求track_scores因此_score虽仍返回但不再代表排序依据。从源码看_source、sort、highlight都是搜索请求体的一等公民解析结构在 es_compat.rs 中分别建模为sort: OptionValue、_source带 serde rename与highlight: OptionValuerest_total_hits_as_int参数也在请求层与响应层均受支持es_compat.rs 展示了该参数将hits.total摊平为裸整数的逻辑方便与 ES 生态中的既有客户端平滑对接。九、一次看懂查询解析链路从 DSL 到 QueryNode 再到评分把本文涉及的九类查询串起来可以看到一条清晰的实现链路全部落在engine/crates下解析层_search请求体在 es_compat.rs 中被解析为xerj_query::ast::QueryNode。词法叶子查询match、match_phrase、match_phrase_prefix、match_bool_prefix被归类为LEXICAL_TEXT_CLAUSES见 es_compat.rs这类查询按分析后的文本走 BM25term/prefix/range/exists/wildcard则被识别为精确匹配子句按各自的常量分值或边界语义执行。AST 层ast.rs 用单一QueryNode枚举统一建模结构化查询MatchAll/Bool、词级查询Term/Terms/Range/Prefix/Wildcard/Exists/Ids/Script与全文查询Match/MatchPhrase/MultiMatch并标注了各叶子节点的可选参数与兼容性注释如 #668 通配符大小写、#830 slop 换位语义、#1284 lenient 语义重写器与规划器以普通递归函数遍历这棵树。执行与评分层全文子句由 xerj-fts 执行——bm25.rs 实现与 ES 一致的 BM25 评分k11.2, b0.75search.rs 的phrase_positions_match实现短语相邻性与 LuceneSloppyPhraseMatcher语义的 slop 匹配index.rs 提供 FST 词典与 posting list 等倒排结构。验证层es-compat-yaml 下的 YAML 回归套件如160_exists_query.yml、200_index_phrase_search.yml以 ES 官方 REST 测试的书写风格验证 XERJ 在各类查询上的行为一致性。小结本速查覆盖了 ES 查询家族在 XERJ 上的九个核心成员与一种组合用法查询适用字段语义分值特征matchtext分析后全文检索BM25 求和match_phrasetext词序敏感短语命中即得分为准term/termskeyword精确等值ORBM25 词权重 / 常量range数值/日期/keyword边界过滤gte/gt/lte/lt常量 1bool mustfilter任意评分与过滤职责分离must 计分、filter 不计分prefixkeyword/text前缀匹配独立书写时为常量 1wildcardkeyword/text?/*通配独立书写时为常量 1exists任意字段字段存在性常量 1每条查询都可以直接用 curl 打到localhost:9200响应体在_shards、hits.total.relation、max_score、sort、_source等细节上与 Elasticsearch 对齐因此熟悉 ES Query DSL 的开发者“Discover muscle memory”可以零成本迁移到 XERJ。想继续深入可以接着阅读同系列的聚合分析演练、向量检索演练与分析器演练或直接翻看 ES 兼容性总览 了解完整的功能矩阵。赞分享【免费下载链接】xerjXERJ is the new way for AI to search data. Its autoindex capability activates agents to know your data without the token waste of grep and sed. One command indexes code, docs, logs and PDFs for search, RAG, security audits and agent memory, using 40x fewer tokens than grep. Elasticsearch compatible, so existing clients just work.项目地址https://gitcode.com/GitHub_Trending/xe/xerj点击查看免费下载相关推荐LanceDB 全文检索查询类型全解FullTextQueryType 枚举与 Match/Phrase/Boost/MultiMatch/Boolean 查询实战LanceDB 全文检索查询类型全解FullTextQueryType 枚举与 Match/Phrase/Boost/MultiMatch/Boolean 查数据库向量数据库全文检索人工智能elasticsearch-php 搜索操作实战指南从 Match 查询到 Bool 组合与 Scroll 深度分页elasticsearch php 搜索操作实战指南从 Match 查询到 Bool 组合与 Scroll 深度分页 本篇指南基于 elasticsearch搜索引擎后端XERJ 向量相似度搜索kNN实战dense_vector 字段、ES 兼容查询与 HNSW 近邻索引XERJ 向量相似度搜索kNN实战dense_vector 字段、ES 兼容查询与 HNSW 近邻索引 本篇技术指南围绕 XERJ 的向量相似度搜索能力展上一篇深度解析LeagueAkari如何高效集成OP.GG英雄数据从社区需求到技术实现下一篇免费开源的STL到STEP转换终极指南轻松实现3D模型格式转换创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考