github.com/keybase/client/go@v0.0.0-20241007131713-f10651d043c8/kbfs/search/mapping_test.go (about) 1 // Copyright 2019 Keybase Inc. All rights reserved. 2 // Use of this source code is governed by a BSD 3 // license that can be found in the LICENSE file. 4 5 package search 6 7 import ( 8 "testing" 9 10 "github.com/blevesearch/bleve" 11 "github.com/blevesearch/bleve/index/store/gtreap" 12 "github.com/blevesearch/bleve/mapping" 13 "github.com/stretchr/testify/require" 14 ) 15 16 const ( 17 testMappingText = "A new span of the Golden Gate Bridge is complete" 18 testMappingHTML = "<span>The Golden Gate Bridge is actually red</span>" 19 ) 20 21 type testMappingTextFile struct { 22 Name string 23 Text string 24 } 25 26 var _ mapping.Classifier = testMappingTextFile{} 27 28 func (tf testMappingTextFile) Type() string { 29 return textFileType 30 } 31 32 type testMappingHTMLFile struct { 33 Name string 34 HTML string 35 } 36 37 var _ mapping.Classifier = testMappingHTMLFile{} 38 39 func (hf testMappingHTMLFile) Type() string { 40 return htmlFileType 41 } 42 43 func TestIndexMapping(t *testing.T) { 44 indexMapping, err := makeIndexMapping() 45 require.NoError(t, err) 46 47 index, err := bleve.NewUsing( 48 "", indexMapping, bleveIndexType, gtreap.Name, nil) 49 require.NoError(t, err) 50 51 t.Log("Insert text file into the index") 52 textID := "text" 53 tf := testMappingTextFile{"textFile", testMappingText} 54 err = index.Index(textID, tf) 55 require.NoError(t, err) 56 57 t.Log("Insert HTML file into the index") 58 htmlID := "html" 59 hf := testMappingHTMLFile{"htmlFile", testMappingHTML} 60 err = index.Index(htmlID, hf) 61 require.NoError(t, err) 62 63 t.Log("Search for a common text word") 64 query := bleve.NewQueryStringQuery("golden") 65 request := bleve.NewSearchRequest(query) 66 result, err := index.Search(request) 67 require.NoError(t, err) 68 require.Len(t, result.Hits, 2) 69 70 t.Log("Search for an HTML tag word") 71 query = bleve.NewQueryStringQuery("span") 72 request = bleve.NewSearchRequest(query) 73 result, err = index.Search(request) 74 require.NoError(t, err) 75 require.Len(t, result.Hits, 1) 76 77 t.Log("Make sure stop words aren't indexed") 78 query = bleve.NewQueryStringQuery("the") 79 request = bleve.NewSearchRequest(query) 80 result, err = index.Search(request) 81 require.NoError(t, err) 82 require.Len(t, result.Hits, 0) 83 }