XML Index with Go Text Indexing Library

How can I use the bleed text indexing library, https://github.com/blevesearch/bleve , to index XML content?

I thought about using code like this XML parser in Go: https://github.com/dps/go-xml-parse , but how can I pass what is parsed to Bleve to be indexed?

Update: My XML:

My XML looks like this:

    <title>Title here</title>
    <image>image url here</title>
    <text>A sentence of two about the topic</title>
        <fact>Fact 1</fact>
        <fact>Fact 2</fact>
        <fact>Fact 3</fact>



source to share

1 answer

You would create a structure that defines the structure of your XML. Then you can use the standard "encoding / xml" package to decouple the XML in the structure. And from there you can index the structure with Bleve as usual.


package main

import (

type Page []struct {
    Title string `xml:"title"`
    Image string `xml:"image"`
    Text  string `xml:"text"`
    Facts []struct {
        Fact string `xml:"fact"`
    } `xml:"facts"`

func main() {
    xmlData := []byte(`<page>
    <title>Title here</title>
    <image>image url here</image>
    <text>A sentence of two about the topic</text>
        <fact>Fact 1</fact>
        <fact>Fact 2</fact>
        <fact>Fact 3</fact>

    inputStruct := &Page{}
    err := xml.Unmarshal(xmlData, inputStruct)
    if nil != err {
        fmt.Println("Error unmarshalling from XML.", err)

    fmt.Printf("%+v\n", inputStruct)




All Articles