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