From a37f1f55cc437663084eacde78ca7bc6d23463c3 Mon Sep 17 00:00:00 2001 From: Simon Willnauer Date: Thu, 7 Mar 2013 12:29:22 +0100 Subject: [PATCH] Add tests for highlighting boost query. Closes #1314 --- .../highlight/HighlighterSearchTests.java | 57 +++++++++++++++++++ 1 file changed, 57 insertions(+) diff --git a/src/test/java/org/elasticsearch/test/integration/search/highlight/HighlighterSearchTests.java b/src/test/java/org/elasticsearch/test/integration/search/highlight/HighlighterSearchTests.java index 495c17fc794..e1a8ddd0e56 100644 --- a/src/test/java/org/elasticsearch/test/integration/search/highlight/HighlighterSearchTests.java +++ b/src/test/java/org/elasticsearch/test/integration/search/highlight/HighlighterSearchTests.java @@ -950,6 +950,63 @@ public class HighlighterSearchTests extends AbstractNodesTests { assertThat(response.getHits().hits()[0].highlightFields().get("tags").fragments()[0].string(), equalTo("this is a really long tag i would like to highlight")); assertThat(response.getHits().hits()[0].highlightFields().get("tags").fragments()[1].string(), equalTo("here is another one that is very long and has the tag token near the end")); } + + + @Test + public void testBoostingQuery() { + try { + client.admin().indices().prepareDelete("test").execute().actionGet(); + } catch (IndexMissingException e) { + // its ok + } + client.admin().indices().prepareCreate("test").execute().actionGet(); + client.admin().cluster().prepareHealth("test").setWaitForGreenStatus().execute().actionGet(); + + client.prepareIndex("test", "type1") + .setSource("field1", "this is a test", "field2", "The quick brown fox jumps over the lazy dog") + .setRefresh(true).execute().actionGet(); + + logger.info("--> highlighting and searching on field1"); + SearchSourceBuilder source = searchSource() + .query(boostingQuery().positive(termQuery("field2", "brown")).negative(termQuery("field2", "foobar")).negativeBoost(0.5f)) + .from(0).size(60).explain(true) + .highlight(highlight().field("field2").order("score").preTags("").postTags("")); + + SearchResponse searchResponse = client.search(searchRequest("test").source(source).searchType(QUERY_THEN_FETCH).scroll(timeValueMinutes(10))).actionGet(); + assertThat("Failures " + Arrays.toString(searchResponse.getShardFailures()), searchResponse.getShardFailures().length, equalTo(0)); + assertThat(searchResponse.getHits().totalHits(), equalTo(1l)); + + assertThat(searchResponse.getHits().getAt(0).highlightFields().get("field2").fragments()[0].string(), equalTo("The quick brown fox jumps over the lazy dog")); + } + + @Test + public void testBoostingQueryTermVector() throws ElasticSearchException, IOException { + try { + client.admin().indices().prepareDelete("test").execute().actionGet(); + } catch (Exception e) { + // ignore + } + client.admin().indices().prepareCreate("test").addMapping("type1", type1TermVectorMapping()).execute().actionGet(); + client.admin().cluster().prepareHealth("test").setWaitForGreenStatus().execute().actionGet(); + + client.prepareIndex("test", "type1").setSource("field1", "this is a test", "field2", "The quick brown fox jumps over the lazy dog") + .setRefresh(true).execute().actionGet(); + + logger.info("--> highlighting and searching on field1"); + SearchSourceBuilder source = searchSource() + .query(boostingQuery().positive(termQuery("field2", "brown")).negative(termQuery("field2", "foobar")).negativeBoost(0.5f)) + .from(0).size(60).explain(true) + .highlight(highlight().field("field2").order("score").preTags("").postTags("")); + + SearchResponse searchResponse = client.search( + searchRequest("test").source(source).searchType(QUERY_THEN_FETCH).scroll(timeValueMinutes(10))).actionGet(); + assertThat("Failures " + Arrays.toString(searchResponse.getShardFailures()), searchResponse.getShardFailures().length, equalTo(0)); + assertThat(searchResponse.getHits().totalHits(), equalTo(1l)); + + assertThat(searchResponse.getHits().getAt(0).highlightFields().get("field2").fragments()[0].string(), + equalTo("The quick brown fox jumps over the lazy dog")); + } + @Test public void testCommonTermsQuery() {