blugelabs / bluge

indexing library for Go
Apache License 2.0
1.88k stars 122 forks source link

Search throught _all does not work? #101

Closed IAkumaI closed 2 years ago

IAkumaI commented 2 years ago

Hi. I trying bluge and can not understand why search without field (default to _all) does not work. It's just example from docs:

config := bluge.DefaultConfig(path)
    writer, err := bluge.OpenWriter(config)
    if err != nil {
        log.Fatalf("error opening writer: %v", err)
    }
    defer writer.Close()

    doc := bluge.NewDocument("example").
        AddField(bluge.NewTextField("name", "bluge"))

    err = writer.Update(doc.ID(), doc)
    if err != nil {
        log.Fatalf("error updating document: %v", err)
    }

    reader, err := writer.Reader()
    if err != nil {
        log.Fatalf("error getting index reader: %v", err)
    }
    defer reader.Close()

    query := bluge.NewMatchQuery("bluge").SetField("_all")
    request := bluge.NewTopNSearch(10, query).
        WithStandardAggregations()
    documentMatchIterator, err := reader.Search(context.Background(), request)
    if err != nil {
        log.Fatalf("error executing search: %v", err)
    }
    match, err := documentMatchIterator.Next()
    for err == nil && match != nil {

        // load the identifier for this match
        err = match.VisitStoredFields(func(field string, value []byte) bool {
            if field == "_id" {
                fmt.Printf("match: %s\n", string(value))
            }
            return true
        })
        if err != nil {
            log.Fatalf("error loading stored fields: %v", err)
        }
        match, err = documentMatchIterator.Next()
    }
    if err != nil {
        log.Fatalf("error iterator document matches: %v", err)
    }

With SetField("name") it works correctly, but if I remove SetField() or set SetField("_all") - it just does not match anything. Why?

IAkumaI commented 2 years ago

I found a solution. I must manually add to doc composite _all field. I have no idea where I should found it in docs :)

Am i right?

AddField(bluge.NewCompositeFieldExcluding("_all", []string{"_id"}))
mschoch commented 2 years ago

This is correct.

The Bluge documentation does not seem to mention composite fields at all. I searched the Bluge website and found one mention of this in the blog post about site search here:

https://github.com/blugelabs/blugelabs.com/blame/master/content/blog/site-search.md#L81-L89

The example in that blog post is identical to the solution you found.

I would recommend we start by just adding a page to the documentation called "Composite Fields" and giving a short explanation with an example. Possibly also linking to this discussion. If we find there are other places to reference/link this information so that people find it, that can be added later.