Amazon Cloudsearch 结构化查询解析器的简单查询构建器。
项目描述
Amazon Cloudsearch 结构化查询解析器的简单查询构建器。
<nav class="contents local" id="contents" role="doc-toc"> </nav>特征
为 Amazon Cloudsearch 结构化查询解析器提供一个简单的查询构建器。
请参阅以下有关结构化搜索语法的链接。
警告
目前,该库仅与结构化搜索语法兼容。
它没有与其他查询解析器(lucene、dismax、simple)相对应的任何计划。
如果您想要 lucene 查询生成器,最好使用以下库。
此库不处理生成与结构化搜索语法无关的查询。例如)尺寸、刻面……
设置
使用 pip 创建环境:
$ pip install csquery
用法
和
语法:(和 boost=N EXPRESSION EXPRESSION ... EXPRESSIONn)
from csquery.structured import and_, field
q = and_(title='star', actors='Harrison Ford', year=('', 2000))
q() #=> (and title:'star' actors:'Harrison Ford' year:{,2000])
# with option
q = and_({'title': 'star'}, {'title': 'star2'}, boost=2)
q() #=> (and boost=2 title:'star' title:'star2')
# another writing
and_({'title': 'star'}, {'actors': 'Harrison Ford'}, {'year': ('', 2000)})
and_(field('star', 'title'), field('Harrison Ford', 'actors'), field(('', 2000), 'year'))
或者
语法:(或 boost=N EXPRESSION1 EXPRESSION2 ... EXPRESSIONn)
from csquery.structured import or_, field
q = or_(title='star', actors='Harrison Ford', year=('', 2000))
q() #=> (or title:'star' actors:'Harrison Ford' year:{,2000])
# with option
q = or_({'title': 'star'}, {'title': 'star2'}, boost=2)
q() #=> (or boost=2 title:'star' title:'star2')
不是
语法:(不是 boost=N 表达式)
from csquery.structured import not_, and_
q = not_(and_(actors='Harrison Ford', year=('', 2010)))
q() #=> (not (and actors:'Harrison Ford' year:{,2010]))
# with option
q = not_(and_(actors='Harrison Ford', year=('', 2010)), boost=2)
q() #=> (not boost=2 (and actors:'Harrison Ford' year:{,2010]))
靠近
语法:(近场=场距离=N boost=N 'STRING')
from csquery.structured import near
q = near('teenage vampire', boost=2, field='plot', distance=2)
q() #=> (near field=plot distance=2 boost=2 'teenage vampire')
短语
语法:(短语字段=FIELD boost=N 'STRING')
from csquery.structured import phrase
q = phrase('star', boost=2, field='title')
q() #=> (phrase field=title boost=2 'star')
字首
语法:(前缀字段=FIELD boost=N 'STRING')
from csquery.structured import prefix
q = prefix('star', boost=2, field='title')
q() #=> (prefix field=title boost=2 'star')
范围
语法:(范围字段=FIELD boost=N RANGE)
from csquery.structured import range_
q = range_((1990, 2000))
q() #=> (range [1990,2000])
q = range_((None, 2000))
q() #=> (range {,2000])
q = range_((1990,))
q() #=> (range [1990,})
# with opition
q = range_((1990, 2000), field='date', boost=2)
q() #=> (range field=date boost=2 [1990,2000])
# another writing
q = range_('[1990,2000]')
q() #=> (range [1990,2000])
q = range_(('', 2000))
q() #=> (range {,2000])
q = range_('{,2000]')
q() #=> (range {,2000])
q = range_((1990, None))
q() #=> (range [1990,})
q = range_((1990, ''))
q() #=> (range [1990,})
q = range_('[1990,}')
q() #=> (range [1990,})
学期
语法:(术语字段=FIELD boost=N 'STRING'|VALUE)
from csquery.structured import term
q = term(2000, field='year', boost=2)
q() #=> (term field=year boost=2 2000)
q = term('star', field='title', boost=2)
q() #=> (term field=title boost=2 'star')
复杂查询示例
from csquery.structured import and_, or_, not_, term
q = and_(
not_('test', field='genres'),
or_(
term('star', field='title', boost=2),
term('star', field='plot')
)
)
q() #=> (and (not field=genres 'test') (or (term field=title boost=2 'star') (term field=plot 'star')))
与 boto 一起使用
http://boto.readthedocs.org/en/latest/ref/cloudsearch2.html
from csquery.structured import and_
from boto.cloudsearch2.layer2 import Layer2
conn = Layer2(
region='ap-northeast-1',
aws_access_key_id=[AWS ACCESSS KEY ID],
aws_secret_access_key=[AWS SECRET KEY],
)
domain = conn.lookup('search_domain_name')
search_service = domain.get_search_service()
q = and_(title='star', actors='Harrison Ford', year=('', 2000))
result = search_service.search(q=q(), parser='structured')
Python 支持
Python 2.7、3,3、3.4 或更高版本。
执照
该库的源代码在 MIT 许可下获得许可。
有关特定条款,请参阅 LICENSE.rst 文件。
贡献者
谢谢。
@podhmo
@furi
历史
0.1.4(2015 年 11 月 25 日)
修复了不带引号的字段值。 #4。
0.1.3(2015 年 11 月 20 日)
修复了过度转义的表达式。#3。
0.1.2(2015 年 11 月 18 日)
修复了逃生错误。#2。
0.1.1(2015 年 11 月 6 日)
修复了错误。#1。
0.1.0(2015 年 6 月 8 日)
首次发布
项目详情
下载文件
下载适用于您平台的文件。如果您不确定要选择哪个,请了解有关安装包的更多信息。