Skip to content

Commit

Permalink
Support filtered queries via filter=, use in facet
Browse files Browse the repository at this point in the history
See #28
  • Loading branch information
fsteeg committed Oct 17, 2017
1 parent b66c906 commit 9b71b14
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 25 deletions.
20 changes: 10 additions & 10 deletions app/controllers/HomeController.java
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,11 @@ public Result index() {
public Result api() {
String format = "json";
ImmutableMap<String, String> searchSamples = ImmutableMap.of(//
"All", controllers.routes.HomeController.search("*", 0, 10, format).toString(), //
"All fields", controllers.routes.HomeController.search("london", 0, 10, format).toString(), //
"All", controllers.routes.HomeController.search("*", "", 0, 10, format).toString(), //
"All fields", controllers.routes.HomeController.search("london", "", 0, 10, format).toString(), //
"Field search",
controllers.routes.HomeController.search("type:CorporateBody", 0, 10, format).toString(), //
"Pagination", controllers.routes.HomeController.search("london", 50, 100, format).toString());
controllers.routes.HomeController.search("type:CorporateBody", "", 0, 10, format).toString(), //
"Pagination", controllers.routes.HomeController.search("london", "", 50, 100, format).toString());
ImmutableMap<String, String> getSamples = ImmutableMap.of(//
"London", controllers.routes.HomeController.authorityDotFormat("4074335-4", "json").toString(), //
"hbz", controllers.routes.HomeController.authorityDotFormat("2047974-8", "json").toString(), //
Expand Down Expand Up @@ -189,17 +189,17 @@ public Result gnd(String id) {
return ok(prettyJsonString(Json.parse(jsonLd))).as(config("index.content"));
}

public Result search(String q, int from, int size, String format) {
public Result search(String q, String filter, int from, int size, String format) {
String responseFormat = Accept.formatFor(format, request().acceptedTypes());
SearchResponse response = index.query(q, from, size);
SearchResponse response = index.query(q, filter, from, size);
response().setHeader("Access-Control-Allow-Origin", "*");
return responseFormat.equals("html") ? htmlSearch(q, from, size, format, response)
return responseFormat.equals("html") ? htmlSearch(q, filter, from, size, format, response)
: ok(returnAsJson(q, response)).as(config("index.content"));
}

private Result htmlSearch(String q, int from, int size, String format, SearchResponse response) {
return ok(
views.html.search.render(q, from, size, returnAsJson(q, response), response.getHits().getTotalHits()));
private Result htmlSearch(String q, String type, int from, int size, String format, SearchResponse response) {
return ok(views.html.search.render(q, type, from, size, returnAsJson(q, response),
response.getHits().getTotalHits()));
}

/**
Expand Down
2 changes: 1 addition & 1 deletion app/models/AuthorityResource.java
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ private String process(String string) {
if (string.startsWith(DNB_PREFIX)) {
label = labelFor(string.substring(DNB_PREFIX.length()));
link = string.replace(DNB_PREFIX, "/gnd/") + ".html";
search = controllers.routes.HomeController.search("\"" + string + "\"", 0, 10, "html").toString();
search = controllers.routes.HomeController.search("\"" + string + "\"", "", 0, 10, "html").toString();
if (label == null) {
label = string;
link = string;
Expand Down
9 changes: 6 additions & 3 deletions app/modules/IndexComponent.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,10 @@
public interface IndexComponent {
Client client();

SearchResponse query(String q, int from, int size);
SearchResponse query(String q, String type, int from, int size);

public default SearchResponse query(String q) {
return query(q, 0, 10);
return query(q, "", 0, 10);
}
}

Expand Down Expand Up @@ -193,13 +193,16 @@ private static void executeBulk(int pendingIndexRequests) {
}

@Override
public SearchResponse query(String q, int from, int size) {
public SearchResponse query(String q, String filter, int from, int size) {
QueryStringQueryBuilder query = QueryBuilders.queryStringQuery(q).field("_all").field("preferredName", 5)
.field("variantName", 5);
SearchRequestBuilder requestBuilder = client().prepareSearch(config("index.name")).setQuery(query).setFrom(from)
.setSize(size);
requestBuilder.addAggregation(
AggregationBuilders.terms(HomeController.TYPE).field(HomeController.TYPE + ".raw").size(1000));
if (!filter.isEmpty()) {
requestBuilder.setPostFilter(QueryBuilders.queryStringQuery(filter));
}
SearchResponse response = requestBuilder.get();
return response;
}
Expand Down
18 changes: 9 additions & 9 deletions app/views/search.scala.html
Original file line number Diff line number Diff line change
@@ -1,30 +1,30 @@
@(q: String, from: Int, size: Int, result: String, allHits: Long)
@(q: String, f: String, from: Int, size: Int, result: String, allHits: Long)

@import play.api.libs.json._

@pagination(hits:Int)={
<nav>
<ul class="pagination">
<li class="previous @if(from==0){disabled}">
<a href="@if(from==0){#} else {@routes.HomeController.search(q,from-size,size,"html")}">&larr;</a>
<a href="@if(from==0){#} else {@routes.HomeController.search(q,f,from-size,size,"html")}">&larr;</a>
</li>
@defining((((from+1)/size)+1,(if(allHits%size==0) allHits/size else allHits/size+1).toInt)) { case (currentPage,lastPage) =>
@defining(Math.min(Math.max(1,currentPage-4)+9,lastPage)) { toPage =>
@for(i <- Math.max(1,toPage-9) to toPage){
<li @if(currentPage==i){class="active"}><a href="@routes.HomeController.search(q,(i*size)-size,size,"html")">@i</a></li>
<li @if(currentPage==i){class="active"}><a href="@routes.HomeController.search(q,f,(i*size)-size,size,"html")">@i</a></li>
}
}
}
<li class="next @if(from+size >= allHits){disabled}">
<a href="@if(from+size >= allHits){#} else {@routes.HomeController.search(q,from+size,size,"html")}">&rarr;</a>
<a href="@if(from+size >= allHits){#} else {@routes.HomeController.search(q,f,from+size,size,"html")}">&rarr;</a>
</li>
</ul>
</nav>
}

@pageLink(num: Int)={
<li @if(size==num){class="active"}>
<a href="@routes.HomeController.search(q,from,num,"html")">@num</a>
<a href="@routes.HomeController.search(q,f,from,num,"html")">@num</a>
</li>
}

Expand Down Expand Up @@ -52,17 +52,17 @@
}

@links(buckets: Seq[JsValue], key: String) = {
@for(bucket <- buckets; term = (bucket \ "key").as[String]; if term != "AuthorityResource"; count = (bucket \ "doc_count").as[Int]; selected = q.contains(key+":\""+term+"\"")) {
@if(!q.contains("type:") || selected) {
@for(bucket <- buckets; term = (bucket \ "key").as[String]; if term != "AuthorityResource"; count = (bucket \ "doc_count").as[Int]; selected = f.contains(key+":"+term)) {
@if(!f.contains(key+":") || selected) {
<i class='@iconFor(Seq(term))' aria-hidden='true' title='@term'></i>
}
@if(selected){
@term
<a href='@routes.HomeController.search(q.replace("AND "+key+":\""+term+"\"","").trim, from, size, "html")'>
<a href='@routes.HomeController.search(q, "", from, size, "html")'>
<span class="badge">Filter entfernen <span class="glyphicon glyphicon-remove"></span></span>
</a>
} else {
<a href='@routes.HomeController.search()[email protected]+AND+@key:"@term"&from=@from&size=@size'>@term (@count)</a>
<a href='@routes.HomeController.search(q, key+":"+term, from, size, "html")'>@term (@count)</a>
}<br/>
}
}
Expand Down
2 changes: 1 addition & 1 deletion conf/routes
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ GET /gnd/api controllers.HomeController.api

GET /gnd/context.jsonld controllers.HomeController.context()

GET /gnd/search controllers.HomeController.search(q ?= "*", from: Int ?= 0, size: Int ?= 10, format ?= null)
GET /gnd/search controllers.HomeController.search(q ?= "*", filter ?= "", from: Int ?= 0, size: Int ?= 10, format ?= null)

GET /gnd/:id.:format controllers.HomeController.authorityDotFormat(id, format)

Expand Down
2 changes: 1 addition & 1 deletion test/controllers/HomeControllerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public static Collection<Object[]> data() {
{ routes.HomeController.authority("2-4", "json").toString(), Status.OK }, //
{ routes.HomeController.authority("1077774206", "json").toString(), Status.OK }, //
{ routes.HomeController.authority("1072719991", "json").toString(), Status.OK }, //
{ routes.HomeController.search("*", 0, 10, "json").toString(), Status.OK },
{ routes.HomeController.search("*", "", 0, 10, "json").toString(), Status.OK },
{ routes.HomeController.authority("---", "json").toString(), Status.NOT_FOUND } });
}

Expand Down

0 comments on commit 9b71b14

Please sign in to comment.