Skip to content

Commit 59929c2

Browse files
author
Max
committed
Merge branch 'es' into 'master'
Es See merge request ubnt-dev/discourse-elasticsearch!1
2 parents 469a943 + 3354269 commit 59929c2

File tree

12 files changed

+3137
-22
lines changed

12 files changed

+3137
-22
lines changed
Lines changed: 180 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,180 @@
1+
export default {
2+
3+
name: "discourse-autocomplete",
4+
initialize() {},
5+
_initialize(options) {
6+
//getlogin status
7+
var currentUser = Discourse.User.current();
8+
if (!currentUser) {
9+
$('.Typeahead-spinner').css("left","390px");
10+
}
11+
//ajax function
12+
var autocomplete = function(query, processSync, processAsync){
13+
//Get text from the input field
14+
var text = $('#search-box').val(),
15+
//ES Query
16+
json = {
17+
"query":{
18+
"multi_match":
19+
{"query":text,
20+
"fields":[],
21+
"type":"best_fields"
22+
}
23+
}
24+
};
25+
// ajax var init
26+
var url = options.elasticsearch_address,
27+
data = json,
28+
formatType = "json",
29+
type = "post",
30+
contentType = "application/json; charset=utf-8";
31+
console.log(url);
32+
if(type === "post" && formatType === "json"){
33+
data = JSON.stringify(data);
34+
}
35+
if(formatType === "json"){
36+
contentType="application/json";
37+
}
38+
return $.ajax({
39+
dataType : "json",
40+
async : true,
41+
type:type,
42+
cache: false,
43+
url:url,
44+
data:data,
45+
contentType:contentType,
46+
timeout:30000
47+
}).then(function(rs){
48+
var dfd = $.Deferred();
49+
var results = $.map([0], function() {
50+
51+
//Parse the results and return them
52+
var resultsData = rs.hits,
53+
resultsLength = Object.keys(resultsData.hits).length,
54+
datum = [];
55+
56+
for (var i = 0; i < resultsLength; i++) {
57+
var resultsArray = resultsData.hits[i]._source,
58+
resultsIndex = resultsData.hits[i]._index;
59+
if (resultsIndex == "discourse-users") {
60+
var user_avatar = resultsArray.avatar_template.replace("\{size}", 50);
61+
datum.push({
62+
// user
63+
user_avatar_template: user_avatar,
64+
user_username: resultsArray.username,
65+
user_likes_received: resultsArray.likes_received,
66+
url: resultsArray.url
67+
});
68+
}else if (resultsIndex == "discourse-tags") {
69+
datum.push({
70+
// tag
71+
tag_name: resultsArray.name,
72+
tag_topic_count: resultsArray.topic_count,
73+
url:resultsArray.url
74+
});
75+
}else if (resultsIndex == "discourse-posts") {
76+
var topic_name = resultsArray.topic.title,
77+
topic_view = resultsArray.topic.views,
78+
topic_url = resultsArray.topic.url,
79+
category = resultsArray.category.name,
80+
category_color = resultsArray.category.color,
81+
category_url = resultsArray.category.url,
82+
author = resultsArray.user.username,
83+
author_url = resultsArray.user.url,
84+
pre = resultsArray.content;
85+
86+
datum.push({
87+
// post
88+
post_topic_name: topic_name,
89+
post_topic_view: topic_view,
90+
url: topic_url,
91+
post_category: category,
92+
post_category_color: category_color,
93+
post_category_url: category_url,
94+
post_author: author,
95+
post_author_url: author_url,
96+
post_pre: pre
97+
98+
});
99+
}
100+
101+
}
102+
return datum;
103+
});
104+
processAsync(results);
105+
106+
return dfd.promise();
107+
},function(){
108+
alert("网络异常");
109+
}).always(function(rs){
110+
111+
});
112+
}
113+
114+
115+
116+
117+
118+
$('#search-box').typeahead({
119+
highlight: true,
120+
minLength: 1
121+
},
122+
{
123+
name: 'posts',
124+
displayKey: 'value',
125+
source: autocomplete,
126+
async: true,
127+
limit: 6,
128+
templates: {
129+
empty: [
130+
'<div class="empty-message">',
131+
'无结果',
132+
'</div>'
133+
].join('\n'),
134+
footer: [
135+
'<div class="show-more">',
136+
'<a class="advanced-search" onclick="document.location.href="/search"; document.reload();" href="/search">更多...</a>',
137+
'<div>'
138+
].join('\n'),
139+
suggestion: function(value) {
140+
if (value.post_topic_name != undefined) {
141+
return '<div class="es-dataset-posts"><div class="hit-post"><div class="hit-post-title-holder"><span class="hit-post-topic-title"><a href="' + value.url + '">'+ value.post_topic_name + '</a></span><span class="hit-post-topic-views" title="Number of times the topic has been viewed">'+ value.post_topic_view + '</span></div><div class="hit-post-category-tags"><span class="hit-post-category"><span class="badge-wrapper bullet"><span class="badge-category-bg" style="background-color: #'+ value.post_category_color +';"></span><a class="badge-category hit-post-category-name" href="'+ value.post_category_url +'">'+ value.post_category + '</a></span></span></div><div class="hit-post-content-holder"><a class="hit-post-username" href="'+ value.post_author_url +'">'+ value.post_author + '</a>:<span class="hit-post-content">'+ value.post_pre +'</span></div></div></div>'
142+
}else{
143+
return '<span></span>'
144+
145+
}
146+
}
147+
}
148+
},{
149+
name: 'users-tags',
150+
displayKey: 'value',
151+
source: autocomplete,
152+
async: true,
153+
limit: 6,
154+
templates: {
155+
empty: "",
156+
suggestion: function(value) {
157+
if (value.tag_name != undefined) {
158+
return '<div class="es-dataset-tags"><a href="'+ value.url +'"><div class="hit-tag"><span class="hit-tag-name">#'+ value.tag_name +' </span><span class="hit-tag-topic_count" title="Number of topics with this tag"> '+ value.tag_topic_count +'</span></div></a></div>'
159+
}else if (value.user_username != undefined){
160+
return '<div class="es-dataset-users"><a href="'+ value.url +'"><div class="hit-user-left"><img class="hit-user-avatar" src="'+ value.user_avatar_template + '" /></div><div class="hit-user-right"><div class="hit-user-username-holder"><span class="hit-user-username">@'+ value.user_username + '</span><span class="hit-user-custom-ranking" title="Number of likes the user has received"><span class="hit-user-like-heart"> ❤ </span>' + value.user_likes_received + '</span></div></div></a></div>'
161+
}else{
162+
return '<span></span>'
163+
}
164+
}
165+
}
166+
167+
}).on('typeahead:selected', function(event, datum) {
168+
window.location = datum.url;
169+
}).on('typeahead:asyncrequest', function() {
170+
$('.Typeahead-spinner').show();
171+
}).on('typeahead:asynccancel typeahead:asyncreceive', function() {
172+
$('.Typeahead-spinner').hide();
173+
});
174+
175+
$("#search-box").on('focus', function (event) {
176+
$(this).select();
177+
});
178+
}
179+
}
180+

