/
gf_crawl_index.go
192 lines (156 loc) · 6.66 KB
/
gf_crawl_index.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/*
GloFlow application and media management/publishing platform
Copyright (C) 2019 Ivan Trajkovic
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
package gf_crawl_core
import (
"time"
"fmt"
"context"
"go.mongodb.org/mongo-driver/bson/primitive"
"github.com/olivere/elastic"
"github.com/gloflow/gloflow/go/gf_core"
)
//--------------------------------------------------
type Gf_index__query_run struct {
Id primitive.ObjectID `bson:"_id,omitempty"`
Id_str string `bson:"id_str"`
T_str string `bson:"t"` //"index__query_run"
Run_time_milisec_int int64 `bson:"run_time_milisec_int"`
Hits_total_int int64 `bson:"hits_total_int"`
Hits_scores_lst []float64 `bson:"hits_scores_lst"`
Hits_score_max_f float64 `bson:"hits_score_max_f"`
Hits_urls_lst []string `bson:"hits_urls_lst"`
}
//--------------------------------------------------
func index__get_stats(pRuntime *GFcrawlerRuntime,
pRuntimeSys *gf_core.RuntimeSys) {
pRuntime.EsearchClient.IndexStats("gf_crawl_pages")
}
//--------------------------------------------------
func IndexQuery(p_term_str string,
pRuntime *GFcrawlerRuntime,
pRuntimeSys *gf_core.RuntimeSys) *gf_core.GFerror {
// ADD!! - use termquery result relevance
// - a terms query would incorporate the percentage of terms that were found
// - https://www.elastic.co/guide/en/elasticsearch/guide/current/relevance-intro.html
index_name_str := "gf_crawl_pages"
field_name_str := "page_text_str"
term_query := elastic.NewTermQuery(field_name_str,p_term_str)
ctx := context.Background()
query_result,err := pRuntime.EsearchClient.Search().
Index(index_name_str). // search in index "twitter"
Query(term_query). // specify the query
Highlight(elastic.NewHighlight().Field(field_name_str)). // result HIGHLIGHTING
Sort("user", true). // sort by "user" field, ascending
From(0).Size(10). // take documents 0-9
Pretty(true). // pretty print request and response JSON
Do(ctx) // execute
if err != nil {
gf_err := gf_core.ErrorCreate("failed to issue a elasticsearch index query - "+p_term_str,
"elasticsearch_query_index",
map[string]interface{}{
"term_str": p_term_str,
"index_name_str": index_name_str,
"field_name_str": field_name_str,
},
err, "gf_crawl_lib", pRuntimeSys)
return gf_err
}
query_run_time_milisec_int := query_result.TookInMillis
//----------------
// HITS
var search_hits *elastic.SearchHits = query_result.Hits
total_hits_int := search_hits.TotalHits
hits_score_max_f := search_hits.MaxScore
hits_lst := search_hits.Hits
hits_scores_lst := []float64{}
hits_urls_lst := []string{}
for _, search_hit := range hits_lst {
//-------------------
// relevance - the algorithm that we use to calculate how similar
// the contents of a full-text field are to a full-text query string.
// - standard similarity algorithm used in Elasticsearch is known as
// term frequency/inverse document frequency, or TF/IDF
// - Individual queries may combine the TF/IDF score with other
// factors such as the term proximity in phrase queries,
// or term similarity in fuzzy queries
//
// - "_score" - field in results
hit_score_f := search_hit.Score
hits_scores_lst = append(hits_scores_lst, *hit_score_f)
//-------------------
var highlights_map map[string][]string = search_hit.Highlight
hit_explanation_str := search_hit.Explanation.Description
fmt.Println(highlights_map)
fmt.Println(hit_explanation_str)
//result_doc_source__json_str := string(search_hit.Source)
var hit__doc_fields_map map[string]interface{} = search_hit.Fields
hit__url_str := hit__doc_fields_map["Url_str"].(string)
hits_urls_lst = append(hits_urls_lst, hit__url_str)
}
//----------------
creation_unix_time_f := float64(time.Now().UnixNano())/1000000000.0
id_str := fmt.Sprintf("crawler_page_img:%f", creation_unix_time_f)
query_run := &Gf_index__query_run{
Id_str: id_str,
T_str: "index__query_run",
Run_time_milisec_int: query_run_time_milisec_int,
Hits_total_int: total_hits_int,
Hits_scores_lst: hits_scores_lst,
Hits_score_max_f: *hits_score_max_f,
Hits_urls_lst: hits_urls_lst,
}
coll_name_str := "gf_crawl"
gf_err := gf_core.MongoInsert(query_run,
coll_name_str,
map[string]interface{}{
"term_str": p_term_str,
"total_hits_int": total_hits_int,
"caller_err_msg_str": "failed to insert a index__query_run into the DB for a elasticsearch index query",
},
ctx,
pRuntimeSys)
if gf_err != nil {
return gf_err
}
return nil
}
//--------------------------------------------------
func indexAddToOfURLfetch(pURLfetch *GFcrawlerURLfetch,
pRuntime *GFcrawlerRuntime,
pRuntimeSys *gf_core.RuntimeSys) *gf_core.GFerror {
indexNameStr := "gf_crawl_pages"
esRecordTypeStr := "crawl_page"
ctx := context.Background()
_, err := pRuntime.EsearchClient.Index().
Index(indexNameStr).
Type(esRecordTypeStr).
BodyJson(pURLfetch).
// Refresh(true). //refresh this index after completing this Index() operation
Do(ctx)
if err != nil {
errMsgStr := fmt.Sprintf("failed to add/index a url_fetch record (es type - %s) to the elasticsearch index - %s", esRecordTypeStr, indexNameStr)
gfErr := gf_core.ErrorCreate(errMsgStr,
"elasticsearch_add_to_index",
map[string]interface{}{
"url_fetch_url_str": pURLfetch.Url_str,
"index_name_str": indexNameStr,
"es_record_type_str": esRecordTypeStr,
},
err, "gf_crawl_core", pRuntimeSys)
return gfErr
}
return nil
}