use of org.elasticsearch.index.query.InnerHitBuilder in project elasticsearch by elastic.
the class ExpandSearchPhaseTests method testFailOneItemFailsEntirePhase.
public void testFailOneItemFailsEntirePhase() throws IOException {
AtomicBoolean executedMultiSearch = new AtomicBoolean(false);
SearchHits collapsedHits = new SearchHits(new SearchHit[] { new SearchHit(2, "ID", new Text("type"), Collections.emptyMap()), new SearchHit(3, "ID", new Text("type"), Collections.emptyMap()) }, 1, 1.0F);
MockSearchPhaseContext mockSearchPhaseContext = new MockSearchPhaseContext(1);
String collapseValue = randomBoolean() ? null : "boom";
mockSearchPhaseContext.getRequest().source(new SearchSourceBuilder().collapse(new CollapseBuilder("someField").setInnerHits(new InnerHitBuilder().setName("foobarbaz"))));
mockSearchPhaseContext.searchTransport = new SearchTransportService(Settings.builder().put("search.remote.connect", false).build(), null, null) {
@Override
void sendExecuteMultiSearch(MultiSearchRequest request, SearchTask task, ActionListener<MultiSearchResponse> listener) {
assertTrue(executedMultiSearch.compareAndSet(false, true));
InternalSearchResponse internalSearchResponse = new InternalSearchResponse(collapsedHits, null, null, null, false, null, 1);
SearchResponse response = mockSearchPhaseContext.buildSearchResponse(internalSearchResponse, null);
listener.onResponse(new MultiSearchResponse(new MultiSearchResponse.Item[] { new MultiSearchResponse.Item(null, new RuntimeException("boom")), new MultiSearchResponse.Item(response, null) }));
}
};
SearchHits hits = new SearchHits(new SearchHit[] { new SearchHit(1, "ID", new Text("type"), Collections.singletonMap("someField", new SearchHitField("someField", Collections.singletonList(collapseValue)))), new SearchHit(2, "ID2", new Text("type"), Collections.singletonMap("someField", new SearchHitField("someField", Collections.singletonList(collapseValue)))) }, 1, 1.0F);
InternalSearchResponse internalSearchResponse = new InternalSearchResponse(hits, null, null, null, false, null, 1);
SearchResponse response = mockSearchPhaseContext.buildSearchResponse(internalSearchResponse, null);
AtomicReference<SearchResponse> reference = new AtomicReference<>();
ExpandSearchPhase phase = new ExpandSearchPhase(mockSearchPhaseContext, response, r -> new SearchPhase("test") {
@Override
public void run() throws IOException {
reference.set(r);
}
});
phase.run();
assertThat(mockSearchPhaseContext.phaseFailure.get(), Matchers.instanceOf(RuntimeException.class));
assertEquals("boom", mockSearchPhaseContext.phaseFailure.get().getMessage());
assertNotNull(mockSearchPhaseContext.phaseFailure.get());
assertNull(reference.get());
assertEquals(0, mockSearchPhaseContext.phasesExecuted.get());
}
use of org.elasticsearch.index.query.InnerHitBuilder in project elasticsearch by elastic.
the class CollapseBuilderTests method randomCollapseBuilder.
public static CollapseBuilder randomCollapseBuilder() {
CollapseBuilder builder = new CollapseBuilder(randomAsciiOfLength(10));
builder.setMaxConcurrentGroupRequests(randomIntBetween(1, 48));
if (randomBoolean()) {
InnerHitBuilder innerHit = InnerHitBuilderTests.randomInnerHits(false, false);
builder.setInnerHits(innerHit);
}
return builder;
}
use of org.elasticsearch.index.query.InnerHitBuilder in project elasticsearch by elastic.
the class CollapseBuilderTests method testBuild.
public void testBuild() throws IOException {
Directory dir = new RAMDirectory();
try (IndexWriter writer = new IndexWriter(dir, newIndexWriterConfig(new MockAnalyzer(random())))) {
writer.commit();
}
SearchContext searchContext = mockSearchContext();
try (IndexReader reader = DirectoryReader.open(dir)) {
when(searchContext.getQueryShardContext().getIndexReader()).thenReturn(reader);
MappedFieldType numberFieldType = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.LONG);
MappedFieldType keywordFieldType = new KeywordFieldMapper.KeywordFieldType();
for (MappedFieldType fieldType : new MappedFieldType[] { numberFieldType, keywordFieldType }) {
fieldType.setName("field");
fieldType.setHasDocValues(true);
when(searchContext.getQueryShardContext().fieldMapper("field")).thenReturn(fieldType);
CollapseBuilder builder = new CollapseBuilder("field");
CollapseContext collapseContext = builder.build(searchContext);
assertEquals(collapseContext.getFieldType(), fieldType);
fieldType.setIndexOptions(IndexOptions.NONE);
collapseContext = builder.build(searchContext);
assertEquals(collapseContext.getFieldType(), fieldType);
fieldType.setHasDocValues(false);
SearchContextException exc = expectThrows(SearchContextException.class, () -> builder.build(searchContext));
assertEquals(exc.getMessage(), "cannot collapse on field `field` without `doc_values`");
fieldType.setHasDocValues(true);
builder.setInnerHits(new InnerHitBuilder());
exc = expectThrows(SearchContextException.class, () -> builder.build(searchContext));
assertEquals(exc.getMessage(), "cannot expand `inner_hits` for collapse field `field`, " + "only indexed field can retrieve `inner_hits`");
}
}
}
use of org.elasticsearch.index.query.InnerHitBuilder in project elasticsearch by elastic.
the class InnerHitsIT method testSimpleNested.
public void testSimpleNested() throws Exception {
assertAcked(prepareCreate("articles").addMapping("article", jsonBuilder().startObject().startObject("article").startObject("properties").startObject("comments").field("type", "nested").startObject("properties").startObject("message").field("type", "text").field("fielddata", true).endObject().endObject().endObject().startObject("title").field("type", "text").endObject().endObject().endObject().endObject()));
List<IndexRequestBuilder> requests = new ArrayList<>();
requests.add(client().prepareIndex("articles", "article", "1").setSource(jsonBuilder().startObject().field("title", "quick brown fox").startArray("comments").startObject().field("message", "fox eat quick").endObject().startObject().field("message", "fox ate rabbit x y z").endObject().startObject().field("message", "rabbit got away").endObject().endArray().endObject()));
requests.add(client().prepareIndex("articles", "article", "2").setSource(jsonBuilder().startObject().field("title", "big gray elephant").startArray("comments").startObject().field("message", "elephant captured").endObject().startObject().field("message", "mice squashed by elephant x").endObject().startObject().field("message", "elephant scared by mice x y").endObject().endArray().endObject()));
indexRandom(true, requests);
SearchResponse response = client().prepareSearch("articles").setQuery(nestedQuery("comments", matchQuery("comments.message", "fox"), ScoreMode.Avg).innerHit(new InnerHitBuilder().setName("comment"), false)).get();
assertNoFailures(response);
assertHitCount(response, 1);
assertSearchHit(response, 1, hasId("1"));
assertThat(response.getHits().getAt(0).getInnerHits().size(), equalTo(1));
SearchHits innerHits = response.getHits().getAt(0).getInnerHits().get("comment");
assertThat(innerHits.getTotalHits(), equalTo(2L));
assertThat(innerHits.getHits().length, equalTo(2));
assertThat(innerHits.getAt(0).getId(), equalTo("1"));
assertThat(innerHits.getAt(0).getNestedIdentity().getField().string(), equalTo("comments"));
assertThat(innerHits.getAt(0).getNestedIdentity().getOffset(), equalTo(0));
assertThat(innerHits.getAt(1).getId(), equalTo("1"));
assertThat(innerHits.getAt(1).getNestedIdentity().getField().string(), equalTo("comments"));
assertThat(innerHits.getAt(1).getNestedIdentity().getOffset(), equalTo(1));
response = client().prepareSearch("articles").setQuery(nestedQuery("comments", matchQuery("comments.message", "elephant"), ScoreMode.Avg).innerHit(new InnerHitBuilder().setName("comment"), false)).get();
assertNoFailures(response);
assertHitCount(response, 1);
assertSearchHit(response, 1, hasId("2"));
assertThat(response.getHits().getAt(0).getShard(), notNullValue());
assertThat(response.getHits().getAt(0).getInnerHits().size(), equalTo(1));
innerHits = response.getHits().getAt(0).getInnerHits().get("comment");
assertThat(innerHits.getTotalHits(), equalTo(3L));
assertThat(innerHits.getHits().length, equalTo(3));
assertThat(innerHits.getAt(0).getId(), equalTo("2"));
assertThat(innerHits.getAt(0).getNestedIdentity().getField().string(), equalTo("comments"));
assertThat(innerHits.getAt(0).getNestedIdentity().getOffset(), equalTo(0));
assertThat(innerHits.getAt(1).getId(), equalTo("2"));
assertThat(innerHits.getAt(1).getNestedIdentity().getField().string(), equalTo("comments"));
assertThat(innerHits.getAt(1).getNestedIdentity().getOffset(), equalTo(1));
assertThat(innerHits.getAt(2).getId(), equalTo("2"));
assertThat(innerHits.getAt(2).getNestedIdentity().getField().string(), equalTo("comments"));
assertThat(innerHits.getAt(2).getNestedIdentity().getOffset(), equalTo(2));
response = client().prepareSearch("articles").setQuery(nestedQuery("comments", matchQuery("comments.message", "fox"), ScoreMode.Avg).innerHit(new InnerHitBuilder().setHighlightBuilder(new HighlightBuilder().field("comments.message")).setExplain(true).addDocValueField("comments.message").addScriptField("script", new Script(ScriptType.INLINE, MockScriptEngine.NAME, "5", Collections.emptyMap())).setSize(1), false)).get();
assertNoFailures(response);
innerHits = response.getHits().getAt(0).getInnerHits().get("comments");
assertThat(innerHits.getTotalHits(), equalTo(2L));
assertThat(innerHits.getHits().length, equalTo(1));
assertThat(innerHits.getAt(0).getHighlightFields().get("comments.message").getFragments()[0].string(), equalTo("<em>fox</em> eat quick"));
assertThat(innerHits.getAt(0).getExplanation().toString(), containsString("weight(comments.message:fox in"));
assertThat(innerHits.getAt(0).getFields().get("comments.message").getValue().toString(), equalTo("eat"));
assertThat(innerHits.getAt(0).getFields().get("script").getValue().toString(), equalTo("5"));
}
use of org.elasticsearch.index.query.InnerHitBuilder in project elasticsearch by elastic.
the class InnerHitsIT method testNestedSourceFiltering.
public void testNestedSourceFiltering() throws Exception {
assertAcked(prepareCreate("index1").addMapping("message", "comments", "type=nested"));
client().prepareIndex("index1", "message", "1").setSource(jsonBuilder().startObject().field("message", "quick brown fox").startArray("comments").startObject().field("message", "fox eat quick").endObject().startObject().field("message", "fox ate rabbit x y z").endObject().startObject().field("message", "rabbit got away").endObject().endArray().endObject()).get();
refresh();
// the field name (comments.message) used for source filtering should be the same as when using that field for
// other features (like in the query dsl or aggs) in order for consistency:
SearchResponse response = client().prepareSearch().setQuery(nestedQuery("comments", matchQuery("comments.message", "fox"), ScoreMode.None).innerHit(new InnerHitBuilder().setFetchSourceContext(new FetchSourceContext(true, new String[] { "comments.message" }, null)), false)).get();
assertNoFailures(response);
assertHitCount(response, 1);
assertThat(response.getHits().getAt(0).getInnerHits().get("comments").getTotalHits(), equalTo(2L));
assertThat(extractValue("comments.message", response.getHits().getAt(0).getInnerHits().get("comments").getAt(0).getSourceAsMap()), equalTo("fox eat quick"));
assertThat(extractValue("comments.message", response.getHits().getAt(0).getInnerHits().get("comments").getAt(1).getSourceAsMap()), equalTo("fox ate rabbit x y z"));
}
Aggregations