Skip to content

Commit e881fc1

Browse files
Diego Giovane Pasqualinhonzakral
Diego Giovane Pasqualin
authored andcommitted
Add search_as_you_type datatype
1 parent 7651c9d commit e881fc1

File tree

2 files changed

+87
-0
lines changed

2 files changed

+87
-0
lines changed

elasticsearch_dsl/field.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,14 @@ class Text(Field):
266266
}
267267
name = 'text'
268268

269+
class SearchAsYouType(Field):
270+
_param_defs = {
271+
'analyzer': {'type': 'analyzer'},
272+
'search_analyzer': {'type': 'analyzer'},
273+
'search_quote_analyzer': {'type': 'analyzer'},
274+
}
275+
name = 'search_as_you_type'
276+
269277
class Keyword(Field):
270278
_param_defs = {
271279
'fields': {'type': 'field', 'hash': True},

examples/search_as_you_type.py

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
# -*- coding: utf-8 -*-
2+
"""
3+
Example ``Document`` with search_as_you_type field datatype and how to search it.
4+
5+
When creating a field with search_as_you_type datatype ElasticSearch creates additional subfields to enable efficient
6+
as-you-type completion, matching terms at any position within the input.
7+
8+
To custom analyzer with ascii folding allow search to work in different languages.
9+
"""
10+
from __future__ import print_function, unicode_literals
11+
12+
from elasticsearch_dsl import connections, Document, analyzer, token_filter, SearchAsYouType
13+
from elasticsearch_dsl.query import MultiMatch
14+
15+
# custom analyzer for names
16+
ascii_fold = analyzer(
17+
'ascii_fold',
18+
# we don't want to split O'Brian or Toulouse-Lautrec
19+
tokenizer='whitespace',
20+
filter=[
21+
'lowercase',
22+
token_filter('ascii_fold', 'asciifolding')
23+
]
24+
)
25+
26+
27+
class Person(Document):
28+
name = SearchAsYouType(max_shingle_size=3)
29+
30+
class Index:
31+
name = 'test-search-as-you-type'
32+
settings = {
33+
'number_of_shards': 1,
34+
'number_of_replicas': 0
35+
}
36+
37+
38+
if __name__ == '__main__':
39+
# initiate the default connection to elasticsearch
40+
connections.create_connection()
41+
42+
# create the empty index
43+
Person.init()
44+
45+
import pprint
46+
pprint.pprint(Person().to_dict(), indent=2)
47+
48+
# index some sample data
49+
names = [
50+
'Andy Warhol',
51+
'Alphonse Mucha',
52+
'Henri de Toulouse-Lautrec',
53+
'Jára Cimrman',
54+
]
55+
for id, name in enumerate(names):
56+
Person(_id=id, name=name).save()
57+
58+
# refresh index manually to make changes live
59+
Person._index.refresh()
60+
61+
# run some suggestions
62+
for text in ('já', 'Cimr', 'toulouse', 'Henri Tou', 'a'):
63+
s = Person.search()
64+
65+
s.query = MultiMatch(
66+
query=text,
67+
type="bool_prefix",
68+
fields=[
69+
"name",
70+
"name._2gram",
71+
"name._3gram"
72+
]
73+
)
74+
75+
response = s.execute()
76+
77+
# print out all the options we got
78+
for h in response:
79+
print('%15s: %25s' % (text, h.name))

0 commit comments

Comments
 (0)