3

如何使用 bleve 文本索引库https://github.com/blevesearch/bleve来索引 XML 内容?

我考虑过在 Go 中使用类似 XML 解析器的代码:https ://github.com/dps/go-xml-parse ,但是我如何将解析的内容传递给 Bleve 以进行索引?

更新:我的 XML:

我的 XML 如下所示:

<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>
4

1 回答 1

2

您将创建一个定义 XML 结构的结构。然后,您可以使用标准的“encoding/xml”包将 XML 解组到结构中。从那里你可以像往常一样用 Bleve 索引结构。

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)
}
于 2014-09-17T13:17:06.417 回答