File: text-indexes.rst

package info (click to toggle)
python-mongoengine 0.29.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 908 kB
  • sloc: python: 7,194; makefile: 57; sh: 17
file content (51 lines) | stat: -rw-r--r-- 1,184 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
===========
Text Search
===========

After MongoDB 2.4 version, supports search documents by text indexes.


Defining a Document with text index
===================================
Use the *$* prefix to set a text index, Look the declaration::

  class News(Document):
      title = StringField()
      content = StringField()
      is_active = BooleanField()

      meta = {'indexes': [
          {'fields': ['$title', "$content"],
           'default_language': 'english',
           'weights': {'title': 10, 'content': 2}
          }
      ]}



Querying
========

Saving a document::

  News(title="Using mongodb text search",
       content="Testing text search").save()

  News(title="MongoEngine 0.9 released",
       content="Various improvements").save()

Next, start a text search using :attr:`QuerySet.search_text` method::

  document = News.objects.search_text('testing').first()
  document.title # may be: "Using mongodb text search"

  document = News.objects.search_text('released').first()
  document.title # may be: "MongoEngine 0.9 released"


Ordering by text score
======================

::

  objects = News.objects.search_text('mongo').order_by('$text_score')