|
1 | 1 | def get_thread_ids(context, group_ids, local_params, search_text)
|
2 |
| - filters = [] |
3 |
| - filters.push({term: {commentable_id: local_params['commentable_id']}}) if local_params['commentable_id'] |
4 |
| - filters.push({terms: {commentable_id: local_params['commentable_ids'].split(',')}}) if local_params['commentable_ids'] |
5 |
| - filters.push({term: {course_id: local_params['course_id']}}) if local_params['course_id'] |
| 2 | + must = [] |
| 3 | + filter = [] |
| 4 | + must.push({term: {commentable_id: local_params['commentable_id']}}) if local_params['commentable_id'] |
| 5 | + must.push({terms: {commentable_id: local_params['commentable_ids'].split(',')}}) if local_params['commentable_ids'] |
| 6 | + must.push({term: {course_id: local_params['course_id']}}) if local_params['course_id'] |
| 7 | + must.push( |
| 8 | + { |
| 9 | + multi_match: { |
| 10 | + query: search_text, |
| 11 | + fields: [:title, :body], |
| 12 | + operator: :AND |
| 13 | + } |
| 14 | + } |
| 15 | + ) |
| 16 | + group_id = local_params['group_id'] |
| 17 | + |
| 18 | + if group_id |
| 19 | + filter.push( |
| 20 | + {:bool => {:must_not => {:exists => {:field => :group_id}}}}, |
| 21 | + {:term => {:group_id => group_id}} |
| 22 | + ) |
| 23 | + end |
6 | 24 |
|
7 |
| - filters.push({or: [ |
8 |
| - {not: {exists: {field: :context}}}, |
9 |
| - {term: {context: context}} |
10 |
| - ]}) |
| 25 | + filter.push( |
| 26 | + {:bool => {:must_not => {:exists => {:field => :context}}}}, |
| 27 | + {:term => {:context => context}} |
| 28 | + ) |
11 | 29 |
|
12 | 30 | unless group_ids.empty?
|
13 |
| - filters.push( |
14 |
| - { |
15 |
| - bool: { |
16 |
| - should: [ |
17 |
| - {:not => {:exists => {:field => :group_id}}}, |
18 |
| - {:terms => {:group_id => group_ids}} |
19 |
| - ] |
20 |
| - } |
21 |
| - } |
| 31 | + filter.push( |
| 32 | + {:bool => {:must_not => {:exists => {:field => :group_id}}}}, |
| 33 | + {:terms => {:group_id => group_ids}} |
22 | 34 | )
|
23 | 35 | end
|
24 | 36 |
|
25 | 37 | body = {
|
26 |
| - size: CommentService.config['max_deep_search_comment_count'].to_i, |
27 |
| - sort: [ |
28 |
| - {updated_at: :desc} |
29 |
| - ], |
30 |
| - query: { |
31 |
| - filtered: { |
32 |
| - query: { |
33 |
| - multi_match: { |
34 |
| - query: search_text, |
35 |
| - fields: [:title, :body], |
36 |
| - operator: :AND |
37 |
| - } |
38 |
| - }, |
39 |
| - filter: { |
40 |
| - bool: { |
41 |
| - must: filters |
42 |
| - } |
43 |
| - } |
44 |
| - } |
| 38 | + size: CommentService.config['max_deep_search_comment_count'].to_i, |
| 39 | + sort: [ |
| 40 | + {updated_at: :desc} |
| 41 | + ], |
| 42 | + query: { |
| 43 | + bool: { |
| 44 | + must: must, |
| 45 | + should: filter |
45 | 46 | }
|
| 47 | + } |
46 | 48 | }
|
47 | 49 |
|
48 |
| - response = Elasticsearch::Model.client.search(index: Content::ES_INDEX_NAME, body: body) |
| 50 | + response = Elasticsearch::Model.client.search(index: TaskHelpers::ElasticsearchHelper::INDEX_NAMES, body: body) |
49 | 51 |
|
50 | 52 | thread_ids = Set.new
|
51 | 53 | response['hits']['hits'].each do |hit|
|
52 |
| - case hit['_type'] |
53 |
| - when CommentThread.document_type |
54 |
| - thread_ids.add(hit['_id']) |
55 |
| - when Comment.document_type |
56 |
| - thread_ids.add(hit['_source']['comment_thread_id']) |
57 |
| - else |
58 |
| - # There shouldn't be any other document types. Nevertheless, ignore them, if they are present. |
59 |
| - next |
| 54 | + if hit['_index'].include? CommentThread.index_name |
| 55 | + thread_ids.add(hit['_id']) |
| 56 | + elsif hit['_index'].include? Comment.index_name |
| 57 | + thread_ids.add(hit['_source']['comment_thread_id']) |
| 58 | + else |
| 59 | + # There shouldn't be any other indices. Nevertheless, ignore them, if they are present. |
| 60 | + next |
60 | 61 | end
|
61 | 62 | end
|
62 | 63 | thread_ids
|
63 | 64 | end
|
64 | 65 |
|
65 | 66 | def get_suggested_text(search_text)
|
66 | 67 | body = {
|
67 |
| - suggestions: { |
68 |
| - text: search_text, |
69 |
| - phrase: { |
70 |
| - field: :_all |
71 |
| - } |
| 68 | + suggest: { |
| 69 | + body_suggestions: { |
| 70 | + text: search_text, |
| 71 | + phrase: { |
| 72 | + field: :body |
| 73 | + } |
| 74 | + }, |
| 75 | + title_suggestions: { |
| 76 | + text: search_text, |
| 77 | + phrase: { |
| 78 | + field: :title |
| 79 | + } |
72 | 80 | }
|
| 81 | + } |
73 | 82 | }
|
74 |
| - response = Elasticsearch::Model.client.suggest(index: Content::ES_INDEX_NAME, body: body) |
75 |
| - suggestions = response.fetch('suggestions', []) |
76 |
| - if suggestions.length > 0 |
77 |
| - options = suggestions[0]['options'] |
78 |
| - if options.length > 0 |
79 |
| - return options[0]['text'] |
| 83 | + |
| 84 | + response = Elasticsearch::Model.client.search(index: TaskHelpers::ElasticsearchHelper::INDEX_NAMES, body: body) |
| 85 | + body_suggestions = response['suggest'].fetch('body_suggestions', []) |
| 86 | + title_suggestions = response['suggest'].fetch('title_suggestions', []) |
| 87 | + |
| 88 | + [body_suggestions, title_suggestions].each do |suggestion| |
| 89 | + if suggestion.length > 0 |
| 90 | + options = suggestion[0]['options'] |
| 91 | + return options[0]['text'] if options.length > 0 |
80 | 92 | end
|
81 | 93 | end
|
82 | 94 |
|
|
0 commit comments