使用Go bleve文本索引库对XML进行索引化。

huangapple go评论67阅读模式
英文:

Index XML with the Go bleve text indexing library

问题

你可以使用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>
英文:

How can I use the bleve 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 then how do I pass what is parsed to Bleve to be indexed?

Update: My XML:

My XML looks like the following:

&lt;page&gt;
    &lt;title&gt;Title here&lt;/title&gt;
    &lt;image&gt;image url here&lt;/title&gt;
    &lt;text&gt;A sentence of two about the topic&lt;/title&gt;
    &lt;facts&gt;
        &lt;fact&gt;Fact 1&lt;/fact&gt;
        &lt;fact&gt;Fact 2&lt;/fact&gt;
        &lt;fact&gt;Fact 3&lt;/fact&gt;
    &lt;/facts&gt;
&lt;/page&gt;

答案1

得分: 2

你可以创建一个定义XML结构的结构体。然后,你可以使用标准的"encoding/xml"包将XML解组为该结构体。然后,你可以像正常情况下一样使用Bleve对结构体进行索引。

以下是代码的中文翻译:

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)
}

希望对你有帮助!

英文:

You would create a struct defining the structure of your XML. You can then use the standard "encoding/xml" package to unmarshal XML into the struct. And from there you can index the struct with Bleve as normal.

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

package main

import (
	&quot;encoding/xml&quot;
	&quot;fmt&quot;
)

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

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

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

	fmt.Printf(&quot;%+v\n&quot;, inputStruct)
}

huangapple
  • 本文由 发表于 2014年9月16日 10:18:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/25859720.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定