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:

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

      

+3


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.

http://play.golang.org/p/IZP4nrOotW



package main

import (
    "encoding/xml"
    "fmt"
)

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>
    <facts>
        <fact>Fact 1</fact>
        <fact>Fact 2</fact>
        <fact>Fact 3</fact>
    </facts>
</page>`)

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

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

      

+2


source







All Articles