work an search results API

This commit is contained in:
Jörg Prante 2021-06-04 18:17:33 +02:00
parent c60bc4de96
commit f2e4d27750
4 changed files with 90 additions and 0 deletions

View 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();
}

View file

@ -0,0 +1,10 @@
package org.xbib.elx.api;
import java.util.List;
public interface SearchResult {
long getTotal();
List<SearchDocument> getDocuments();
}

View file

@ -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();
}
}

View file

@ -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;
}
}