sql >> データベース >  >> NoSQL >> MongoDB

MongoDBで全文検索を行う方法

    主要なNoSQLデータベースの1つであるMongoDBは、その高速なパフォーマンス、用途の広いスキーマ、スケーラビリティ、およびインデックス作成のための優れた機能でよく知られています。詳細に入る前に、いくつかのコンテキストを見てみましょう。全文検索は、インターネットでコンテンツを見つけることについて話すときに不可欠な機能です。フレーズやキーワードを使用してコンテンツを表示する場合は、Google検索がその最良の例です。この記事では、テキストインデックスに基づくMongoDBの全文検索機能について学習します。

    サンプルデータベースを作成する

    始める前に、チュートリアルで使用するサンプルデータベースを作成します。

    myDBという名前のデータベースを作成します booksという名前のコレクションを作成します 。この場合、ステートメントは次のようになります。

    > use myDB
    > db.createCollection("books")
    >

    次のステートメントを使用して、いくつかのドキュメントを挿入しましょう。

    > db.books.insert([
    	{
          "title": "Eloquent JavaScript, Second Edition",
          "subtitle": "A Modern Introduction to Programming",
          "author": "Marijn Haverbeke",
          "publisher": "No Starch Press",
          "description": "JavaScript lies at the heart of almost every modern web application, from social apps to the newest browser-based games. Though simple for beginners to pick up and play with, JavaScript is a flexible, complex language that you can use to build full-scale applications."
        },
        {
          "title": "Learning JavaScript Design Patterns",
          "subtitle": "A JavaScript and jQuery Developer's Guide",
          "author": "Addy Osmani",
          "publisher": "O'Reilly Media",
          "description": "With Learning JavaScript Design Patterns, you'll learn how to write beautiful, structured, and maintainable JavaScript by applying classical and modern design patterns to the language. If you want to keep your code efficient, more manageable, and up-to-date with the latest best practices, this book is for you."
        },
        {
          "title": "Speaking JavaScript",
          "subtitle": "An In-Depth Guide for Programmers",
          "author": "Axel Rauschmayer",
          "publisher": "O'Reilly Media",
          "description": "Like it or not, JavaScript is everywhere these days, from browser to server to mobile and now you, too, need to learn the language or dive deeper than you have. This concise book guides you into and through JavaScript, written by a veteran programmer who once found himself in the same position."
        },
        {
          "title": "Programming JavaScript Applications",
          "subtitle": "Robust Web Architecture with Node, HTML5, and Modern JS Libraries",
          "author": "Eric Elliott",
          "publisher": "O'Reilly Media",
          "description": "Take advantage of JavaScript's power to build robust web-scale or enterprise applications that are easy to extend and maintain. By applying the design patterns outlined in this practical book, experienced JavaScript developers will learn how to write flexible and resilient code that's easier-yes, easier-to work with as your codebase grows."
        },
        {
          "title": "Understanding ECMAScript 6",
          "subtitle": "The Definitive Guide for JavaScript Developers",
          "author": "Nicholas C. Zakas",
          "publisher": "No Starch Press",
          "description": "ECMAScript 6 represents the biggest update to the core of JavaScript in the history of the language. In Understanding ECMAScript 6, expert developer Nicholas C. Zakas provides a complete guide to the object types, syntax, and other exciting changes that ECMAScript 6 brings to JavaScript."
        }
    ])

    テキストインデックスの作成

    テキスト検索を実行するには、フィールドにテキストインデックスを作成する必要があります。これは、単一または複数のフィールドで作成できます。次のステートメントは、単一のフィールドにテキストインデックスを作成します。

    >db.books.createIndex({"description":"text"})

    説明にテキストインデックスを作成します および字幕 このチュートリアルのフィールド。 MongoDBでは、コレクションごとに1つのテキストインデックスしか作成できません。したがって、次のステートメントを使用して複合テキストインデックスを作成します。

    >db.books.createIndex({"subtitle":"text","description":"text"})

    次に、 descriptionに「ECMAScript」というキーワードが含まれるドキュメントを検索します。 および字幕 田畑。これには、以下のステートメントを使用できます。

    db.books.find({$text: {$search: "ECMAScript"}})

    >db.books.find({$text: {$search: "ECMAScript"}},{ subtitle: 1, description: 1 })
    	{
        "_id" : ObjectId("602b09cb3cb6144ada1c62fe"),
        "subtitle" : "The Definitive Guide for JavaScript Developers",
        "description" : "ECMAScript 6 represents the biggest update to the core of JavaScript in the history of the language. In Understanding ECMAScript 6, expert developer Nicholas C. Zakas provides a complete guide to the object types, syntax, and other exciting changes that ECMAScript 6 brings to JavaScript."
    	}
    >

    フレーズ

    テキストインデックスを使用してフレーズを検索できます。デフォルトでは、テキスト検索はフレーズ内のすべての単語に対してOR検索を実行します。 「モダンデザインパターン」を検索する場合は、「モダン」、「デザイン」、「パターン」のいずれかのキーワードでドキュメントを検索します。

    >db.books.find({$text: {$search: "modern design patterns"}},{ subtitle: 1, description: 1 })
    	{
        "_id" : ObjectId("602b098f3cb6144ada1c2ea1"),
        "subtitle" : "A JavaScript and jQuery Developer's Guide",
        "description" : "With Learning JavaScript Design Patterns, you'll learn how to write beautiful, structured, and maintainable JavaScript by applying classical and modern design patterns to the language. If you want to keep your code efficient, more manageable, and up-to-date with the latest best practices, this book is for you."
    	},
    	{
        "_id" : ObjectId("602b09b93cb6144ada1c4bca"),
        "subtitle" : "Robust Web Architecture with Node, HTML5, and Modern JS Libraries",
        "description" : "Take advantage of JavaScript's power to build robust web-scale or enterprise applications that are easy to extend and maintain. By applying the design patterns outlined in this practical book, experienced JavaScript developers will learn how to write flexible and resilient code that's easier-yes, easier-to work with as your code base grows.",
    	},
    	{
        "_id" : ObjectId("602b095c3cb6144ada1c1028"),
        "subtitle" : "A Modern Introduction to Programming",
        "description" : "JavaScript lies at the heart of almost every modern web application, from social apps to the newest browser-based games. Though simple for beginners to pick up and play with, JavaScript is a flexible, complex language that you can use to build full-scale applications."
    	}
    >

    「モダンなデザインパターン」を含むドキュメントのような正確なフレーズを一緒に検索する場合は、検索テキストに二重引用符を指定することで検索できます。

    >db.books.find({$text: {$search: "\"modern design patterns\""}},{ subtitle: 1, description: 1 })
    	{
        "_id" : ObjectId("602b098f3cb6144ada1c2ea1"),
        "subtitle" : "A JavaScript and jQuery Developer's Guide",
        "description" : "With Learning JavaScript Design Patterns, you'll learn how to write beautiful, structured, and maintainable JavaScript by applying classical and modern design patterns to the language. If you want to keep your code efficient, more manageable, and up-to-date with the latest best practices, this book is for you."
    }

    否定

    特定の単語を含むドキュメントを除外する場合は、否定検索を使用できます。たとえば、「JavaScript」を使用してすべてのドキュメントを検索するが、「HTML5」または「ECMAScript」を使用しない場合は、次の例のように検索できます。

    >db.books.find({$text: {$search: "JavaScript -HTML5 -ECMAScript"}},{ subtitle: 1, description: 1 })
    	{
        "_id" : ObjectId("602b098f3cb6144ada1c2ea1"),
        "subtitle" : "A JavaScript and jQuery Developer's Guide",
        "description" : "With Learning JavaScript Design Patterns, you'll learn how to write beautiful, structured, and maintainable JavaScript by applying classical and modern design patterns to the language. If you want to keep your code efficient, more manageable, and up-to-date with the latest best practices, this book is for you."
    	},
    	{
        "_id" : ObjectId("602b09a83cb6144ada1c4973"),
        "subtitle" : "An In-Depth Guide for Programmers",
        "description" : "Like it or not, JavaScript is everywhere these days, from browser to server to mobile and now you, too, need to learn the language or dive deeper than you have. This concise book guides you into and through JavaScript, written by a veteran programmer who once found himself in the same position."
    	},
    	{
        "_id" : ObjectId("602b095c3cb6144ada1c1028"),
        "subtitle" : "A Modern Introduction to Programming",
        "description" : "JavaScript lies at the heart of almost every modern web application, from social apps to the newest browser-based games. Though simple for beginners to pick up and play with, JavaScript is a flexible, complex language that you can use to build full-scale applications."
    	}

    テキスト検索スコア

    テキスト検索は、検索クエリとのドキュメントの関連性を表すスコアを各ドキュメントに提供します。このスコアを使用して、検索結果で返されたすべてのレコードを並べ替えることができます。スコアが高いほど、最も関連性の高い一致を示します。

    >db.books.find({$text: {$search: "JavaScript "}},{score: {$meta: "textScore"}, subtitle: 1, description: 1 }).sort({score:{$meta:"textScore"}})
    	{
        "_id" : ObjectId("602b098f3cb6144ada1c2ea1"),
        "subtitle" : "A JavaScript and jQuery Developer's Guide",
        "description" : "With Learning JavaScript Design Patterns, you'll learn how to write beautiful, structured, and maintainable JavaScript by applying classical and modern design patterns to the language. If you want to keep your code efficient, more manageable, and up-to-date with the latest best practices, this book is for you.",
        "score" : 1.43269230769231
    	},
    	{
        "_id" : ObjectId("602b09cb3cb6144ada1c62fe"),
        "subtitle" : "The Definitive Guide for JavaScript Developers",
        "description" : "ECMAScript 6 represents the biggest update to the core of JavaScript in the history of the language. In Understanding ECMAScript 6, expert developer Nicholas C. Zakas provides a complete guide to the object types, syntax, and other exciting changes that ECMAScript 6 brings to JavaScript.",
        "score" : 1.42672413793103
    	},
    	{
        "_id" : ObjectId("602b09a83cb6144ada1c4973"),
        "subtitle" : "An In-Depth Guide for Programmers",
        "description" : "Like it or not, JavaScript is everywhere these days, from browser to server to mobile and now you, too, need to learn the language or dive deeper than you have. This concise book guides you into and through JavaScript, written by a veteran programmer who once found himself in the same position.",
        "score" : 0.818181818181818
    	},
    	{
        "_id" : ObjectId("602b095c3cb6144ada1c1028"),
        "subtitle" : "A Modern Introduction to Programming",
        "description" : "JavaScript lies at the heart of almost every modern web application, from social apps to the newest browser-based games. Though simple for beginners to pick up and play with, JavaScript is a flexible, complex language that you can use to build full-scale applications.",
        "score" : 0.801724137931034
    	},
    	{
        "_id" : ObjectId("602b09b93cb6144ada1c4bca"),
        "subtitle" : "Robust Web Architecture with Node, HTML5, and Modern JS Libraries",
        "description" : "Take advantage of JavaScript's power to build robust web-scale or enterprise applications that are easy to extend and maintain. By applying the design patterns outlined in this practical book, experienced JavaScript developers will learn how to write flexible and resilient code that's easier-yes, easier-to work with as your codebase grows.",
        "score" : 0.792857142857143
    	}

    ストップワード

    $ text演算子は、英語のa、an、theなどの言語固有のストップワードを除外します。以下の検索では、結果にドキュメントは返されません。

    >db.books.find({$text: {$search: "is"}},{subtitle: 1, description: 1 })
    	Fetched 0 record(s)

    ステム化された単語

    $ text演算子は、完全な語幹単語に一致します。したがって、一部のドキュメントフィールドに「learning」または「learn」という単語が含まれている場合、「learning」または「learn」という用語を検索すると同じ結果になります。

    >db.books.find({$text: {$search: " learn"}},{subtitle: 1, description: 1 }) or >db.books.find({$text: {$search: " learning"}},{subtitle: 1, description: 1 })
    	{
        "_id" : ObjectId("602b098f3cb6144ada1c2ea1"),
        "subtitle" : "A JavaScript and jQuery Developer's Guide",
        "description" : "With Learning JavaScript Design Patterns, you'll learn how to write beautiful, structured, and maintainable JavaScript by applying classical and modern design patterns to the language. If you want to keep your code efficient, more manageable, and up-to-date with the latest best practices, this book is for you."
    	},
    	{
        "_id" : ObjectId("602b09a83cb6144ada1c4973"),
        "subtitle" : "An In-Depth Guide for Programmers",
        "description" : "Like it or not, JavaScript is everywhere these days, from browser to server to mobile and now you, too, need to learn the language or dive deeper than you have. This concise book guides you into and through JavaScript, written by a veteran programmer who once found himself in the same position."
    	},
    	{
        "_id" : ObjectId("602b09b93cb6144ada1c4bca"),
        "subtitle" : "Robust Web Architecture with Node, HTML5, and Modern JS Libraries",
        "description" : "Take advantage of JavaScript's power to build robust web-scale or enterprise applications that are easy to extend and maintain. By applying the design patterns outlined in this practical book, experienced JavaScript developers will learn how to write flexible and resilient code that's easier-yes, easier-to work with as your codebase grows."
    	}

    結論

    今日、あなたが何か新しいことを学んだことを願っています。これは、セルフホストのMongoDBに関する興味深い記事です。また、自分で試してみて、コメントセクションで経験を共有することをお勧めします。さらに、上記の定義のいずれかで問題が発生した場合は、下のコメントセクションでお気軽にお問い合わせください。


    1. マングースのfindOneサブドキュメント

    2. パンダを使用した大規模なデータワークフロー

    3. 複数のフィールドで同時にMongodb集計(カウント)

    4. Hadoop –初心者向けのApacheHadoopチュートリアル