assets/javascripts/initializers/discourse-elasticsearch.es6

Lines changed: 0 additions & 16 deletions
This file was deleted.
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import { h } from 'virtual-dom';
2+
import { on } from 'ember-addons/ember-computed-decorators';
3+
import DiscourseURL from 'discourse/lib/url';
4+
import { withPluginApi } from 'discourse/lib/plugin-api';
5+
import discourseAutocomplete from './discourse-autocomplete';
6+
7+
function elasticsearch(api){
8+
const container = api.container;
9+
const siteSettings = container.lookup("site-settings:main");
10+
api.modifyClass('component:site-header', {
11+
@on("didInsertElement")
12+
initializeElk() {
13+
this._super();
14+
var elasticsearch_address = this.siteSettings.elasticsearch_server_ip + ":" + this.siteSettings.elasticsearch_server_port + "/_search";
15+
if (this.siteSettings.elasticsearch_enabled) {
16+
$("body").addClass("elasticsearch-enabled");
17+
if (!this.siteSettings.elasticsearch_server_port) {
18+
elasticsearch_address = this.siteSettings.elasticsearch_server_ip + "/_search";
19+
}
20+
setTimeout(() => {
21+
discourseAutocomplete._initialize({
22+
elasticsearch_address: elasticsearch_address
23+
});
24+
}, 100);
25+
}
26+
}
27+
});
28+
29+
api.createWidget('es', {
30+
tagName: 'li.es-holder',
31+
html() {
32+
return [
33+
h('form', {
34+
action: '/search',
35+
method: 'GET'
36+
}, [
37+
h('input.es-input#search-box', {
38+
name: "q",
39+
placeholder: "Search the forum...",
40+
autocomplete: "off"
41+
}),
42+
h('img.Typeahead-spinner',{
43+
src: "https://hugelolcdn.com/comments/1225799.gif"
44+
})
45+
])
46+
];
47+
}
48+
});
49+
50+
api.decorateWidget('header-icons:before', function(helper) {
51+
if (helper.widget.siteSettings.elasticsearch_enabled) {
52+
return helper.attach('es');
53+
}
54+
});
55+
}
56+
57+
export default {
58+
name : "discourse-elasticsearch",
59+
initialize(container) {
60+
withPluginApi('0.8.8', api => elasticsearch(api, container));
61+
62+
}
63+
}

0 commit comments

Comments
 (0)