This repository has been archived by the owner on Nov 19, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
search_bar_module.R
314 lines (280 loc) · 11.7 KB
/
search_bar_module.R
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
searchBarUI <- function(id){
ns <- NS(id)
tagList(
fluidRow(
column(
fluidRow(
column(
uiOutput(ns("query_text_ui")),
width = 9
),
column(
# Cant use "dropdownButton" because of https://github.com/dreamRs/shinyWidgets/issues/38
shinyWidgets::dropdown(
# https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-simple-query-string-query.html#simple-query-string-syntax',
actionLink(ns("advanced_query_info"), label = "Click here for documentation on Advanced Queries", style = "color: blue; font-weight: bold"),
br(), br(),
shinyWidgets::checkboxGroupButtons(ns("search_fields"),
"Search Fields",
choices = search_fields(),
status = 'info',
selected = search_fields(),
checkIcon = list(yes = icon("ok", lib = "glyphicon"),
no = icon("remove", lib = "glyphicon"))),
uiOutput(ns("date_range_ui"), style = "padding-left: 10px"),
uiOutput(ns("sentiment_range_ui")),
shinyWidgets::radioGroupButtons(ns("sort_by"),
label = "Sort Results By",
choices = c("Search Score" = "score",
"Date (Oldest First)" = "date_asc",
"Date (Newest First)" = "date_desc"),
status = "success",
selected = "score"),
uiOutput(ns("data_source_ui")),
# Only turn on AI search if we've specified an API Host:
if (!is.null(get_configs()$embedding_api_host)) {
tagList(
shinyWidgets::switchInput(ns("use_embeddings"),
label = "🤖 AI Search 🤖",
labelWidth = "110px",
value = FALSE,
inline = TRUE),
span("BETA", class = "label label-info") # Bootstrap label
)
},
circle = FALSE,
status = "primary",
icon = icon("cog"),
width = "600px",
tooltip = shinyWidgets::tooltipOptions(title = "Advanced Options")
),
width = 3
),
style = sprintf("
background-color: %s;
margin: 0 0 0 0;
padding: 20px 0 5px 0;
border-radius: 10px;
vertical-align = center;
min-height: 75px; // Should be 50 + padding
", main_div_background())
),
width = 6
)
)
)
}
searchBar <- function(input, output, session,
es_connection,
data_set_info,
query_param) {
output$query_text_ui <- renderUI({
shinyWidgets::searchInput(inputId = session$ns("query_text"),
label = NULL,
value = query_param,
placeholder = "Type Search Here",
btnSearch = icon("search"),
btnReset = icon("times"),
width = "100%")
})
observeEvent(input$advanced_query_info, {
modalDialog(
title = "Text Depot - Advanced Queries",
size = "l",
'
Text Depot provides basic keyword search (enter the words you are interested
in), but also provides a way to put together advanced queries using several
logical operators that you can use in your searches:
',
br(),
br(),
fluidRow(
column(strong("+ (plus sign)"), width = 3),
column("Indicates that the term following it must be present in the search results.", width = 6)
),
br(),
fluidRow(
column(strong("- (minus sign)"), width = 3),
column("Indicates that the term following it must be absent from the search results.", width = 6)
),
br(),
fluidRow(
column(strong('" " (quotation marks)'), width = 3),
column("Search for an exact phrase. Results will only include documents that contain the exact phrase.", width = 6)
),
br(),
fluidRow(
column(strong("* (asterisk)"), width = 3),
column('Multiple character wildcard. For example, a search for "bus*" would match both "buses" and "business".', width = 6)
),
br(),
fluidRow(
column(strong("? (question mark)"), width = 3),
column('Wildcard, but it matches only one character. For example, a search for "te?t" would match "test" and "text" but not "tenant".', width = 6)
),
br(),
fluidRow(
column(strong("~ (tilde)"), width = 3),
column("Fuzzy matching. It will match documents that are similar to the search term, with a specified level of tolerance for differences, in the format [term]~[edit distance]. For example, a search for test~1 would match test, text, best, and other words that are an edit-distance of 1 away from your query. This is useful when including mis-spellings.", width = 6)
),
br(),
fluidRow(
column(strong("AND"), width = 3),
column('Returns documents that match all of the search terms. Example: "dog AND cat" returns documents containing both "dog" and "cat".', width = 6)
),
br(),
fluidRow(
column(strong("OR"), width = 3),
column('Returns documents that match at least one of the search terms. Example: "dog OR cat" returns documents containing either "dog" or "cat" or both.', width = 6)
),
br(),
fluidRow(
column(strong("NOT"), width = 3),
column('Excludes documents that match the search term. Example: "dog NOT cat" returns documents containing "dog" but not "cat".', width = 6)
),
br(),
'Note that you can combine the above operators, and group them with parenthesis (). For example: "safety AND (bus OR train) -budget".',
easyClose = TRUE
) %>%
showModal()
})
# Set the focus on the search bar, once input is loaded
execute_at_first_input(runjs(sprintf(
'document.getElementById("%s").focus();', session$ns("query_text_text")
)))
output$data_source_ui <- renderUI({
choices = data_set_info()$alias_name
names(choices) = paste0(data_set_info()$display_name, " (", data_set_info()$db_size, " docs)")
shinyWidgets::pickerInput(session$ns("data_sources"),
label = "Data Sources",
choices = choices,
selected = choices,
multiple = TRUE,
options = shinyWidgets::pickerOptions(
actionsBox = TRUE,
title = "Please Select a Data Source"
))
})
# Setting initial value so that we dont have to load the UI
# element before setting the date ranges:
date_stats <- reactive({
stats_for_field(es_connection, data_set_info()$alias_name, "date")
})
selected_date_range <- reactive({
range = c(as.Date(date_stats()$min), as.Date(date_stats()$max))
# Check that UI element exists and that valid dates are there:
if (!is.null(input$date_range)) {
if ((!is.na(input$date_range[1])) &
(!is.na(input$date_range[2]))) {
range = input$date_range
}
}
return(range)
})
output$date_range_ui <- renderUI({
min = as.Date(date_stats()$min)
max = as.Date(date_stats()$max)
dateRangeInput(
inputId = session$ns("date_range"),
label = "Date Range",
start = min,
end = max)
})
sentiment_stats <- reactive({
stats_for_field(es_connection, data_set_info()$alias_name, "sentiment_polarity",numeric = TRUE)
})
selected_sentiment_range <- reactive({
range = c(sentiment_stats()$min, sentiment_stats()$max)
# Check that UI element exists and that valid sentiment are there:
if (!is.null(input$sentiment_range)) {
if ((!is.na(input$sentiment_range[1])) &
(!is.na(input$sentiment_range[2]))) {
range = input$sentiment_range
}
}
return(range)
})
output$sentiment_range_ui <- renderUI({
min_val = sentiment_stats()$min
max_val = sentiment_stats()$max
sliderInput(
inputId = session$ns("sentiment_range"),
label = "Sentiment Range",
min = min_val,
max = max_val,
value = c(min_val,max_val),step=0.05)
})
selected_data_sources <- reactive({
data_sets <- data_set_info()$alias_name
# On page load, input$data_sources is null because it's buried in the dropdown options
if (!is.null(input$data_sources)) {
data_sets = input$data_sources
}
return(data_sets)
})
cleaned_query <- reactive({
req(input$query_text)
query = input$query_text
query = gsub('"', '\"', query)
query = gsub("'", "\"", query)
query
})
aggregations <- reactiveVal(c())
use_embeddings_setting <- reactive({
ifelse(is.null(input$use_embeddings), FALSE, input$use_embeddings)
})
# folowing is for computing num_hits. This is used in this module, but also in other modules.
# Get aggregates for ALL results when search is clicked:
observe({
aggregations(c())
req(nchar(cleaned_query()) > 0)
req(length(selected_data_sources()) > 0)
req(length(input$search_fields) > 0)
aggregations = query_text_depot(query_info = list(conn = es_connection,
query = cleaned_query(),
index = selected_data_sources(),
search_fields = input$search_fields,
min_score = 0,
min_date = selected_date_range()[1],
max_date = selected_date_range()[2],
min_sentiment = selected_sentiment_range()[1],
max_sentiment = selected_sentiment_range()[2],
use_embeddings = use_embeddings_setting()),
aggregates_json = statsPerIndexQuery())
aggregations = parse_aggregates(aggregations)
if (is.character(aggregations)) {
# There was an error
shinyjs::alert(aggregations)
aggregations(c())
} else {
parsed <- parse_aggregates(aggregations)
aggregations(aggregations)
}
})
query_info <- reactive({
list(conn = es_connection,
query = cleaned_query(),
index = selected_data_sources(),
search_fields = input$search_fields,
min_score = 0,
min_date = selected_date_range()[1],
max_date = selected_date_range()[2],
min_sentiment = selected_sentiment_range()[1],
max_sentiment = selected_sentiment_range()[2],
sort_by = input$sort_by,
num_hits = sum(aggregations()$counts_by_index$doc_count),
use_embeddings = use_embeddings_setting())
})
return(query_info)
}
statsPerIndexQuery <- function(){
'
"aggs": {
"group_by_index": {
"terms": {
"field": "_index"
}
}
}
'
}