155
157
self.assertItemsEqual(['data', 'weight'], results[0].keys())
159
def test_ngrams_found(self):
160
# This tests that the underlying client search works
161
# not that the charmworld search API does _client != client.
162
client = self.index_client
163
test_value = "blarglefoafblatherinfinatum"
164
Charm(self.makeCharm(name=test_value))
165
query = {"query": {"match": {"ngrams": "bla"}}}
166
start, mingram = 0, 3
167
while start < len(test_value) - 3:
168
while mingram < len(test_value):
169
query['query']['match']['ngrams'] = test_value[start:mingram]
170
results = client._client.search(query, index=client.index_name)
171
self.assertEquals(results['hits']['total'], 1)
175
def test_ngrams_too_short(self):
176
client = self.index_client
178
Charm(self.makeCharm(name=test_value))
179
query = {"query": {"match": {"ngrams": "bl"}}}
180
start, mingram = 0, 2
181
while start < len(test_value) - 3:
182
while mingram < len(test_value):
183
query['query']['match']['ngrams'] = test_value[start:mingram]
184
results = client._client.search(query, index=client.index_name)
185
self.assertEquals(results['hits']['total'], 0)
189
def test_ngrams_no_matching_ngrams(self):
190
client = self.index_client
191
test_value = "blarglefoafblatherinfinatum"
192
foo_charm = Charm(self.makeCharm(name=test_value))
193
foo_charm = foo_charm # shut it pyflakes
194
query = {"query": {"match":
195
{"ngrams": "blvlsdkjfa;lsdkghoifdhvsoidufhvsdececewv"}}}
196
results = client._client.search(query, index=client.index_name)
197
self.assertEquals(results['hits']['total'], 0)
157
199
def test_search_bundle(self):
158
200
bundle = Bundle(self.makeBundle())
159
201
client = self.index_client
516
558
ids = [r['data']['_id'] for r in result]
517
559
self.assertEqual([matching_charm['_id']], ids)
519
def test_autocomplete_matches_on_prefix(self):
561
def test_autocomplete_matches_on_ngram(self):
520
562
charm1 = self.makeCharm(name='foo')
521
563
charm2 = self.makeCharm(name='foobar')
564
charm3 = self.makeCharm(name='barfoo')
565
result = self.index_client.api_search(
566
'foo', autocomplete=True, min_score=0)
567
ids = [r['data']['_id'] for r in result]
568
self.assertItemsEqual([
574
def test_autocomplete_misses_ngram(self):
575
self.makeCharm(name='foo')
576
self.makeCharm(name='foobar')
522
577
self.makeCharm(name='barfoo')
523
578
result = self.index_client.api_search(
524
'foo', autocomplete=True, min_score=0)
525
ids = [r['data']['_id'] for r in result]
526
self.assertItemsEqual([charm1['_id'], charm2['_id']], ids)
579
'bla', autocomplete=True, min_score=0)
580
self.assertEquals(result, [])
528
582
def test_special_characters_return_no_results(self):
529
583
self.makeCharm(name='foo')
1105
1159
def put_incompatible_mapping(index_client):
1106
1160
put_mapping(index_client, {
1107
'name': {'type': 'string', 'index': 'analyzed'},
1161
'_id': {'type': 'string', 'index': 'analyzed'},
1131
1185
put_incompatible_mapping(index_client)
1132
1186
update(index_client)
1133
1187
mapping = index_client.get_mapping()[CHARM]['properties']['data']
1134
self.assertEqual('not_analyzed',
1135
mapping['properties']['name']['index'])
1190
mapping['properties']['name']['fields']['name']['index'])
1193
mapping['properties']['name']['fields']['ngrams']['analyzer'])
1137
1195
def test_update_no_index(self):
1138
1196
index_client = ElasticSearchClient.from_settings(
1143
1201
self.assertEqual([actual_client.index_name],
1144
1202
index_client.get_aliased())
1145
1203
mapping = index_client.get_mapping()[CHARM]['properties']['data']
1146
self.assertEqual('not_analyzed',
1147
mapping['properties']['name']['index'])
1206
mapping['properties']['name']['fields']['name']['index'])
1209
mapping['properties']['name']['fields']['ngrams']['analyzer'])
1149
1211
def update_to_static_mapping(self, force_reindex):
1150
1212
index_client = self.use_index_client(put_mapping=False)
1218
1280
new_aliased = reindex(alias)
1219
1281
self.assertEqual([new_aliased.index_name], alias.get_aliased())
1220
1282
mapping = alias.get_mapping()[CHARM]['properties']['data']
1221
self.assertEqual('not_analyzed',
1222
mapping['properties']['name']['index'])
1285
mapping['properties']['name']['fields']['ngrams']['analyzer'])
1288
mapping['properties']['name']['fields']['name']['index'])
1290
@unittest.skip("XXX: Bug #1317567 - http://bit.ly/1lsXCfv")
1224
1291
def test_reindexed_no_client_charms(self):
1225
1292
client = ElasticSearchClient.from_settings(get_ini())
1226
1293
# This should not raise an exception, even though the index does not