mongodb handles Chinese indexing and search string detail

  • 2020-06-15 10:26:16
  • OfStack

reference

First of all, Chinese indexing has been supported since version 3.2. All supported languages are referenced here:

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

Then, text index needs to be suggested for the table to support indexes, how to set up the reference here:

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

After the index text is built, if the reference is retrieved:

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

The instance

I have a table defined as follows:


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]
 });

Then, there is data in it, so I directly retrieve it and get the result:


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

Note that text index is not recommended, then build 1:


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

See the results:


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

Here's the result. It worked


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

Then I started searching:


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

Nothing.

I retrieve 1 existing Chinese:


> db.articles.find( { $text: { $search: " operation " } } )
{ "_id" : ObjectId("58b0eb5a136dc51b541eaf81"), "user" : ObjectId("589c8d22f7d9dc15989be255"), "comments" : [ ], "toPublish" : true, "category" : "blog", "content" : "<p> </p><p><br/></p><p> It's really about using ubuntu 16 , please refer to the website: </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> My operation steps: </p><pre>1. Pour into key : sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv EA312927&#10;</pre><p><br/></p><p>2.  create mongodb Source of software: </p><p>/etc/apt/sources.list.d/mongodb-org-3.2.list</p><p> Operation: </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.  Update system: </p><p>sudo apt update</p><p> Then look at all the software that can be updated: </p><p>sudo apt list --upgradable</p><p> Then update all the software: </p><p>sudo apt upgrade</p><p><br/></p><p>4.  The installation mongodb  : </p><p>sudo apt install -y mongodb-org</p><p> Version installation can also be specified: </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> But I don't use it that way. </p><p><br/></p><p>5.  add systemd  Self-starting entry: </p><p>sudo vim /lib/systemd/system/mongod.service</p><p> Add Content: </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.  Enable and enable services: </h2><p>sudo systemctl enable mongod.service</p><p>sudo systemctl start mongod.service</p><p><br/></p><h2> Check the status, 1 cut ok . </h2><p>sudo systemctl status mongod.service</p><p> low  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.  Check to see if the service startup port is available 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" : " It's really about using ubuntu 16 , please refer to the website: ", "title" : " It's really about using ubuntu 16 , please refer to the website: ", "created" : ISODate("2017-02-25T02:26:34.483Z"), "__v" : 0 }
>

The last

However, this kind of retrieval is not perfect, if you need better support, you need to refer to:

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

Installing rlp supports mongodb to retrieve Chinese, but it's not free...

So, the best approach is to synchronize mongodb using Elastic Search and then retrieve it, which is beyond the scope of this article and will be discussed later.

conclusion


Related articles: