MongoDB-텍스트 검색

버전 2.4부터 MongoDB는 문자열 콘텐츠 내부 검색을위한 텍스트 인덱스 지원을 시작했습니다. 그만큼Text Search 형태소 분석 기술을 사용하여 다음과 같은 형태소 중지 단어를 삭제하여 문자열 필드에서 지정된 단어를 찾습니다. a, an, the, 현재 MongoDB는 약 15 개의 언어를 지원합니다.

텍스트 검색 활성화

처음에는 텍스트 검색이 실험적 기능 이었지만 버전 2.6부터는 구성이 기본적으로 활성화되어 있습니다.

텍스트 인덱스 생성

아래의 다음 문서를 고려하십시오. posts 게시물 텍스트와 태그를 포함하는 컬렉션-

> db.posts.insert({
   "post_text": "enjoy the mongodb articles on tutorialspoint",
   "tags": ["mongodb", "tutorialspoint"]
}
{
	"post_text" : "writing tutorials on mongodb",
	"tags" : [ "mongodb", "tutorial" ]
})
WriteResult({ "nInserted" : 1 })

post_text 필드에 텍스트 인덱스를 생성하여 게시물의 텍스트 내부를 검색 할 수 있습니다.

>db.posts.createIndex({post_text:"text"})
{
	"createdCollectionAutomatically" : true,
	"numIndexesBefore" : 1,
	"numIndexesAfter" : 2,
	"ok" : 1
}

텍스트 인덱스 사용

이제 post_text 필드에 텍스트 인덱스를 만들었으므로 단어가있는 모든 게시물을 검색합니다. tutorialspoint 그들의 텍스트에서.

> db.posts.find({$text:{$search:"tutorialspoint"}}).pretty()
{
	"_id" : ObjectId("5dd7ce28f1dd4583e7103fe0"),
	"post_text" : "enjoy the mongodb articles on tutorialspoint",
	"tags" : [
		"mongodb",
		"tutorialspoint"
	]
}

위의 명령은 다음과 같은 결과 문서를 반환했습니다. tutorialspoint 포스트 텍스트에서-

{ 
   "_id" : ObjectId("53493d14d852429c10000002"), 
   "post_text" : "enjoy the mongodb articles on tutorialspoint", 
   "tags" : [ "mongodb", "tutorialspoint" ]
}

텍스트 인덱스 삭제

기존 텍스트 인덱스를 삭제하려면 먼저 다음 쿼리를 사용하여 인덱스 이름을 찾으십시오.

>db.posts.getIndexes()
[
	{
		"v" : 2,
		"key" : {
			"_id" : 1
		},
		"name" : "_id_",
		"ns" : "mydb.posts"
	},
	{
		"v" : 2,
		"key" : {
			"fts" : "text",
			"ftsx" : 1
		},
		"name" : "post_text_text",
		"ns" : "mydb.posts",
		"weights" : {
			"post_text" : 1
		},
		"default_language" : "english",
		"language_override" : "language",
		"textIndexVersion" : 3
	}
]
>

위 쿼리에서 색인 이름을 가져온 후 다음 명령을 실행합니다. 여기,post_text_text 색인의 이름입니다.

>db.posts.dropIndex("post_text_text")
{ "nIndexesWas" : 2, "ok" : 1 }