~fabricematrat/charmworld/redirect-charm

« back to all changes in this revision

Viewing changes to charmworld/tests/test_search.py

  • Committer: Tarmac
  • Author(s): Reed O'Brien
  • Date: 2014-05-14 16:16:48 UTC
  • mfrom: (506.2.10 ngrams)
  • Revision ID: tarmac-20140514161648-ax50xhzkvot5vv4p
adds ngrams as multifield index to name. skips destructive test -- see Bug #1317567.

Approved by Juju Gui Bot, Richard Harding, j.c.sackett.

Show diffs side-by-side

added added

removed removed

Lines of Context:
8
8
from textwrap import dedent
9
9
from time import sleep
10
10
 
 
11
import unittest
 
12
 
11
13
from pyelasticsearch import ElasticSearch
12
14
from pyelasticsearch.exceptions import (
13
15
    ElasticHttpError,
154
156
        # it's present.
155
157
        self.assertItemsEqual(['data', 'weight'], results[0].keys())
156
158
 
 
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)
 
172
                mingram += 1
 
173
            start += 1
 
174
 
 
175
    def test_ngrams_too_short(self):
 
176
        client = self.index_client
 
177
        test_value = "blarg"
 
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)
 
186
                mingram += 1
 
187
                start += 1
 
188
 
 
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)
 
198
 
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)
518
560
 
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([
 
569
            charm1['_id'],
 
570
            charm2['_id'],
 
571
            charm3['_id']],
 
572
            ids)
 
573
 
 
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, [])
527
581
 
528
582
    def test_special_characters_return_no_results(self):
529
583
        self.makeCharm(name='foo')
1104
1158
 
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'},
1108
1162
    })
1109
1163
 
1110
1164
 
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'])
 
1188
        self.assertEqual(
 
1189
            'not_analyzed',
 
1190
            mapping['properties']['name']['fields']['name']['index'])
 
1191
        self.assertEqual(
 
1192
            'n3_20grams',
 
1193
            mapping['properties']['name']['fields']['ngrams']['analyzer'])
1136
1194
 
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'])
 
1204
        self.assertEqual(
 
1205
            'not_analyzed',
 
1206
            mapping['properties']['name']['fields']['name']['index'])
 
1207
        self.assertEqual(
 
1208
            'n3_20grams',
 
1209
            mapping['properties']['name']['fields']['ngrams']['analyzer'])
1148
1210
 
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'])
 
1283
        self.assertEqual(
 
1284
            'n3_20grams',
 
1285
            mapping['properties']['name']['fields']['ngrams']['analyzer'])
 
1286
        self.assertEqual(
 
1287
            'not_analyzed',
 
1288
            mapping['properties']['name']['fields']['name']['index'])
1223
1289
 
 
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