Skip to content

[]string in, string out #570

@porjo

Description

@porjo

I have a situation where if I store a []string with a single element I get back a value of type string, whereas if I store a []string with 2 or more elements, I get back a value of type []interface{}

Is this a property of Go language, or something about Bleve implementation?

This example code below demonstrates the issue. It should produce output:

abc result type string
xyz result type []interface {}
package main

import (
	"fmt"
	"log"
	"reflect"

	"github.com/blevesearch/bleve"
)

type doc struct {
	Name       string
	Recipients []string
}

func main() {
	mapping := bleve.NewIndexMapping()
	index, err := bleve.NewMemOnly(mapping)

	// single string
	id := "1234"
	d1 := doc{Name: "abc", Recipients: []string{"a@example.com"}}
	err = index.Index(id, d1)
	if err != nil {
		log.Fatal(err)
	}

	query := bleve.NewMatchQuery("abc")
	search := bleve.NewSearchRequest(query)
	search.Fields = []string{"Name", "Recipients"}
	searchResults, err := index.Search(search)
	if err != nil {
		log.Fatal(err)
	}

	hit := searchResults.Hits[0]
	fmt.Printf("abc result type %s\n", reflect.TypeOf(hit.Fields["Recipients"]))

	// multi strings
	id = "5678"
	d2 := doc{Name: "xyz", Recipients: []string{"a@example.com", "b@example.com"}}
	err = index.Index(id, d2)
	if err != nil {
		log.Fatal(err)
	}

	query = bleve.NewMatchQuery("xyz")
	search = bleve.NewSearchRequest(query)
	search.Fields = []string{"Name", "Recipients"}
	searchResults, err = index.Search(search)
	if err != nil {
		log.Fatal(err)
	}

	hit = searchResults.Hits[0]
	fmt.Printf("xyz result type %s\n", reflect.TypeOf(hit.Fields["Recipients"]))
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions