-
Notifications
You must be signed in to change notification settings - Fork 0
/
search.go
133 lines (116 loc) · 3.26 KB
/
search.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package elasticsearch
import (
"context"
"encoding/json"
"errors"
"fmt"
"os"
"strings"
"github.com/JSTOR-Labs/pep/api/globals"
"github.com/JSTOR-Labs/pep/api/pdfs"
"github.com/olivere/elastic/v7"
)
func BuildFilters(req SearchRequest) []elastic.Query {
var filters []elastic.Query
for _, f := range req.Filters {
filters = append(filters, elastic.NewQueryStringQuery(strings.ReplaceAll(f, "/", "\\/")))
}
for _, f := range titleExclude {
filters = append(filters, elastic.NewQueryStringQuery(fmt.Sprintf("-title:\"%s\"", f)).DefaultOperator("AND"))
}
return filters
}
func NewProcessTerms(req SearchRequest) elastic.Query {
query := elastic.NewBoolQuery()
var subquery elastic.Query
must := make([]elastic.Query, 0)
should := make([]elastic.Query, 0)
mustNot := make([]elastic.Query, 0)
if req.Query != "" && req.Query != "*:*" {
subquery = elastic.NewQueryStringQuery(strings.ReplaceAll(req.Query, "/", "\\/")).
DefaultOperator("AND").
FieldWithBoost("title", 3).
FieldWithBoost("semanticTerms", 3).
FieldWithBoost("abstract", 2).
FieldWithBoost("authors", 2).
Field("disciplines").
Field("journal").
Field("publisher").
Field("ocr").
Field("doi")
} else {
subquery = elastic.NewMatchAllQuery()
}
query.Filter(BuildFilters(req)...)
must = append(must, subquery)
query.Must(must...)
query.Should(should...)
query.MustNot(mustNot...)
return query
}
func ParseResults(esRes *elastic.SearchResult) *SearchResult {
res := new(SearchResult)
res.Total = esRes.TotalHits()
res.QueryTime = esRes.TookInMillis
res.Documents = make([]map[string]interface{}, 0)
for _, hit := range esRes.Hits.Hits {
doc := make(map[string]interface{})
err := json.Unmarshal(hit.Source, &doc)
if err != nil {
os.Exit(3)
}
doc["_id"] = doc["doi"]
doc["_score"] = *hit.Score
doc["_index"] = hit.Index
res.Documents = append(res.Documents, doc)
}
noIndex := false
index, err := pdfs.LoadIndex()
if err != nil {
noIndex = true
}
res.Facets = make(map[string][]map[string]interface{})
res.Stats = make(map[string]interface{})
for agg, aggData := range esRes.Aggregations {
ad := make(map[string]interface{})
err := json.Unmarshal(aggData, &ad)
if err != nil {
os.Exit(3)
}
if _, ok := ad["avg"]; ok {
res.Stats[agg] = aggData
} else {
res.Facets[agg] = make([]map[string]interface{}, 0)
for _, item := range ad["buckets"].([]interface{}) {
i := item.(map[string]interface{})
tempAgg := map[string]interface{}{
"value": i["key"],
"count": i["doc_count"],
}
res.Facets[agg] = append(res.Facets[agg], tempAgg)
}
}
}
for _, doc := range res.Documents {
PDFAvailable := false
if !noIndex {
PDFAvailable = index.Search(doc["doi"].(string))
}
doc["pdfAvailable"] = PDFAvailable
}
return res
}
func GetESDocument(doi string) (map[string]interface{}, error) {
query := elastic.NewTermQuery("doi", doi)
// Query ES for article title and pages
esDoc, err := globals.ES.Search().Index("_all").Query(query).RestTotalHitsAsInt(true).Do(context.Background())
if err != nil {
return nil, err
}
if esDoc.Hits.TotalHits.Value == 0 {
return nil, errors.New("no documents found")
}
var doc map[string]interface{}
err = json.Unmarshal(esDoc.Hits.Hits[0].Source, &doc)
return doc, err
}