Skip to content

Commit

Permalink
Merge pull request #7 from jguyomard/feature/improve-search
Browse files Browse the repository at this point in the history
Improve search
  • Loading branch information
jguyomard authored Oct 17, 2017
2 parents bee9d5a + caf3064 commit 98a2c97
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 4 deletions.
1 change: 1 addition & 0 deletions API.apib
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ To install this bot, follow [installation instructions](https://github.com/jguyo
+ Default: `0`
+ search (string, optional) - Search by term.
+ url (string, optional) - Search by url.
+ sharedby (string, optional) - Filter links shared by this user.

+ Response 200 (application/json)

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ This bot use Elastic Search to save links. The client connects to Elasticsearch

For instance, to start Elastic Search using official docker image:
```
docker run -d -p 9200:9200 elasticsearch
docker run -d -p 9200:9200 elasticsearch:2
```

### 4. Run this bot
Expand Down
13 changes: 13 additions & 0 deletions docs/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,19 @@ <h3>GET</h3>
</td>
</tr>

<tr>
<td class="center aligned one wide">
<i class="ui empty circular label grey" data-content="optional" data-position="top center"></i>
</td>
<td><code>sharedby</code></td>
<td class="center aligned">
<code>string<code>

</td>
<td class="eight wide"><p>Filter links shared by this user.</p>
</td>
</tr>

</tbody>
</table>

Expand Down
24 changes: 22 additions & 2 deletions src/links/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ import (
)

const (
esIndex = "slackbot-links"
esType = "links"
esIndex = "slackbot-links"
esType = "links"
esAnalyzer = "french"
)

var (
Expand Down Expand Up @@ -75,9 +76,28 @@ func createESIndexIfNeeded() bool {
"type": "string",
"index": "not_analyzed"
},
"title":{
"type": "string",
"analyzer": "` + esAnalyzer + `"
},
"author":{
"type": "string"
},
"excerpt":{
"type": "string",
"analyzer": "` + esAnalyzer + `"
},
"published_at":{
"type":"date"
},
"image_url":{
"type": "string",
"index": "not_analyzed"
},
"content":{
"type": "string",
"analyzer": "` + esAnalyzer + `"
},
"shared_at":{
"type":"date"
}
Expand Down
8 changes: 7 additions & 1 deletion src/links/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,14 +79,17 @@ func (r *SearchResult) GetCursor() map[string]interface{} {
func getQuery(params url.Values) *elastic.BoolQuery {
search := params.Get("search")
url := params.Get("url")
if search == "" && url == "" {
sharedby := params.Get("sharedby")
if search == "" && url == "" && sharedby == "" {
return nil
}

query := elastic.NewBoolQuery()
if search != "" {
matchQuery := elastic.NewSimpleQueryStringQuery(search).
DefaultOperator("and").
Field("shared_by.name^4").
Field("shared_on.name^4").
Field("title^3").
Field("url^3").
Field("author^2").
Expand All @@ -97,6 +100,9 @@ func getQuery(params url.Values) *elastic.BoolQuery {
if url != "" {
query.Must(elastic.NewTermQuery("url", url))
}
if sharedby != "" {
query.Must(elastic.NewTermQuery("shared_by.name", sharedby))
}
return query
}

Expand Down

0 comments on commit 98a2c97

Please sign in to comment.