~ubuntu-branches/debian/sid/golang-github-blevesearch-bleve/sid

« back to all changes in this revision

Viewing changes to cmd/bleve/cmd/check.go

  • Committer: Package Import Robot
  • Author(s): Michael Lustfield
  • Date: 2017-03-30 16:06:03 UTC
  • Revision ID: package-import@ubuntu.com-20170330160603-0oogmb960l7918jx
Tags: upstream-0.5.0+git20170324.202.4702785f
Import upstream version 0.5.0+git20170324.202.4702785f

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Copyright © 2016 Couchbase, Inc.
 
2
//
 
3
// Licensed under the Apache License, Version 2.0 (the "License");
 
4
// you may not use this file except in compliance with the License.
 
5
// You may obtain a copy of the License at
 
6
//
 
7
//              http://www.apache.org/licenses/LICENSE-2.0
 
8
//
 
9
// Unless required by applicable law or agreed to in writing, software
 
10
// distributed under the License is distributed on an "AS IS" BASIS,
 
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
12
// See the License for the specific language governing permissions and
 
13
// limitations under the License.
 
14
 
 
15
package cmd
 
16
 
 
17
import (
 
18
        "fmt"
 
19
        "log"
 
20
 
 
21
        "github.com/blevesearch/bleve"
 
22
        "github.com/spf13/cobra"
 
23
)
 
24
 
 
25
var checkFieldName string
 
26
var checkCount int
 
27
 
 
28
// checkCmd represents the check command
 
29
var checkCmd = &cobra.Command{
 
30
        Use:   "check [index path]",
 
31
        Short: "checks the contents of the index",
 
32
        Long:  `The check command will perform consistency checks on the index.`,
 
33
        RunE: func(cmd *cobra.Command, args []string) error {
 
34
 
 
35
                var fieldNames []string
 
36
                var err error
 
37
                if checkFieldName == "" {
 
38
                        fieldNames, err = idx.Fields()
 
39
                        if err != nil {
 
40
                                return err
 
41
                        }
 
42
                } else {
 
43
                        fieldNames = []string{checkFieldName}
 
44
                }
 
45
                fmt.Printf("checking fields: %v\n", fieldNames)
 
46
 
 
47
                totalProblems := 0
 
48
                for _, fieldName := range fieldNames {
 
49
                        fmt.Printf("checking field: '%s'\n", fieldName)
 
50
                        problems, err := checkField(idx, fieldName)
 
51
                        if err != nil {
 
52
                                log.Fatal(err)
 
53
                        }
 
54
                        totalProblems += problems
 
55
                }
 
56
 
 
57
                if totalProblems != 0 {
 
58
                        return fmt.Errorf("found %d total problems\n", totalProblems)
 
59
                }
 
60
 
 
61
                return nil
 
62
        },
 
63
}
 
64
 
 
65
func checkField(index bleve.Index, fieldName string) (int, error) {
 
66
        termDictionary, err := getDictionary(index, fieldName)
 
67
        if err != nil {
 
68
                return 0, err
 
69
        }
 
70
        fmt.Printf("field contains %d terms\n", len(termDictionary))
 
71
 
 
72
        numTested := 0
 
73
        numProblems := 0
 
74
        for term, count := range termDictionary {
 
75
                fmt.Printf("checked %d terms\r", numTested)
 
76
                if checkCount > 0 && numTested >= checkCount {
 
77
                        break
 
78
                }
 
79
 
 
80
                tq := bleve.NewTermQuery(term)
 
81
                tq.SetField(fieldName)
 
82
                req := bleve.NewSearchRequest(tq)
 
83
                req.Size = 0
 
84
                res, err := index.Search(req)
 
85
                if err != nil {
 
86
                        return 0, err
 
87
                }
 
88
 
 
89
                if res.Total != count {
 
90
                        fmt.Printf("unexpected mismatch for term '%s', dictionary %d, search hits %d\n", term, count, res.Total)
 
91
                        numProblems++
 
92
                }
 
93
 
 
94
                numTested++
 
95
        }
 
96
        fmt.Printf("done checking %d terms, found %d problems\n", numTested, numProblems)
 
97
 
 
98
        return numProblems, nil
 
99
}
 
100
 
 
101
func getDictionary(index bleve.Index, field string) (map[string]uint64, error) {
 
102
        rv := make(map[string]uint64)
 
103
        i, _, err := index.Advanced()
 
104
        if err != nil {
 
105
                log.Fatal(err)
 
106
        }
 
107
        r, err := i.Reader()
 
108
        if err != nil {
 
109
                log.Fatal(err)
 
110
        }
 
111
        d, err := r.FieldDict(field)
 
112
        if err != nil {
 
113
                log.Fatal(err)
 
114
        }
 
115
 
 
116
        de, err := d.Next()
 
117
        for err == nil && de != nil {
 
118
                rv[de.Term] = de.Count
 
119
                de, err = d.Next()
 
120
        }
 
121
        if err != nil {
 
122
                return nil, err
 
123
        }
 
124
        return rv, nil
 
125
}
 
126
 
 
127
func init() {
 
128
        RootCmd.AddCommand(checkCmd)
 
129
        checkCmd.Flags().StringVarP(&checkFieldName, "field", "f", "", "Restrict check to the specified field name, by default check all fields.")
 
130
        checkCmd.Flags().IntVarP(&checkCount, "count", "c", 100, "Check this many terms, default 100.")
 
131
}