From 5f8dd4d2fec59313eb227b405a1d569948e152e1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=CC=88rg=20Prante?= Date: Fri, 4 Jun 2021 18:17:33 +0200 Subject: [PATCH] work an search results API --- .../java/org/xbib/elx/api/SearchDocument.java | 14 ++++++++ .../java/org/xbib/elx/api/SearchResult.java | 10 ++++++ .../elx/common/DefaultSearchDocument.java | 35 +++++++++++++++++++ .../xbib/elx/common/DefaultSearchResult.java | 31 ++++++++++++++++ 4 files changed, 90 insertions(+) create mode 100644 elx-api/src/main/java/org/xbib/elx/api/SearchDocument.java create mode 100644 elx-api/src/main/java/org/xbib/elx/api/SearchResult.java create mode 100644 elx-common/src/main/java/org/xbib/elx/common/DefaultSearchDocument.java create mode 100644 elx-common/src/main/java/org/xbib/elx/common/DefaultSearchResult.java diff --git a/elx-api/src/main/java/org/xbib/elx/api/SearchDocument.java b/elx-api/src/main/java/org/xbib/elx/api/SearchDocument.java new file mode 100644 index 0000000..6bbf691 --- /dev/null +++ b/elx-api/src/main/java/org/xbib/elx/api/SearchDocument.java @@ -0,0 +1,14 @@ +package org.xbib.elx.api; + +import java.util.Map; + +public interface SearchDocument { + + String getIndex(); + + String getId(); + + float getScore(); + + Map getFields(); +} diff --git a/elx-api/src/main/java/org/xbib/elx/api/SearchResult.java b/elx-api/src/main/java/org/xbib/elx/api/SearchResult.java new file mode 100644 index 0000000..7ea0dd1 --- /dev/null +++ b/elx-api/src/main/java/org/xbib/elx/api/SearchResult.java @@ -0,0 +1,10 @@ +package org.xbib.elx.api; + +import java.util.List; + +public interface SearchResult { + + long getTotal(); + + List getDocuments(); +} diff --git a/elx-common/src/main/java/org/xbib/elx/common/DefaultSearchDocument.java b/elx-common/src/main/java/org/xbib/elx/common/DefaultSearchDocument.java new file mode 100644 index 0000000..ef8ad6b --- /dev/null +++ b/elx-common/src/main/java/org/xbib/elx/common/DefaultSearchDocument.java @@ -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 getFields() { + return searchHit.sourceAsMap(); + } +} diff --git a/elx-common/src/main/java/org/xbib/elx/common/DefaultSearchResult.java b/elx-common/src/main/java/org/xbib/elx/common/DefaultSearchResult.java new file mode 100644 index 0000000..c445085 --- /dev/null +++ b/elx-common/src/main/java/org/xbib/elx/common/DefaultSearchResult.java @@ -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 getDocuments() { + List list = new ArrayList<>(); + for (SearchHit searchHit : searchHits.getHits()) { + list.add(new DefaultSearchDocument(searchHit)); + } + return list; + } +}