国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 數(shù)據(jù)庫 > MongoDB > 正文

mongodb處理中文索引與查找字符串詳解

2020-10-29 18:47:09
字體:
供稿:網(wǎng)友

參考文獻(xiàn)

首先自打3.2版本之后,就開始支持中文索引了,支持的所有的語言參考這里:

https://docs.mongodb.com/manual/reference/text-search-languages/

然后,對于要支持索引的表需要建議text index,如何建立參考這里:

https://docs.mongodb.com/manual/core/index-text/

在建好索引text之后,如果檢索參考:

https://docs.mongodb.com/manual/reference/operator/query/text/

實例

我有一個表,定義如下:

var ArticleSchema = new Schema({ created: { type: Date, default: Date.now }, title: { type: String, default: '', trim: true, required: 'Title cannot be blank' }, abstract: { type: String, default: '', trim: true }, abstractImg: { type: String, default: 'http://www.doocr.com/modules/core/client/img/brand/font-ocr.png', trim: true }, content: { type: String, default: '', trim: true }, category: { type: String, default: 'news', trim: true }, user: { type: Schema.ObjectId, ref: 'User' }, toPublish: { type: Boolean, default: true }, comments: [CommentSchema] });

然后,里面有數(shù)據(jù)的,所以我直接檢索,得到結(jié)果:

> db.articles.find( { $text: { $search: "coffee" } } )Error: error: { "waitedMS" : NumberLong(0), "ok" : 0, "errmsg" : "text index required for $text query", "code" : 27}

說明沒有建議text索引,那么建一個:

db.articles.createIndex( {title: "text", content: "text" })

看看效果:

> db.articles.createIndex(... {... title: "text",... content: "text"... }... )

下面是結(jié)果,成功了

{ "createdCollectionAutomatically" : false, "numIndexesBefore" : 1, "numIndexesAfter" : 2, "ok" : 1}

然后我開始檢索:

> db.articles.find( { $text: { $search: "coffee" } } )

什么都沒有。

我檢索一個存在的中文:

> db.articles.find( { $text: { $search: "操作" } } ){ "_id" : ObjectId("58b0eb5a136dc51b541eaf81"), "user" : ObjectId("589c8d22f7d9dc15989be255"), "comments" : [ ], "toPublish" : true, "category" : "blog", "content" : "<p> </p><p><br/></p><p>其實就是使用ubuntu 16的安裝方式,參考網(wǎng)址:</p><p><a href=/"https://docs.mongodb.com/master/tutorial/install-mongodb-on-ubuntu//" target=/"_blank/">https://docs.mongodb.com/master/tutorial/install-mongodb-on-ubuntu/</a></p><p><br/></p><p>我的操作步驟:</p><pre>1.倒入key:sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv EA312927&#10;</pre><p><br/></p><p>2. 創(chuàng)建mongodb的軟件源:</p><p>/etc/apt/sources.list.d/mongodb-org-3.2.list</p><p>操作:</p><pre>echo &#34;deb http://repo.mongodb.org/apt/ubuntu xenial/mongodb-org/3.2 multiverse&#34; | sudo tee /etc/apt/sources.list.d/mongodb-org-3.2.list&#10;</pre><p><br/></p><p>3. 更新系統(tǒng):</p><p>sudo apt update</p><p>之后查看所有可以更新的軟件:</p><p>sudo apt list --upgradable</p><p>然后升級所有軟件:</p><p>sudo apt upgrade</p><p><br/></p><p>4. 安裝mongodb :</p><p>sudo apt install -y mongodb-org</p><p>也可以指定版本安裝:</p><p>sudo apt-get install -y mongodb-org=3.2.8 mongodb-org-server=3.2.8 mongodb-org-shell=3.2.8 mongodb-org-mongos=3.2.8 mongodb-org-tools=3.2.8</p><p>不過我不使用這種方式。</p><p><br/></p><p>5. 添加systemd 自啟動條目:</p><p>sudo vim /lib/systemd/system/mongod.service</p><p>添加內(nèi)容:</p><p>[Unit]</p><p>Description=High-performance, schema-free document-oriented database</p><p>After=network.target</p><p>Documentation=https://docs.mongodb.org/manual</p><p><br/></p><p>[Service]</p><p>User=mongodb</p><p>Group=mongodb</p><p>ExecStart=/usr/bin/mongod --quiet --config /etc/mongod.conf</p><p><br/></p><p>[Install]</p><p>WantedBy=multi-user.target</p><p><br/></p><h2>6. 使能和啟動服務(wù):</h2><p>sudo systemctl enable mongod.service</p><p>sudo systemctl start mongod.service</p><p><br/></p><h2>查看狀態(tài),一切ok。</h2><p>sudo systemctl status mongod.service</p><p>● mongod.service - High-performance, schema-free document-oriented database</p><p>Loaded: loaded (/lib/systemd/system/mongod.service; enabled; vendor preset: enabled)</p><p>Active: active (running) since Sun 2016-07-31 21:59:00 CST; 13min ago</p><p>Docs: https://docs.mongodb.org/manual</p><p>Main PID: 19374 (mongod)</p><p>CGroup: /system.slice/mongod.service</p><p>└─19374 /usr/bin/mongod --quiet --config /etc/mongod.conf</p><p><br/></p><p>Jul 31 21:59:00 mint systemd[1]: Started High-performance, schema-free document-oriented database.</p><p><br/></p><p>7. 查看服務(wù)啟動端口是否ok:</p><p>azuo1228@mint ~/webproj/mjs2/meanjs $ netstat -apn | grep mong</p><p>(Not all processes could be identified, non-owned process info</p><p>will not be shown, you would have to be root to see it all.)</p><p>unix 2 [ ACC ] STREAM LISTENING 76731 - /tmp/mongodb-27017.sock</p><p><br/></p><p><br/></p>", "abstractImg" : "http://www.doocr.com/modules/core/client/img/brand/font-ocr.png", "abstract" : "其實就是使用ubuntu 16的安裝方式,參考網(wǎng)址:", "title" : "其實就是使用ubuntu 16的安裝方式,參考網(wǎng)址:", "created" : ISODate("2017-02-25T02:26:34.483Z"), "__v" : 0 }>

最后

但是這種檢索都是不完美的,如果需要更好的支持,就需要參考:

https://docs.mongodb.com/manual/tutorial/text-search-with-rlp/

安裝rlp支持mongodb檢索中文,但是不是免費的。。。

所以,最好的方式就是使用Elastic Search同步mongodb,然后通過它檢索,超出本文范圍了,以后再說。

總結(jié)

以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作能帶來一定的幫助,如果有疑問大家可以留言交流,謝謝大家對武林網(wǎng)的支持。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 江北区| 大田县| 江口县| 剑川县| 同德县| 云林县| 额尔古纳市| 方正县| 宜宾县| 玉屏| 扶余县| 桂东县| 丹巴县| 盐津县| 龙川县| 阳新县| 九江市| 衡阳市| 东海县| 宜宾县| 庄浪县| 北宁市| 贵港市| 东宁县| 威信县| 鄂托克旗| 泸水县| 东兰县| 新乡市| 岳阳县| 宿州市| 五寨县| 唐山市| 马公市| 衢州市| 甘泉县| 怀安县| 满洲里市| 凤城市| 泗洪县| 梅河口市|