Skip to content

Commit 780ae8f

Browse files
Inheriting the correct analyzer when default mapping is in use (#1807)
Here's an unhandled situation: - Say one defines a default dynamic mapping with a non-standard analyzer - Field content is indexed correctly per this non-standard analyzer - But, now let them run an analytic query over a field (that exists) - - This analytic query is incorrectly pulling the definition from the index's default_analyzer (`standard`) as opposed to the non-standard analyzer. This situation is not handled properly only for the default mapping and not type mappings. Also, https://issues.couchbase.com/browse/MB-56306
1 parent fa7b997 commit 780ae8f

File tree

2 files changed

+52
-1
lines changed

2 files changed

+52
-1
lines changed

mapping/index.go

+11-1
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,7 @@ func (im *IndexMappingImpl) AnalyzerNameForPath(path string) string {
364364
return analyzerName
365365
}
366366
}
367+
367368
// now try the default mapping
368369
pathMapping := im.DefaultMapping.documentMappingForPath(path)
369370
if pathMapping != nil {
@@ -377,7 +378,16 @@ func (im *IndexMappingImpl) AnalyzerNameForPath(path string) string {
377378
// next we will try default analyzers for the path
378379
pathDecoded := decodePath(path)
379380
for _, docMapping := range im.TypeMapping {
380-
rv := docMapping.defaultAnalyzerName(pathDecoded)
381+
if docMapping.Enabled {
382+
rv := docMapping.defaultAnalyzerName(pathDecoded)
383+
if rv != "" {
384+
return rv
385+
}
386+
}
387+
}
388+
// now the default analyzer for the default mapping
389+
if im.DefaultMapping.Enabled {
390+
rv := im.DefaultMapping.defaultAnalyzerName(pathDecoded)
381391
if rv != "" {
382392
return rv
383393
}

search_test.go

+41
Original file line numberDiff line numberDiff line change
@@ -1933,3 +1933,44 @@ func TestIPRangeQuery(t *testing.T) {
19331933
t.Fatal("Expected the 1 result - doc")
19341934
}
19351935
}
1936+
1937+
func TestAnalyzerInheritanceForDefaultDynamicMapping(t *testing.T) {
1938+
tmpIndexPath := createTmpIndexPath(t)
1939+
defer cleanupTmpIndexPath(t, tmpIndexPath)
1940+
1941+
imap := mapping.NewIndexMapping()
1942+
imap.DefaultMapping.DefaultAnalyzer = keyword.Name
1943+
1944+
idx, err := New(tmpIndexPath, imap)
1945+
if err != nil {
1946+
t.Fatal(err)
1947+
}
1948+
defer func() {
1949+
err = idx.Close()
1950+
if err != nil {
1951+
t.Fatal(err)
1952+
}
1953+
}()
1954+
1955+
doc := map[string]interface{}{
1956+
"fieldX": "AbCdEf",
1957+
}
1958+
1959+
if err = idx.Index("doc", doc); err != nil {
1960+
t.Fatal(err)
1961+
}
1962+
1963+
// Match query to apply keyword analyzer to fieldX.
1964+
mq := NewMatchQuery("AbCdEf")
1965+
mq.SetField("fieldX")
1966+
1967+
sr := NewSearchRequest(mq)
1968+
results, err := idx.Search(sr)
1969+
if err != nil {
1970+
t.Fatal(err)
1971+
}
1972+
1973+
if len(results.Hits) != 1 {
1974+
t.Fatalf("expected 1 hit, got %d", len(results.Hits))
1975+
}
1976+
}

0 commit comments

Comments
 (0)