-
Notifications
You must be signed in to change notification settings - Fork 65
/
SearchableImpl.java
126 lines (99 loc) · 3.56 KB
/
SearchableImpl.java
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
package internal.org.springframework.content.fragments;
import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.content.commons.repository.StoreAccessException;
import org.springframework.content.commons.search.Searchable;
import static internal.org.springframework.content.elasticsearch.ElasticsearchIndexer.INDEX_NAME;
import static java.lang.String.format;
public class SearchableImpl implements Searchable<Serializable> {
private final RestHighLevelClient client;
private Class<?> domainClass;
public SearchableImpl() {
client = null;
domainClass = null;
}
@Autowired
public SearchableImpl(RestHighLevelClient client) {
this.client = client;
}
public void setDomainClass(Class<?> domainClass) {
this.domainClass = domainClass;
}
@Override
public Iterable<Serializable> search(String queryStr) {
SearchRequest searchRequest = new SearchRequest(INDEX_NAME);
searchRequest.types(domainClass.getName());
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
sourceBuilder.query(QueryBuilders.simpleQueryStringQuery(queryStr));
searchRequest.source(sourceBuilder);
SearchResponse res = null;
try {
res = client.search(searchRequest, RequestOptions.DEFAULT);
}
catch (IOException ioe) {
throw new StoreAccessException(format("Error searching indexed content for '%s'", queryStr), ioe);
}
return getIDs(res.getHits());
}
@Override
public Iterable<Serializable> findKeyword(String query) {
SearchRequest searchRequest = new SearchRequest(INDEX_NAME);
searchRequest.types(domainClass.getName());
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
sourceBuilder.query(QueryBuilders.queryStringQuery(query));
searchRequest.source(sourceBuilder);
SearchResponse res = null;
try {
res = client.search(searchRequest, RequestOptions.DEFAULT);
}
catch (IOException ioe) {
throw new StoreAccessException(format("Error searching indexed content for '%s'", query), ioe);
}
return getIDs(res.getHits());
}
@Override
public Iterable<Serializable> findAllKeywords(String... terms) {
throw new UnsupportedOperationException();
}
@Override
public Iterable<Serializable> findAnyKeywords(String... terms) {
throw new UnsupportedOperationException();
}
@Override
public Iterable<Serializable> findKeywordsNear(int proximity, String... terms) {
throw new UnsupportedOperationException();
}
@Override
public Iterable<Serializable> findKeywordStartsWith(String term) {
throw new UnsupportedOperationException();
}
@Override
public Iterable<Serializable> findKeywordStartsWithAndEndsWith(String a, String b) {
throw new UnsupportedOperationException();
}
@Override
public Iterable<Serializable> findAllKeywordsWithWeights(String[] terms, double[] weights) {
throw new UnsupportedOperationException();
}
private List<Serializable> getIDs(SearchHits result) {
List<Serializable> contents = new ArrayList<>();
if (result == null) {
return contents;
}
for (SearchHit hit : result.getHits()) {
contents.add(hit.getId());
}
return contents;
}
}