Lucene源码分析 - queryparser > flexible

Flexible

This project contains the new Lucene query parser implementation, which matches the syntax of the core QueryParser but offers a more modular architecture to enable customization.

This project contains the new Lucene query parser implementation,
which matches the syntax
of the core QueryParser
but offers a more modular architecture
to enable customization.

包含新Lucene查询解析实现,匹配core QueryParser语法,在自定义方面,结构更加模块化。

It's currently divided in 2 main packages:
分布在2个包中:
org.apache.lucene.queryparser.flexible.core
包含query parser api类,可以被query parser 实现扩展。

org.apache.lucene.queryparser.flexible.standard
包new query parser api实现的Lucene query parser 实现。

{@link org.apache.lucene.queryparser.flexible.core}: it contains the query parser API classes, which should be extended by query parser implementations.

{@link org.apache.lucene.queryparser.flexible.standard}: it contains the current Lucene query parser implementation using the new query parser API.

Features

  1. Full support for boolean logic (not enabled)
    完全支持boolean 逻辑

  2. ueryNode Trees - support for several syntaxes, that can be converted into similar syntax QueryNode trees.
    支持几种语法,可以转换成相似的语法 QueryNode 树。

  3. QueryNode Processors - Optimize, validate, rewrite the QueryNode trees
    QueryNode Processors - 优化,验证,rewrite QueryNode 树。

  4. Processors Pipelines - Select your favorite Processor and build a processor pipeline, to implement the features you need
    Pipelines处理器,选择你最喜欢的处理器,构建一个pipeline处理器,来实现你需要的特性。

  5. Config Interfaces - Allow the consumer of the Query Parser to implement a diff Config Handler Objects to suite their needs.
    配置接口,允许Query Parser的consumer来实现一个diff 配置处理对象来

  6. Standard Builders - convert QueryNode's into several lucene representations. Supported conversion is using a 2.4 compatible logic
    独立的builders -

  7. QueryNode tree's can be converted to a lucene 2.4 syntax string, using toQueryString

Design

This new query parser was designed to have very generic architecture, so that it can be easily used for different products with varying query syntaxes. This code is much more flexible and extensible than the Lucene query parser in 2.4.X.

1、新的query parser 被设计有一种通用的结构
2、很容易被用在不同的产品上
3、比起2.4.x中的Lucene query parser 扩展性和灵活性更好

The new query parser goal is to separate syntax and semantics of a query. E.g. 'a AND b', '+a +b', 'AND(a,b)' could be different syntaxes for the same query. It distinguishes the semantics of the different query components, e.g. whether and how to tokenize/lemmatize/normalize the different terms or which Query objects to create for the terms. It allows to write a parser with a new syntax, while reusing the underlying semantics, as quickly as possible.

The query parser has three layers and its core is what we call the QueryNode tree. It is a tree that initially represents the syntax of the original query, e.g. for 'a AND b':

  AND
 /   \
A     B

The three layers are:

QueryParser

  • This layer is the text parsing layer which simply transforms the query text string into a {@link org.apache.lucene.queryparser.flexible.core.nodes.QueryNode} tree. Every text parser must implement the interface {@link org.apache.lucene.queryparser.flexible.core.parser.SyntaxParser}. Lucene default implementations implements it using JavaCC.

QueryNodeProcessor

  • The query node processors do most of the work. It is in fact a configurable chain of processors. Each processors can walk the tree and modify nodes or even the tree's structure. That makes it possible to e.g. do query optimization before the query is executed or to tokenize terms.

QueryBuilder:

  • The third layer is a configurable map of builders, which map {@link org.apache.lucene.queryparser.flexible.core.nodes.QueryNode} types to its specific builder that will transform the QueryNode into Lucene Query object.

Furthermore, the query parser uses flexible configuration objects. It also uses message classes that allow to attach resource bundles. This makes it possible to translate messages, which is an important feature of a query parser.

This design allows to develop different query syntaxes very quickly.

StandardQueryParser and QueryParserWrapper

The classic Lucene query parser is located under {@link org.apache.lucene.queryparser.classic}.

To make it simpler to use the new query parser the class {@link org.apache.lucene.queryparser.flexible.standard.StandardQueryParser} may be helpful, specially for people that do not want to extend the Query Parser. It uses the default Lucene query processors, text parser and builders, so you don't need to worry about dealing with those. {@link org.apache.lucene.queryparser.flexible.standard.StandardQueryParser} usage:

  StandardQueryParser qpHelper = new StandardQueryParser();
  StandardQueryConfigHandler config =  qpHelper.getQueryConfigHandler();
  config.setAllowLeadingWildcard(true);
  config.setAnalyzer(new WhitespaceAnalyzer());
  Query query = qpHelper.parse("apache AND lucene", "defaultField");
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 213,928评论 6 493
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 91,192评论 3 387
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 159,468评论 0 349
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 57,186评论 1 286
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 66,295评论 6 386
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 50,374评论 1 292
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,403评论 3 412
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,186评论 0 269
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,610评论 1 306
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 36,906评论 2 328
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,075评论 1 341
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 34,755评论 4 337
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 40,393评论 3 320
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,079评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,313评论 1 267
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 46,934评论 2 365
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 43,963评论 2 351

推荐阅读更多精彩内容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi阅读 7,319评论 0 10
  • 天气开始变得寒冷起来,在这个逐渐熟悉的城市,有些记忆会使人温暖起来。有时候会想想曾经熟悉的人和事。比如:老家的...
    3个橘子阅读 339评论 0 0
  • 今天八月十五中秋节,月圆人圆家团圆,月到中秋分外明! 今年中秋,最开心的莫过于儿子回家过节,虽然回家的路上,有很多...
    惠芝阅读 481评论 0 1
  • 我去看这部电影原因有二:一是帅气的抖森先生,二是帅气的汤先生。抖森先生据说是位既绅士又蠢萌的学霸,汤先生是动作片拥...
    大月123阅读 819评论 0 2
  • 2017年国庆长假期间,有幸拜读了周国平老师论阅读《做大师的学生》,让我荣幸的站在大师肩膀上阅读,对读书的...
    走在飘香的麦埂上阅读 389评论 0 0