work an search results API
This commit is contained in:
parent
7007ddb465
commit
5f8dd4d2fe
4 changed files with 90 additions and 0 deletions
14
elx-api/src/main/java/org/xbib/elx/api/SearchDocument.java
Normal file
14
elx-api/src/main/java/org/xbib/elx/api/SearchDocument.java
Normal file
|
@ -0,0 +1,14 @@
|
|||
package org.xbib.elx.api;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
public interface SearchDocument {
|
||||
|
||||
String getIndex();
|
||||
|
||||
String getId();
|
||||
|
||||
float getScore();
|
||||
|
||||
Map<String, Object> getFields();
|
||||
}
|
10
elx-api/src/main/java/org/xbib/elx/api/SearchResult.java
Normal file
10
elx-api/src/main/java/org/xbib/elx/api/SearchResult.java
Normal file
|
@ -0,0 +1,10 @@
|
|||
package org.xbib.elx.api;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface SearchResult {
|
||||
|
||||
long getTotal();
|
||||
|
||||
List<SearchDocument> getDocuments();
|
||||
}
|
|
@ -0,0 +1,35 @@
|
|||
package org.xbib.elx.common;
|
||||
|
||||
import org.elasticsearch.search.SearchHit;
|
||||
import org.xbib.elx.api.SearchDocument;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
public class DefaultSearchDocument implements SearchDocument {
|
||||
|
||||
private final SearchHit searchHit;
|
||||
|
||||
public DefaultSearchDocument(SearchHit searchHit) {
|
||||
this.searchHit = searchHit;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getIndex() {
|
||||
return searchHit.getIndex();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getId() {
|
||||
return searchHit.getId();
|
||||
}
|
||||
|
||||
@Override
|
||||
public float getScore() {
|
||||
return searchHit.getScore();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<String, Object> getFields() {
|
||||
return searchHit.sourceAsMap();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,31 @@
|
|||
package org.xbib.elx.common;
|
||||
|
||||
import org.elasticsearch.search.SearchHit;
|
||||
import org.elasticsearch.search.SearchHits;
|
||||
import org.xbib.elx.api.SearchDocument;
|
||||
import org.xbib.elx.api.SearchResult;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class DefaultSearchResult implements SearchResult {
|
||||
|
||||
private final SearchHits searchHits;
|
||||
|
||||
public DefaultSearchResult(SearchHits searchHits) {
|
||||
this.searchHits = searchHits;
|
||||
}
|
||||
@Override
|
||||
public long getTotal() {
|
||||
return searchHits.getTotalHits();
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<SearchDocument> getDocuments() {
|
||||
List<SearchDocument> list = new ArrayList<>();
|
||||
for (SearchHit searchHit : searchHits.getHits()) {
|
||||
list.add(new DefaultSearchDocument(searchHit));
|
||||
}
|
||||
return list;
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue