这是我的代码:
转到1.13并进行弹性搜索7

package mainimport (    "context"    "encoding/json"    "fmt"    "github.com/teris-io/shortid"    "github.com/olivere/elastic/v7"    "log"    "reflect"    "time")const (    elasticIndexName = "documents")var (    elasticClient *elastic.Client)type DocumentRequest struct {    Title   string `json:"title"`    Content string `json:"content"`}type Document struct {    ID        string    `json:"id"`    Title     string    `json:"title"`    CreatedAt time.Time `json:"created_at"`    Content   string    `json:"content"`}func main() {    var err error    ctx := context.Background()    elasticClient,err = elastic.NewClient(        elastic.SetURL("http://127.0.0.1:9200"),elastic.Setsniff(false),)    if err != nil {        panic(err)    }    //_,err = elasticClient.CreateIndex(elasticIndexName).Do(ctx)    if err != nil {        fmt.Println(err)    }    //_,err2 := elasticClient.DeleteIndex(elasticIndexName).Do(ctx)    if err != nil {        fmt.Println(err)    }    indexParams := `{        "settings":{            "number_of_shards":1,"number_of_replicas":0        },"mappings":{                "properties": {                    "content": {"type":"text","fielddata": true                    },"title": {"type":"text","fielddata": true                    }                }            }        }    }`    // Create an index    _,err = elasticClient.CreateIndex(elasticIndexName).BodyString(indexParams).Do(ctx)    fmt.Println(indexParams)    if err != nil {        // Handle error        // Get *elastic.Error which contains additional information        e,ok := err.(*elastic.Error)        if !ok {        }        log.Printf("Elastic failed with status %d and error %s.",e.Status,e.Details)        panic(err)    }    if err != nil {        fmt.Println(err)    }    var docs []DocumentRequest    var doc1 DocumentRequest    doc1.COntent= "Content Bonjour"    doc1.Title = "Title Bonjour"    docs = append(docs,doc1)fmt.Println(docs)    for _,d := range docs {        doc := Document{            ID:        shortid.MustGenerate(),Title:     d.Title,CreatedAt: time.Now().UTC(),Content:   d.Content,}        docJ,_ := json.Marshal(doc)        fmt.Println(string(docJ))        put2,err4 := elasticClient.Index().            Index(elasticIndexName ).            Id(doc.ID).            BodyString(string(docJ)).            Do(ctx)        if err4 != nil {            panic(err4)        }        fmt.Println(put2)    }    termQuery := elastic.NewTermQuery("title","Bonjour")    result,err := elasticClient.Search().        Index(elasticIndexName).            // search in index "documents"        Query(termQuery).           // specify the query        Sort("title",true). // sort by "user" field,ascending        From(0).Size(10).           // take documents 0-9        Pretty(true).               // pretty print request and response JSON        Do(ctx)    if err != nil {        panic(err)    }    fmt.Println("result")    fmt.Println(result)    fmt.Printf("Found a total of %d docs\n",result.TotalHits())    var ttyp DocumentRequest    for _,item := range result.Each(reflect.TypeOf(ttyp)) {        if t,ok := item.(DocumentRequest); ok {            fmt.Printf("Doc %s: %s\n",t.Title,t.Content)        }    }}

最后我有:

[{Title Bonjour Content Bonjour}]{"id":"5DJ0u81WR","title":"Title Bonjour","created_at":"2019-11-14T23:16:26.028333176Z","content":"Content Bonjour"}&{documents _doc 5DJ0u81WR 1 created 0xc0001581c0 2 1 0 false}result&{map[Content-Type:[application/json; charset=UTF-8]] 4 false 0 []  0xc00015b3e0 map[] map[] false   0xc000158600 0}Found a total of 0 docs

我不明白为什么。该文档似乎已创建,但无法通过搜索找到。...


您正在使用术语查询来匹配完全相同的字段

Title Bonjour!=Bonjour

此行

termQuery := elastic.NewTermQuery("title","Bonjour")

等效于

where title='Bonjour'

在mysql中,如果您要匹配但不完全匹配,则可以使用匹配查询

matchQuery := elastic.NewMatchQuery("title","Bonjour")