Skip to content

Commit

Permalink
Introduces StaticMapQueryDecorator
Browse files Browse the repository at this point in the history
This decorator can replace static (not topic-dependent) variables into
templates.

This allow boost keywords experiments such as bst-mug#81.
  • Loading branch information
Michel Oleynik committed Jul 18, 2017
1 parent b48f340 commit fabed06
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 17 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package at.medunigraz.imi.bst.trec.query;

import java.util.Map;

public abstract class MapQueryDecorator extends QueryDecorator {

public MapQueryDecorator(Query decoratedQuery) {
super(decoratedQuery);
}

protected void map(Map<String, String> keymap) {
String jsonQuery = getJSONQuery();

for (Map.Entry<String, String> entry : keymap.entrySet()) {
String search = String.format("{{%s}}", entry.getKey());
jsonQuery = jsonQuery.replace(search, entry.getValue());
}

setJSONQuery(jsonQuery);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package at.medunigraz.imi.bst.trec.query;

import java.util.List;
import java.util.Map;

import at.medunigraz.imi.bst.trec.model.Result;
import at.medunigraz.imi.bst.trec.model.Topic;

public class StaticMapQueryDecorator extends MapQueryDecorator {

public StaticMapQueryDecorator(Map<String, String> keymap, Query decoratedQuery) {
super(decoratedQuery);
map(keymap);
}

@Override
public List<Result> query(Topic topic) {
return decoratedQuery.query(topic);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,22 @@
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Map;

import org.apache.commons.io.FileUtils;

import at.medunigraz.imi.bst.trec.model.Result;
import at.medunigraz.imi.bst.trec.model.Topic;

public class TemplateQueryDecorator extends QueryDecorator {

private String jsonTemplate;
public class TemplateQueryDecorator extends MapQueryDecorator {

public TemplateQueryDecorator(File template, Query decoratedQuery) {
super(decoratedQuery);
this.jsonTemplate = readTemplate(template);
setJSONQuery(readTemplate(template));
}

@Override
public List<Result> query(Topic topic) {
applyTemplate(topic);
map(topic.getAttributes());
return decoratedQuery.query(topic);
}

Expand All @@ -35,15 +32,4 @@ private String readTemplate(File template) {
return ret;
}

private void applyTemplate(Topic topic) {
String jsonQuery = jsonTemplate;

for (Map.Entry<String, String> entry : topic.getAttributes().entrySet()) {
String search = String.format("{{%s}}", entry.getKey());
jsonQuery = jsonQuery.replace(search, entry.getValue());
}

setJSONQuery(jsonQuery);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package at.medunigraz.imi.bst.trec.query;

import static org.junit.Assert.assertEquals;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

import org.junit.Test;

import at.medunigraz.imi.bst.trec.model.Topic;

public class StaticMapQueryDecoratorTest extends QueryDecoratorTest {

private static final String DISEASE = "thyroid";

private final File template = new File(getClass().getResource("/templates/match-title.json").getFile());

private static Map<String, String> keymap = new HashMap<>();

static {
keymap.put("disease", DISEASE);
}

public StaticMapQueryDecoratorTest() {

this.decoratedQuery = new StaticMapQueryDecorator(keymap,
new TemplateQueryDecorator(template, new ElasticSearchQuery("trec")));
this.topic = new Topic();
}

@Test
public void testGetJSONQuery() {
Query decoratedQuery = new StaticMapQueryDecorator(keymap,
new TemplateQueryDecorator(template, new DummyElasticSearchQuery()));
decoratedQuery.query(topic);

String actual = decoratedQuery.getJSONQuery();
String expected = String.format("{\"match\":{\"title\":\"%s\"}}", DISEASE);
assertEquals(expected, actual);
}

}

0 comments on commit fabed06

Please sign in to comment.