Last active
February 24, 2021 14:34
-
-
Save fergiemcdowall/6b34f87f51dd04a9b07156044fca3fbc to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ref https://github.com/fergiemcdowall/search-index/issues/519 | |
// run | |
// > npm init | |
// > npm install search-index tape | |
// > node 519.js | |
const si = require('search-index') | |
const test = require('tape') | |
const indexName = '_519' | |
const data = [{ | |
"_id": "4KmtfEtD1sTTVz4e3MOUiRXnR4M4hP2X", | |
"idx": ["音","切实","百","音","例","么","已","车","还","做","公","此","则","土","期","时","列","同","金","特","资","半点","主题","并","当","交","复","县","产","出","权","林","史","年","学","打","达","习","速回","志","本","习","即","三十","近","方","心","图","名","高","果","技","存","现","由","四","真","市","参","部","证","育","走","始","具","按","行","变","了","件","党格","业","战","专","解","程","四世","高","断","要","单子","法","法","查","历","位","表","书","石","百","对","部","程","义","在","今","展","权","本","矿","了","易","代","意","器","何","约","始","长","太","七","目","济","如","委","京","和","严","儿","况","拉","者","周","应","应","音","样","内","造","存"] | |
}, { | |
"_id": "S0iYuV3mj1Up6u1vKPor0q4K6lJgvw3p", | |
"idx": ["边","次","办","解度","总","布","圆","长","争","五","电","界","算","听","广","族","须","太","性","那","京","统","已","工用","产","广","素","华","采","进","它","行","决","量","个","布","技","张","身","极","农","选","队","斯","解","消","会","美","收","越","把","专美","场","器","验","而","步","极","千","名","民","完","运八","学","局","才","去","只","分数","始","华","家","特","且","改","等","低","发","江","己","心","温表","自","万","体","次","开车","红","广","记","达","但","亲","约","能","心","值","对","如","议","世","基","开","是","律","经理","权","主","头","派"] | |
}, { | |
"_id": "qZ36wBEIniK1drbRB2mpFA1BmwxATA5A", | |
"idx": ["件","用","备","片","或","林","院","育","导","工","术","完","门","合","红","各","具","本","九","光点","平","选","者","片","各","中","们","走","者","据","角","美国","断","两","时","高","列","相","据","米","员","打","科","在","计","究","进","使","发","基","别题","在","变","放","位","而","内","流","资","单易","务","法","领","被","着","单","规","类","路","三","对","制","育","始","想","农","特","律","计划","次","真","去","始","力","号","持","多","委","节","没准","系","老","由","识","办","和","元","斯","军","支","身","张","共","收","新","家","到","政","经过","百","边","管","定","开","住","即","江"] | |
}] | |
test('create a search index', t => { | |
t.plan(1) | |
si({ | |
name: indexName, | |
storeVectors: true | |
}).then(db => { | |
global[indexName] = db | |
t.pass('ok') | |
}) | |
}) | |
test('can add some data', t => { | |
const { PUT } = global[indexName] | |
t.plan(1) | |
PUT(data).then(() => { | |
t.pass('ok') | |
}) | |
}) | |
test('can search for 音 and 切实', t => { | |
const { QUERY } = global[indexName] | |
t.plan(1) | |
QUERY({ | |
SEARCH: [ '音', '切实' ] | |
}).then(result => t.deepEquals(result, { | |
"RESULT": [ | |
{ | |
"_id": "4KmtfEtD1sTTVz4e3MOUiRXnR4M4hP2X", | |
"_match": [ | |
"idx:音#1.00", | |
"idx:切实#0.33" | |
], | |
"_score": 1.84 | |
} | |
], | |
"RESULT_LENGTH": 1 | |
})) | |
}) | |
test('dump all documents', t => { | |
const { ALL_DOCUMENTS } = global[indexName] | |
t.plan(1) | |
ALL_DOCUMENTS().then(docs => { | |
console.log(JSON.stringify(docs, null, 2)) | |
t.deepEquals(docs.map(d => d._doc), data) | |
}) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment