在golang中有任何用于ElasticSearch的布尔查询解析器吗?

fsi0uk1n  于 2022-11-22  发布在  ElasticSearch
关注(0)|答案(1)|浏览(155)

go中是否有任何库可以将布尔查询转换为ElasticSearch查询?
使用典型的布尔查询表达式(AND,OR,“",*,?)转换为ElasticSearch的“json”查询,并创建“必须”,“应该”等。
例如:

(john smith AND book_id:123 AND book:(name:Rough)) AND (category='Java')
olqngx59

olqngx591#

我确实使用了ElasticSearch官方go
https://github.com/olivere/elastic之间的关系
要构造ElasticSearch查询,我需要一个布尔匹配查询,如下所示

GET listing/_search
{
  "query": {
    "bool": {
      "should": [
        {
          "match": {
            "location": {
              "query": ""
            }
          }
        },
        {
          "match": {
            "category": {
              "query": ""
            }
          }
        },
        {
          "match": {
            "description": {
              "query": ""
            }
          }
        },
        {
          "match": {
            "title": {
              "query": ""
            }
          }
        }
      ]
    }
  }
}

这是我最后写的

func Listings(){
q := esutils.NewBoolQuery()
        q = q.Should(esutils.NewMatchQuery("location", location), esutils.NewMatchQuery("category", category), esutils.NewMatchQuery("description", search), esutils.NewMatchQuery("title", search))

        src, err := q.Source()

        var buf bytes.Buffer
        query := map[string]interface{}{
            "query": src,
        }
        if err := json.NewEncoder(&buf).Encode(query); err != nil {
            log.Fatalf("Error encoding query: %s", err)
        }

        res, err := l.client.Search(l.client.Search.WithIndex("listing"),
            l.client.Search.WithPretty(),
            l.client.Search.WithSize(size),
            l.client.Search.WithFrom(offset),
            l.client.Search.WithTrackTotalHits(true),
            l.client.Search.WithBody(&buf),
        )

        if err != nil {
            logger.Error(err.Error())
            return listing, total, err
        }
        defer func(Body io.ReadCloser) {
            err := Body.Close()
            if err != nil {

            }
        }(res.Body)

        if res.IsError() {
            var e map[string]interface{}
            if err := json.NewDecoder(res.Body).Decode(&e); err != nil {
                logger.Error("Error parsing the response body: %s" + err.Error())
                return listing, total, err

            } else {
                // Print the response status and error information.
                logger.Error("Error parsing the response body: %s" + e["error"].(map[string]interface{})["type"].(string) + e["error"].(map[string]interface{})["reason"].(string))

                return listing, total, err

            }
        }

        if err := json.NewDecoder(res.Body).Decode(&r); err != nil {
            logger.Error(err.Error())

            return listing, total, err
        }

        total = int(r["hits"].(map[string]interface{})["total"].(map[string]interface{})["value"].(float64))

        for _, hit := range r["hits"].(map[string]interface{})["hits"].([]interface{}) {
            source := hit.(map[string]interface{})["_source"]
            sourceMap := source.(map[string]interface{})

            li, _ := dto.NewListingResponseBuilder().Location(sourceMap["location"].(string)).
                Title(sourceMap["title"].(string)).
                Images(sourceMap["files"].(string)).
                Location(sourceMap["location"].(string)).
                Description(sourceMap["description"].(string)).
                Pricing(sourceMap["pricing"].(string)).
                Facilities(sourceMap["facilities"].(string)).
                Id(hit.(map[string]interface{})["_id"].(string)).
                Keywords(sourceMap["keywords"].(string)).
                Build()

            listing = append(listing, *li)

        }

        return listing, total, err

}

相关问题