【问题标题】:How to search a string in the elasticsearch document(indexed) in golang?如何在golang的elasticsearch文档(索引)中搜索字符串?
【发布时间】:2015-05-23 13:51:43
【问题描述】:

我正在用 golang 编写一个函数来搜索被索引的 elasticsearch 文档中的字符串。我正在使用elasticsearch golang 客户端elastic。例如考虑对象是推文,

type Tweet struct {
    User    string
    Message string
    Retweets int
}

而搜索功能是

func SearchProject() error{
    // Search with a term query
    termQuery := elastic.NewTermQuery("user", "olivere")
    searchResult, err := client.Search().
        Index("twitter").   // search in index "twitter"
        Query(&termQuery).  // specify the query
        Sort("user", true). // sort by "user" field, ascending
        From(0).Size(10).   // take documents 0-9
        Pretty(true).       // pretty print request and response JSON
        Do()                // execute
    if err != nil {
        // Handle error
        panic(err)
        return err
    }

    // searchResult is of type SearchResult and returns hits, suggestions,
    // and all kinds of other information from Elasticsearch.
    fmt.Printf("Query took %d milliseconds\n", searchResult.TookInMillis)

    // Each is a convenience function that iterates over hits in a search result.
    // It makes sure you don't need to check for nil values in the response.
    // However, it ignores errors in serialization. If you want full control
    // over iterating the hits, see below.
    var ttyp Tweet
    for _, item := range searchResult.Each(reflect.TypeOf(ttyp)) {
        t := item.(Tweet)
        fmt.Printf("Tweet by %s: %s\n", t.User, t.Message)
    }
    // TotalHits is another convenience function that works even when something goes wrong.
    fmt.Printf("Found a total of %d tweets\n", searchResult.TotalHits())

    // Here's how you iterate through results with full control over each step.
    if searchResult.Hits != nil {
        fmt.Printf("Found a total of %d tweets\n", searchResult.Hits.TotalHits)

        // Iterate through results
        for _, hit := range searchResult.Hits.Hits {
            // hit.Index contains the name of the index

            // Deserialize hit.Source into a Tweet (could also be just a map[string]interface{}).
            var t Tweet
            err := json.Unmarshal(*hit.Source, &t)
            if err != nil {
                // Deserialization failed
            }

            // Work with tweet
            fmt.Printf("Tweet by %s: %s\n", t.User, t.Message)
        }
    } else {
        // No hits
        fmt.Print("Found no tweets\n")
    }
    return nil
}

此搜索正在打印用户“olivere”的推文。但是,如果我给“橄榄”,那么搜索将不起作用。如何搜索属于用户/消息/转推的字符串?

而索引功能是这样的,

func IndexProject(p *objects.ElasticProject) error {
// Index a tweet (using JSON serialization)
    tweet1 := `{"user" : "olivere", "message" : "It's a Raggy Waltz"}`
    put1, err := client.Index().
        Index("twitter").
        Type("tweet").
        Id("1").
        BodyJson(tweet1).
        Do()
    if err != nil {
        // Handle error
        panic(err)
        return err
    }
    fmt.Printf("Indexed tweet %s to index %s, type %s\n", put1.Id, put1.Index, put1.Type)

    return nil
}

输出:

Indexed tweet 1 to index twitter, type tweet
Got document 1 in version 1 from index twitter, type tweet
Query took 4 milliseconds
Tweet by olivere: It's a Raggy Waltz
Found a total of 1 tweets
Found a total of 1 tweets
Tweet by olivere: It's a Raggy Waltz

版本

Go 1.4.2
Elasticsearch-1.4.4

Elasticsearch Go 库

github.com/olivere/elastic

有人可以帮我解决这个问题吗?谢谢

【问题讨论】:

    标签: search indexing go elasticsearch


    【解决方案1】:

    您如何搜索和查找数据取决于您的分析器 - 从您的代码来看,可能正在使用标准分析器(即您没有在映射中指定替代方案)。

    Standard Analyser 只会索引完整的单词。因此,要将“olive”与“olivere”匹配,您可以:

    1. 更改搜索过程

    例如从术语查询切换到 Prefix query 或使用带有通配符的 Query String query

    1. 更改索引进程

    如果您想在较大的字符串中查找字符串,请考虑在分析器中使用 nGramsEdge nGrams

    【讨论】:

    • 我是弹性搜索的新手。我很感激帮助。您能告诉我如何更改索引过程吗?因此,搜索与 JSON 中所有字段匹配的字符串会很有帮助。
    • 第一步是设置一个分析器,然后在你的映射中引用它。这里有一个很好的介绍 - elastic.co/guide/en/elasticsearch/guide/current/…
    【解决方案2】:
    multiQuery := elastic.NewMultiMatchQuery(
        term,
        "name", "address", "location", "email", "phone_number", "place", "postcode",
    ).Type("phrase_prefix")
    

    【讨论】:

      猜你喜欢
      • 2021-03-26
      • 2017-10-21
      • 2020-09-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-10-15
      • 2021-07-19
      相关资源
      最近更新 更多