node.jsのsphinxqlクエリビルダーは、typescriptに書きました。生のsphinxql文字列を常に書くことを避けて簡単なクエリを作成します。デフォルトでは、常にセキュリティで考えている逃げられたクエリパラメーターを使用します。
PHP Sphinxql-Query-BuilderとEloquent Query Builder(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サーバーの間に接続を作成する方法は2つあります。最初で最も簡単なのは、 createConnectionメソッドを使用することです。
const { Sphinxql } = require ( 'sphinxql' ) ;
const sphql = Sphinxql . createConnection ( {
host : 'localhost' ,
port : 9306
} ) ; 2番目のオプションは、 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を使用した場合は、このセクションも非常に基本的に確認できます。とにかく、このAPIの使用方法について良いアイデアを作成するために、Manticore SearchまたはSphinxのドキュメントを読むことを強くお勧めします。
ここの例:
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は簡単で、メソッドのリストは次のとおりです。
このすべての方法は、Promiseオブジェクトを返します。
トランザクションを使用して作業する簡単な例:
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 and Sphinxのドキュメントが、バッチで使用される次のステートメントのみがサポートされていると述べたように、
これは、今がコードを書く瞬間です。必要な方法だけを実装するクラス、キューがあります。マルチクエリを実行すると便利です。マルチステートメントを有効にするには、接続作成のために構成オブジェクトで指定する必要があります。
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 ) ;
} ) ; フラッシュrtindexについて読むこのステートメントを使用するには、以下の例を参照してください):
connection . getQueryBuilder ( )
. flushRTIndex ( 'my_rt_index' )
. execute ( )
. then ( ( result , fields ) => {
console . log ( result ) ;
} )
. catch ( err => {
console . log ( err ) ;
} ) ; Manticore DocumantationのTruncate 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 Documantationのリロードインデックスについて読んでください。以下の例を参照してください。
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 ) ;
} ) ;Call 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