15
15
# specific language governing permissions and limitations
16
16
# under the License.
17
17
18
- from .connections import get_connection
18
+ from . import async_connections , connections
19
19
from .utils import AttrDict , DslBase , merge
20
20
21
21
__all__ = ["tokenizer" , "analyzer" , "char_filter" , "token_filter" , "normalizer" ]
@@ -119,20 +119,7 @@ class CustomAnalyzer(CustomAnalysisDefinition, Analyzer):
119
119
"tokenizer" : {"type" : "tokenizer" },
120
120
}
121
121
122
- def simulate (self , text , using = "default" , explain = False , attributes = None ):
123
- """
124
- Use the Analyze API of elasticsearch to test the outcome of this analyzer.
125
-
126
- :arg text: Text to be analyzed
127
- :arg using: connection alias to use, defaults to ``'default'``
128
- :arg explain: will output all token attributes for each token. You can
129
- filter token attributes you want to output by setting ``attributes``
130
- option.
131
- :arg attributes: if ``explain`` is specified, filter the token
132
- attributes to return.
133
- """
134
- es = get_connection (using )
135
-
122
+ def _get_body (self , text , explain , attributes ):
136
123
body = {"text" : text , "explain" : explain }
137
124
if attributes :
138
125
body ["attributes" ] = attributes
@@ -156,7 +143,43 @@ def simulate(self, text, using="default", explain=False, attributes=None):
156
143
if self ._builtin_type != "custom" :
157
144
body ["analyzer" ] = self ._builtin_type
158
145
159
- return AttrDict (es .indices .analyze (body = body ))
146
+ return body
147
+
148
+ def simulate (self , text , using = "default" , explain = False , attributes = None ):
149
+ """
150
+ Use the Analyze API of elasticsearch to test the outcome of this analyzer.
151
+
152
+ :arg text: Text to be analyzed
153
+ :arg using: connection alias to use, defaults to ``'default'``
154
+ :arg explain: will output all token attributes for each token. You can
155
+ filter token attributes you want to output by setting ``attributes``
156
+ option.
157
+ :arg attributes: if ``explain`` is specified, filter the token
158
+ attributes to return.
159
+ """
160
+ es = connections .get_connection (using )
161
+ return AttrDict (
162
+ es .indices .analyze (body = self ._get_body (text , explain , attributes ))
163
+ )
164
+
165
+ async def async_simulate (
166
+ self , text , using = "default" , explain = False , attributes = None
167
+ ):
168
+ """
169
+ Use the Analyze API of elasticsearch to test the outcome of this analyzer.
170
+
171
+ :arg text: Text to be analyzed
172
+ :arg using: connection alias to use, defaults to ``'default'``
173
+ :arg explain: will output all token attributes for each token. You can
174
+ filter token attributes you want to output by setting ``attributes``
175
+ option.
176
+ :arg attributes: if ``explain`` is specified, filter the token
177
+ attributes to return.
178
+ """
179
+ es = async_connections .get_connection (using )
180
+ return AttrDict (
181
+ await es .indices .analyze (body = self ._get_body (text , explain , attributes ))
182
+ )
160
183
161
184
162
185
class Normalizer (AnalysisBase , DslBase ):
0 commit comments