~jcsackett/charmworld/bac-tag-constraints

« back to all changes in this revision

Viewing changes to charmworld/tests/test_search.py

  • Committer: Tarmac
  • Author(s): Brad Crittenden
  • Date: 2014-07-16 16:39:51 UTC
  • mfrom: (515.1.1 ngram-inquiry)
  • Revision ID: tarmac-20140716163951-hunwr56yizg012h5
Make ngram searches work.

After the ngram search was deployed it was discovered to only work if 1) using
the API and 2) had autocomplete=true.

Inspecting the code showed the intent for ngram to be used in other
situations, and there was not documented rationale for only using it when
doing autocomplete.

The problem was simply in the query for non-autocomplete the field for the
ngrams was prefixed with 'data.' when it should not have been.

The ngrams fields were moved out of the charm and bundle fields and passed as
separate fields, treated like 'provides' and 'requires', to avoid the
prefixing.

As a result of this change, the search on the manage.jujucharms.com page now
uses ngrams too.

https://codereview.appspot.com/117810044/

R=makyo. Fixes: https://bugs.launchpad.net/bugs/1220909.

Approved by Juju Gui Bot, Matthew Scott.

Show diffs side-by-side

added added

removed removed

Lines of Context:
187
187
    def test_ngrams_no_matching_ngrams(self):
188
188
        client = self.index_client
189
189
        test_value = "blarglefoafblatherinfinatum"
190
 
        foo_charm = Charm(self.makeCharm(name=test_value))
191
 
        foo_charm = foo_charm  # shut it pyflakes
 
190
        Charm(self.makeCharm(name=test_value))
192
191
        query = {"query": {"match":
193
192
                {"ngrams": "blvlsdkjfa;lsdkghoifdhvsoidufhvsdececewv"}}}
194
193
        results = client._client.search(query, index=client.index_name)
195
194
        self.assertEquals(results['hits']['total'], 0)
196
195
 
 
196
    def test_search_charm_ngrams_found(self):
 
197
        client = self.index_client
 
198
        test_value = "blarglefoafblatherinfinatum"
 
199
        Charm(self.makeCharm(name=test_value))
 
200
        results = client.search('foaf')['results']
 
201
        self.assertEqual(1, len(results[CHARM]))
 
202
 
 
203
    def test_search_charm_no_matching_ngrams(self):
 
204
        client = self.index_client
 
205
        test_value = "blarglefoafblatherinfinatum"
 
206
        Charm(self.makeCharm(name=test_value))
 
207
        results = client.search('schmoo')['results']
 
208
        self.assertEqual(0, len(results[CHARM]))
 
209
 
 
210
    def test_search_charm_ngrams_do_not_match_description(self):
 
211
        client = self.index_client
 
212
        name = "blarglefoafblatherinfinatum"
 
213
        Charm(self.makeCharm(name=name, description='supermoon'))
 
214
        results = client.search('moon')['results']
 
215
        self.assertEqual(0, len(results[CHARM]))
 
216
 
197
217
    def test_search_bundle(self):
198
218
        bundle = Bundle(self.makeBundle())
199
219
        client = self.index_client
325
345
            self.assertEqual(2, len(results['bundle']))
326
346
            self.assertEqual(0, len(results['charm']))
327
347
 
 
348
    def test_search_bundle_ngrams_found(self):
 
349
        client = self.index_client
 
350
        test_value = "blarglefoafblatherinfinatum"
 
351
        Bundle(self.makeBundle(name=test_value))
 
352
        results = client.search('foaf')['results']
 
353
        self.assertEqual(1, len(results[BUNDLE]))
 
354
 
 
355
    def test_search_bundle_no_matching_ngrams(self):
 
356
        client = self.index_client
 
357
        test_value = "blarglefoafblatherinfinatum"
 
358
        Bundle(self.makeBundle(name=test_value))
 
359
        results = client.search('schmoo')['results']
 
360
        self.assertEqual(0, len(results[BUNDLE]))
 
361
 
 
362
    def test_search_bundle_ngrams_do_not_match_description(self):
 
363
        client = self.index_client
 
364
        name = "blarglefoafblatherinfinatum"
 
365
        Bundle(self.makeBundle(name=name, description='supermoon'))
 
366
        results = client.search('moon')['results']
 
367
        self.assertEqual(0, len(results[BUNDLE]))
 
368
 
328
369
    def test_searching_for_charms_only(self):
329
370
        charm = Charm(self.makeCharm(description='a mozilla charm'))
330
371
        Bundle(self.makeBundle(description='a mozilla bundle'))
577
618
            'bla', autocomplete=True, min_score=0)
578
619
        self.assertEquals(result, [])
579
620
 
 
621
    def test_ngram_match(self):
 
622
        # Without autocomplete.
 
623
        charm1 = self.makeCharm(name='foo')
 
624
        charm2 = self.makeCharm(name='foobar')
 
625
        charm3 = self.makeCharm(name='barfoo')
 
626
        result = self.index_client.api_search(
 
627
            'foo', autocomplete=False, min_score=0)
 
628
        ids = [r['data']['_id'] for r in result]
 
629
        self.assertItemsEqual([
 
630
            charm1['_id'],
 
631
            charm2['_id'],
 
632
            charm3['_id']],
 
633
            ids)
 
634
 
 
635
    def test_ngram_miss(self):
 
636
        # Without autocomplete.
 
637
        self.makeCharm(name='foo')
 
638
        self.makeCharm(name='foobar')
 
639
        self.makeCharm(name='barfoo')
 
640
        result = self.index_client.api_search(
 
641
            'bla', autocomplete=False, min_score=0)
 
642
        self.assertEquals(result, [])
 
643
 
580
644
    def test_special_characters_return_no_results(self):
581
645
        self.makeCharm(name='foo')
582
646
        result = self.index_client.api_search(