Home  >  Article  >  Backend Development  >  How to develop an application for ES retrieval using Golang

How to develop an application for ES retrieval using Golang

PHPz
PHPzOriginal
2023-04-05 14:37:151077browse

With the rapid development of Internet applications, massive data has become a norm, and efficient storage and query of these data have become very important. Search Engine is an efficient and scalable distributed retrieval engine for large-scale distributed data storage environments. It is a technology oriented to the field of text retrieval.

ElasticSearch (ES) is a search engine developed based on the Lucene library. It is a distributed full-text search engine based on RESTful architecture that can support real-time search, data analysis and other functions. Due to its open source nature and ease of use, ElasticSearch is increasingly favored by developers. This article will introduce how to use Golang to develop applications for ES retrieval.

First, we need to install the ES client in the Go programming language. The client of ES uses a RESTful architecture, so we can use Go's HTTP request library to interact with ES. Then, we can refer to the following code example to call the ES RESTful API to implement a simple search:

package main

import (
    "encoding/json"
    "fmt"
    "net/http"
    "bytes"
)

type SearchResult struct {
    Hits struct {
        Total int `json:"total"`
        Hits []struct {
            Source interface{} `json:"_source"`
        } `json:"hits"`
    } `json:"hits"`
}

func main() {
    query := "hello"
    url := fmt.Sprintf("http://localhost:9200/_search?q=%s", query)
    resp, _ := http.Get(url)
    defer resp.Body.Close()
    var result SearchResult
    json.NewDecoder(resp.Body).Decode(&result)
    b, _ := json.Marshal(result.Hits.Hits)
    fmt.Println(string(b))
}

First, we define a structure named SearchResult to store ES search results. Then, we used the fmt.Sprintf function to construct the search URL, submitted the request to ES through the http.Get function, and parsed the result into the structure.

Finally, we serialize the results to JSON format and print to the console. In this way, we can use Go language to search documents in ES very simply.

However, this method is only suitable for simple search applications. For operations that require richer functions such as search by conditions or aggregation, we need to use the Golang client officially provided by ES: go-elasticsearch.

First, we need to install the officially provided go-elasticsearch library. You can use the following command to install:

go get github.com/elastic/go-elasticsearch/v8

Next, we use the following code example to implement ES query :

package main

import (
    "context"
    "fmt"
    "github.com/elastic/go-elasticsearch/v8"
    "github.com/elastic/go-elasticsearch/v8/esapi"
    "encoding/json"
    "bytes"
)

type SearchResult struct {
    Hits struct {
        Total int `json:"total"`
        Hits []struct {
            Source interface{} `json:"_source"`
        } `json:"hits"`
    } `json:"hits"`
}

func main() {
    es, err := elasticsearch.NewDefaultClient()
    if err != nil {
        fmt.Println("Error creating Elasticsearch client:", err)
        return
    }

    query := "hello"

    var buf bytes.Buffer
    queryMap := map[string]interface{}{
        "query": map[string]interface{}{
            "match": map[string]interface{}{
                "message": query,
            },
        },
    }
    if err := json.NewEncoder(&buf).Encode(queryMap); err != nil {
        fmt.Println("Error encoding query:", err)
        return
    }

    req := esapi.SearchRequest{
        Index: []string{"my_index"},
        Body:  &buf,
        Pretty: true,
    }

    res, err := req.Do(context.Background(), es)
    if err != nil {
        fmt.Println("Error searching for documents:", err)
        return
    }
    defer res.Body.Close()

    var result SearchResult
    json.NewDecoder(res.Body).Decode(&result)
    b, _ := json.Marshal(result.Hits.Hits)
    fmt.Println(string(b))
}

First, we create an Elasticsearch client and then define the query keywords. Next, we construct a map in JSON format containing query conditions and encode it into buf through the json.NewEncoder function.

Finally, we use the ES API provided by the go-elasticsearch library to send query requests to ES, and read and parse the request responses.

Using the go-elasticsearch library can easily implement complex ES search functions and make the code more elegant and simple. Using Golang for ES search greatly improves search speed while maintaining code efficiency.

In short, Golang is a concise and efficient programming language, and it is very easy to use it to implement ES search. I hope this article can help you understand the use of ES search and go-elasticsearch library.

The above is the detailed content of How to develop an application for ES retrieval using Golang. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn