node.js的sphinxql查询构建器在打字稿中写入。简单地查询避免写下您可以写的原始sphinxql字符串。默认情况下,它使用逃逸的查询参数,始终在安全性中思考。
它受到PHP phhinxql-Query-Buerder和雄辩的查询构建器的启发(Laravel Framework Orm)
用于创建连接的客户端是MySQL2,它专注于性能。
您必须使用node.js> = 6.x
只需运行NPM命令:
npm install --save sphinxql要创建一个简单的连接(不是最建议的连接,请使用池连接)并编写您的第一个查询,只需执行此操作:
const { Sphinxql , Expression } = require ( 'sphinxql' ) ;
const sphql = Sphinxql . createConnection ( {
host : 'localhost' ,
port : 9306
} ) ;
sphql . getQueryBuilder ( )
. select ( '*' )
. from ( 'books' )
. match ( 'title' , 'harry potter' )
. where ( 'created_at' , '<' , Expression . raw ( 'YEAR()' ) )
. between ( Expression . raw ( `YEAR(created_at)` ) , 2014 , 2019 )
. orderBy ( { 'date_published' : 'ASC' , 'price' : 'DESC' } )
. limit ( 10 )
. execute ( )
. then ( ( result , fields ) => {
console . log ( result ) ;
} )
. catch ( err => {
console . log ( err ) ;
} ) ;在您的应用程序和Manticore/Sphinx服务器之间创建连接有两种可能的方法。首先,最简单的是使用createConnection方法。
const { Sphinxql } = require ( 'sphinxql' ) ;
const sphql = Sphinxql . createConnection ( {
host : 'localhost' ,
port : 9306
} ) ;第二个选项是使用createPoolConnection方法。这种方法使您可以通过Manticore/sphinx重用以前的连接具有多个开放连接。要了解有关MySQL2连接池的更多信息(允许使用池的创建和配置参数)读取有关使用连接池的MySQL2文档。该技术使用更多的内存,因此请注意。
const { Sphinxql } = require ( 'sphinxql' ) ;
// Create the connection pool. The pool-specific settings are the defaults
const sphql = Sphinxql . createPoolConnection ( {
host : 'localhost' ,
port : 9306 ,
waitForConnections : true ,
connectionLimit : 10 ,
queueLimit : 0
} ) ;本节在许多部分中分开,但是如果您之前使用过sphinxql或SQL,则可以看到此部分也非常基本。无论如何,我强烈建议您阅读Manticore搜索或狮身人面像文档,以便对如何使用此API有一个很好的了解。
示例在这里:
sphql . getQueryBuilder ( )
. select ( 'id' , 'author_id' , 'publication_date' )
. from ( 'books' )
. match ( '*' , '"harry potter"' , false )
. whereIn ( 'lang' , [ 'en' , 'sp' , 'fr' ] )
. between ( Expression . raw ( `YEAR(publication_date)` ) , 2008 , 2015 )
. execute ( )
. then ( ( result , fields ) => {
console . log ( result ) ;
} )
. catch ( err => {
console . log ( err ) ;
} ) ; 您可以使用方法“选项”链多个选项。方法头是:
带有选项的示例:
sphql . getQueryBuilder ( )
. select ( 'id' , 'author_id' , 'publication_date' )
. from ( 'books' )
. match ( '*' , '"harry potter"' , false )
. between ( Expression . raw ( `YEAR(publication_date)` ) , 2008 , 2015 )
. orderBy ( { 'publication_date' : 'ASC' , 'price' : 'DESC' } )
. limit ( 10 )
. option ( 'rank_fields' , 'title content' )
. option ( 'field_weights' , { title : 100 , content : 1 } )
. execute ( )
. then ( ( result , fields ) => {
console . log ( result ) ;
} )
. catch ( err => {
console . log ( err ) ;
} ) ; // todo
这样创建了一个插入语句:
const document = {
id : 1 ,
content : 'this is the first post for the blog...' ,
title : 'First post'
} ;
connection . getQueryBuilder ( )
. insert ( 'my_rtindex' , document )
. execute ( )
. then ( ( result , fields ) => {
console . log ( result ) ;
} )
. catch ( err => {
console . log ( err ) ;
} ) ;或使用键值对数组将多个值插入在同一查询中
const document = [ {
id : 1 ,
content : 'this is the first post for the blog...' ,
title : 'First post'
} , {
id : 2 ,
content : 'this is the second post for the blog...' ,
title : 'Second post'
} ] ;
connection . getQueryBuilder ( )
. insert ( 'my_rtindex' , document )
. execute ( )
. then ( ( result ) => {
console . log ( result ) ;
} )
. catch ( err => {
console . log ( err ) ;
} ) ;使用文档ID或插入替换文档。类似于插入语句仅更改替换的插入。
const document = {
id : 1 ,
content : 'this is the first post for the blog...' ,
title : 'UPDATE! First post'
} ;
connection . getQueryBuilder ( )
. replace ( 'my_rtindex' , document )
. execute ( )
. then ( ( result ) => {
console . log ( result ) ;
} )
. catch ( err => {
console . log ( err ) ;
} ) ; const document = {
content : 'UPDATE! it's an old post. this is the first post for the blog...' ,
title : 'First post (edit)'
} ;
connection . getQueryBuilder ( )
. update ( 'my_rtindex' )
. set ( document )
. match ( 'fullname' , 'John' )
. where ( 'salary' , '<' , 3000 )
. execute ( )
. then ( ( result , fields ) => {
console . log ( result ) ;
} )
. catch ( err => {
console . log ( err ) ;
} ) ;该软件包还带有对交易的支持。请记住,交易仅适用于RT索引。有关更多信息,请访问交易文档以进行Manticore搜索。
交易API很简单,下面的方法列表在这里:
所有这些方法都返回承诺对象。
一个简单的与交易一起工作的示例:
const document = {
id : 1 ,
content : 'this is the first post for the blog...' ,
title : 'First post'
} ;
const insertDocumentAndCommit = async ( doc ) => {
await connection . getQueryBuilder ( ) . transaction . begin ( ) ;
connection . getQueryBuilder ( )
. insert ( 'my_rtindex' , doc )
. execute ( )
. then ( ( result , fields ) => {
console . log ( result ) ;
} )
. catch ( err => {
console . log ( err ) ;
} ) ;
await connection . getQueryBuilder ( ) . transaction . commit ( ) ;
return true ;
}
insertDocumentAndCommit ( document ) ;首先,您需要知道Manticore/sphinx中多查询的局限性。正如Manticore Search和Sphinx文档所说,仅对批处理中使用的以下陈述有所支持:
这么说,现在是编写代码的时刻。有一个类,排队,仅实现了必要的方法,它对运行多查询很有用。要启用多语句,您必须在配置对象中指定连接创建乘数:true如下:
const { Sphinxql } = require ( 'sphinxql' ) ;
const sphql = Sphinxql . createConnection ( {
host : 'localhost' ,
port : 9306 ,
multipleStatements : true
} ) ;现在,让我们创建一个队列并处理它:
const { Queue , Sphinxql } = require ( 'sphinxql' ) ;
const sphql = Sphinxql . createConnection ( {
host : 'localhost' ,
port : 9306 ,
multipleStatements : true
} ) ;
const queue = new Queue ( sphql . getConnection ( ) ) ;
queue
. push ( sphql . getQueryBuilder ( ) . select ( '*' ) . from ( 'rt' ) . where ( 'id' , '=' , 1 ) )
. push (
sphql . getQueryBuilder ( )
. select ( 'id' , 'author_id' , 'publication_date' )
. from ( 'books' )
. match ( '*' , '"harry potter"' , false )
) ;
queue . process ( )
. then ( results => {
console . log ( results . results . length ) // 2
} )
. catch ( err => console . log ( err ) ) ;阅读有关Manticore Documantation中的附加索引的有关此陈述中的有关索引的信息,请参见下面的示例:
connection . getQueryBuilder ( )
. attachIndex ( 'my_disk_index' )
. to ( 'my_rt_index' )
. withTruncate ( ) // this method is optional
. execute ( )
. then ( ( result , fields ) => {
console . log ( result ) ;
} )
. catch ( err => {
console . log ( err ) ;
} ) ; 阅读有关使用此语句的Flush Rtindex的信息,请参见下面的示例):
connection . getQueryBuilder ( )
. flushRTIndex ( 'my_rt_index' )
. execute ( )
. then ( ( result , fields ) => {
console . log ( result ) ;
} )
. catch ( err => {
console . log ( err ) ;
} ) ; 在Manticore文档中阅读有关截短的rtIndex以使用此语句,请参见下面的示例:
connection . getQueryBuilder ( )
. truncate ( 'my_rt_index' )
. withReconfigure ( ) // this method is optional
. execute ( )
. then ( ( result , fields ) => {
console . log ( result ) ;
} )
. catch ( err => {
console . log ( err ) ;
} ) ; 阅读有关Manticore文档中的重新加载索引以使用此说明,请参见下面的示例:
connection . getQueryBuilder ( )
. reloadIndex ( 'my_index' )
. from ( '/home/mighty/new_index_files' ) // this method is optional
. then ( ( result , fields ) => {
console . log ( result ) ;
} )
. catch ( err => {
console . log ( err ) ;
} ) ;使用呼叫GetQueryBuilder方法可用的查询方法运行原始查询。此方法允许使用A的准备好的语句? (问号)您想在哪里逃脱价值。
connection . getQueryBuilder ( )
. query ( `SELECT * FROM sales WHERE MATCH(@title "italian lamp") AND tags IN (?, ?)` , [ 'home' , 'italian style' ] )
. then ( ( result , fields ) => {
console . log ( result ) ;
} )
. catch ( err => {
console . log ( err ) ;
} ) ; 所有语句都有一种最终方法,用于内部用于执行查询。此方法可使用generate()在外部使用,并返回带有最终查询的字符串。
const sphinxqlQuery = connection . getQueryBuilder ( )
. select ( 'user_id' , 'product_id' , Expression . raw ( 'SUM(product_price) as total' ) . getExpression ( ) )
. from ( 'rt_sales' )
. facet ( ( f ) => {
return f
. fields ( [ 'category_id' ] )
. by ( [ 'category_id' ] )
} )
. facet ( ( f ) => {
return f
. field ( 'brand_id' )
. orderBy ( Expression . raw ( 'facet()' ) )
. limit ( 5 )
} )
. generate ( ) ;
console . log ( sphinxqlQuery ) ; // SELECT user_id, product_id, SUM(product_price) as total FROM rt_sales FACET category_id BY category_id FACET brand_id ORDER BY facet() DESC LIMIT 5