SPHINXQL BUILDER สำหรับ Node.js เขียนใน TypeScript ทำให้การสืบค้นง่าย ๆ หลีกเลี่ยงการเขียนสตริง Raw Sphinxql ที่คุณสามารถทำได้เสมอ โดยค่าเริ่มต้นจะใช้พารามิเตอร์การสืบค้นที่หลบหนีโดยคิดในความปลอดภัยเสมอ
มันได้รับแรงบันดาลใจอย่างมากใน PHP Sphinxql-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 อันดับแรกและง่ายที่สุดคือการใช้วิธี 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 ) ;
} ) ;แทนที่เอกสารโดยใช้ DOC 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 และสฟิงซ์กล่าวว่ามีเพียงการสนับสนุนสำหรับข้อความต่อไปนี้ที่ใช้ในแบทช์เท่านั้น:
กล่าวว่าตอนนี้เป็นช่วงเวลาที่จะเขียนโค้ด มีคลาส คิว ที่ใช้เพียงวิธีการที่จำเป็นมันมีประโยชน์ในการเรียกใช้แบบสอบถามหลายแบบ ในการเปิดใช้งานหลายข้อความคุณต้องระบุในวัตถุการกำหนดค่าของคุณสำหรับการสร้างการเชื่อม ต่อหลายรายการ: จริง ดังต่อไปนี้:
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 ) ) ;อ่านเกี่ยวกับดัชนีแนบใน documantation Manticore เพื่อใช้คำสั่งนี้ดูตัวอย่างด้านล่าง:
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 ) ;
} ) ; อ่านเกี่ยวกับการตัดทอน rtindex ใน manticore documantation เพื่อใช้คำสั่งนี้ดูตัวอย่างด้านล่าง:
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 ) ;
} ) ;เรียกใช้การสืบค้นแบบดิบโดยใช้วิธีการสืบค้นที่มีอยู่หลังจากการโทรหาวิธี 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