本文整理汇总了Java中org.elasticsearch.index.query.InnerHitBuilder类的典型用法代码示例。如果您正苦于以下问题:Java InnerHitBuilder类的具体用法?Java InnerHitBuilder怎么用?Java InnerHitBuilder使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
InnerHitBuilder类属于org.elasticsearch.index.query包,在下文中一共展示了InnerHitBuilder类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: testNestedInnerHitWrappedInParentChildInnerhit
import org.elasticsearch.index.query.InnerHitBuilder; //导入依赖的package包/类
public void testNestedInnerHitWrappedInParentChildInnerhit() throws Exception {
assertAcked(prepareCreate("test").addMapping("child_type", "_parent", "type=parent_type", "nested_type", "type=nested"));
client().prepareIndex("test", "parent_type", "1").setSource("key", "value").get();
client().prepareIndex("test", "child_type", "2").setParent("1").setSource("nested_type", Collections.singletonMap("key", "value"))
.get();
refresh();
SearchResponse response = client().prepareSearch("test")
.setQuery(boolQuery().must(matchQuery("key", "value"))
.should(hasChildQuery("child_type", nestedQuery("nested_type", matchAllQuery(), ScoreMode.None)
.innerHit(new InnerHitBuilder(), false), ScoreMode.None).innerHit(new InnerHitBuilder(), false)))
.get();
assertHitCount(response, 1);
SearchHit hit = response.getHits().getAt(0);
assertThat(hit.getInnerHits().get("child_type").getAt(0).field("_parent").getValue(), equalTo("1"));
assertThat(hit.getInnerHits().get("child_type").getAt(0).getInnerHits().get("nested_type").getAt(0).field("_parent"), nullValue());
}
示例2: testHasChildInnerHitsHighlighting
import org.elasticsearch.index.query.InnerHitBuilder; //导入依赖的package包/类
public void testHasChildInnerHitsHighlighting() throws Exception {
assertAcked(prepareCreate("test")
.addMapping("parent")
.addMapping("child", "_parent", "type=parent"));
ensureGreen();
client().prepareIndex("test", "parent", "1").setSource("p_field", 1).get();
client().prepareIndex("test", "child", "2").setParent("1").setSource("c_field", "foo bar").get();
client().admin().indices().prepareFlush("test").get();
SearchResponse searchResponse = client().prepareSearch("test").setQuery(
hasChildQuery("child", matchQuery("c_field", "foo"), ScoreMode.None)
.innerHit(new InnerHitBuilder().setHighlightBuilder(
new HighlightBuilder().field(new Field("c_field")
.highlightQuery(QueryBuilders.matchQuery("c_field", "bar")))), false))
.get();
assertNoFailures(searchResponse);
assertThat(searchResponse.getHits().getTotalHits(), equalTo(1L));
assertThat(searchResponse.getHits().getHits()[0].getId(), equalTo("1"));
SearchHit[] searchHits = searchResponse.getHits().getHits()[0].getInnerHits().get("child").getHits();
assertThat(searchHits.length, equalTo(1));
assertThat(searchHits[0].getHighlightFields().get("c_field").getFragments().length, equalTo(1));
assertThat(searchHits[0].getHighlightFields().get("c_field").getFragments()[0].string(), equalTo("foo <em>bar</em>"));
}
示例3: getAllMetaAlertsForAlert
import org.elasticsearch.index.query.InnerHitBuilder; //导入依赖的package包/类
@Override
public SearchResponse getAllMetaAlertsForAlert(String guid) throws InvalidSearchException {
if (guid == null || guid.trim().isEmpty()) {
throw new InvalidSearchException("Guid cannot be empty");
}
// Searches for all alerts containing the meta alert guid in it's "metalerts" array
QueryBuilder qb = boolQuery()
.must(
nestedQuery(
ALERT_FIELD,
boolQuery()
.must(termQuery(ALERT_FIELD + "." + GUID, guid)),
ScoreMode.None
).innerHit(new InnerHitBuilder())
)
.must(termQuery(STATUS_FIELD, MetaAlertStatus.ACTIVE.getStatusString()));
return queryAllResults(qb);
}
示例4: buildExpandSearchSourceBuilder
import org.elasticsearch.index.query.InnerHitBuilder; //导入依赖的package包/类
private SearchSourceBuilder buildExpandSearchSourceBuilder(InnerHitBuilder options) {
SearchSourceBuilder groupSource = new SearchSourceBuilder();
groupSource.from(options.getFrom());
groupSource.size(options.getSize());
if (options.getSorts() != null) {
options.getSorts().forEach(groupSource::sort);
}
if (options.getFetchSourceContext() != null) {
if (options.getFetchSourceContext().includes() == null && options.getFetchSourceContext().excludes() == null) {
groupSource.fetchSource(options.getFetchSourceContext().fetchSource());
} else {
groupSource.fetchSource(options.getFetchSourceContext().includes(),
options.getFetchSourceContext().excludes());
}
}
if (options.getDocValueFields() != null) {
options.getDocValueFields().forEach(groupSource::docValueField);
}
if (options.getStoredFieldsContext() != null && options.getStoredFieldsContext().fieldNames() != null) {
options.getStoredFieldsContext().fieldNames().forEach(groupSource::storedField);
}
if (options.getScriptFields() != null) {
for (SearchSourceBuilder.ScriptField field : options.getScriptFields()) {
groupSource.scriptField(field.fieldName(), field.script());
}
}
if (options.getHighlightBuilder() != null) {
groupSource.highlighter(options.getHighlightBuilder());
}
groupSource.explain(options.isExplain());
groupSource.trackScores(options.isTrackScores());
return groupSource;
}
示例5: testNestedDefinedAsObject
import org.elasticsearch.index.query.InnerHitBuilder; //导入依赖的package包/类
public void testNestedDefinedAsObject() throws Exception {
assertAcked(prepareCreate("articles").addMapping("article", "comments", "type=nested", "title", "type=text"));
List<IndexRequestBuilder> requests = new ArrayList<>();
requests.add(client().prepareIndex("articles", "article", "1").setSource(jsonBuilder().startObject()
.field("title", "quick brown fox")
.startObject("comments").field("message", "fox eat quick").endObject()
.endObject()));
indexRandom(true, requests);
SearchResponse response = client().prepareSearch("articles")
.setQuery(nestedQuery("comments", matchQuery("comments.message", "fox"), ScoreMode.Avg)
.innerHit(new InnerHitBuilder(), false))
.get();
assertNoFailures(response);
assertHitCount(response, 1);
assertThat(response.getHits().getAt(0).getId(), equalTo("1"));
assertThat(response.getHits().getAt(0).getInnerHits().get("comments").getTotalHits(), equalTo(1L));
assertThat(response.getHits().getAt(0).getInnerHits().get("comments").getAt(0).getId(), equalTo("1"));
assertThat(response.getHits().getAt(0).getInnerHits().get("comments").getAt(0).getNestedIdentity().getField().string(),
equalTo("comments"));
assertThat(response.getHits().getAt(0).getInnerHits().get("comments").getAt(0).getNestedIdentity().getOffset(), equalTo(0));
assertThat(response.getHits().getAt(0).getInnerHits().get("comments").getAt(0).getNestedIdentity().getChild(), nullValue());
}
示例6: testMatchesQueriesParentChildInnerHits
import org.elasticsearch.index.query.InnerHitBuilder; //导入依赖的package包/类
public void testMatchesQueriesParentChildInnerHits() throws Exception {
assertAcked(prepareCreate("index").addMapping("child", "_parent", "type=parent"));
List<IndexRequestBuilder> requests = new ArrayList<>();
requests.add(client().prepareIndex("index", "parent", "1").setSource("{}", XContentType.JSON));
requests.add(client().prepareIndex("index", "child", "1").setParent("1").setSource("field", "value1"));
requests.add(client().prepareIndex("index", "child", "2").setParent("1").setSource("field", "value2"));
requests.add(client().prepareIndex("index", "parent", "2").setSource("{}", XContentType.JSON));
requests.add(client().prepareIndex("index", "child", "3").setParent("2").setSource("field", "value1"));
indexRandom(true, requests);
SearchResponse response = client().prepareSearch("index")
.setQuery(hasChildQuery("child", matchQuery("field", "value1").queryName("_name1"), ScoreMode.None)
.innerHit(new InnerHitBuilder(), false))
.addSort("_uid", SortOrder.ASC)
.get();
assertHitCount(response, 2);
assertThat(response.getHits().getAt(0).getId(), equalTo("1"));
assertThat(response.getHits().getAt(0).getInnerHits().get("child").getTotalHits(), equalTo(1L));
assertThat(response.getHits().getAt(0).getInnerHits().get("child").getAt(0).getMatchedQueries().length, equalTo(1));
assertThat(response.getHits().getAt(0).getInnerHits().get("child").getAt(0).getMatchedQueries()[0], equalTo("_name1"));
assertThat(response.getHits().getAt(1).getId(), equalTo("2"));
assertThat(response.getHits().getAt(1).getInnerHits().get("child").getTotalHits(), equalTo(1L));
assertThat(response.getHits().getAt(1).getInnerHits().get("child").getAt(0).getMatchedQueries().length, equalTo(1));
assertThat(response.getHits().getAt(1).getInnerHits().get("child").getAt(0).getMatchedQueries()[0], equalTo("_name1"));
QueryBuilder query = hasChildQuery("child", matchQuery("field", "value2").queryName("_name2"), ScoreMode.None)
.innerHit(new InnerHitBuilder(), false);
response = client().prepareSearch("index")
.setQuery(query)
.addSort("_uid", SortOrder.ASC)
.get();
assertHitCount(response, 1);
assertThat(response.getHits().getAt(0).getId(), equalTo("1"));
assertThat(response.getHits().getAt(0).getInnerHits().get("child").getTotalHits(), equalTo(1L));
assertThat(response.getHits().getAt(0).getInnerHits().get("child").getAt(0).getMatchedQueries().length, equalTo(1));
assertThat(response.getHits().getAt(0).getInnerHits().get("child").getAt(0).getMatchedQueries()[0], equalTo("_name2"));
}
示例7: testDontExplode
import org.elasticsearch.index.query.InnerHitBuilder; //导入依赖的package包/类
public void testDontExplode() throws Exception {
assertAcked(prepareCreate("index1").addMapping("child", "_parent", "type=parent"));
List<IndexRequestBuilder> requests = new ArrayList<>();
requests.add(client().prepareIndex("index1", "parent", "1").setSource("{}", XContentType.JSON));
requests.add(client().prepareIndex("index1", "child", "1").setParent("1").setSource("field", "value1"));
indexRandom(true, requests);
QueryBuilder query = hasChildQuery("child", matchQuery("field", "value1"), ScoreMode.None)
.innerHit(new InnerHitBuilder().setSize(ArrayUtil.MAX_ARRAY_LENGTH - 1), false);
SearchResponse response = client().prepareSearch("index1")
.setQuery(query)
.addSort("_uid", SortOrder.ASC)
.get();
assertNoFailures(response);
assertHitCount(response, 1);
assertAcked(prepareCreate("index2").addMapping("type", "nested", "type=nested"));
client().prepareIndex("index2", "type", "1").setSource(jsonBuilder().startObject()
.startArray("nested")
.startObject()
.field("field", "value1")
.endObject()
.endArray()
.endObject())
.setRefreshPolicy(IMMEDIATE)
.get();
query = nestedQuery("nested", matchQuery("nested.field", "value1"), ScoreMode.Avg)
.innerHit(new InnerHitBuilder().setSize(ArrayUtil.MAX_ARRAY_LENGTH - 1), false);
response = client().prepareSearch("index2")
.setQuery(query)
.addSort("_uid", SortOrder.ASC)
.get();
assertNoFailures(response);
assertHitCount(response, 1);
}
示例8: testNestedSourceFiltering
import org.elasticsearch.index.query.InnerHitBuilder; //导入依赖的package包/类
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"));
}
示例9: testInnerHitsWithIgnoreUnmapped
import org.elasticsearch.index.query.InnerHitBuilder; //导入依赖的package包/类
public void testInnerHitsWithIgnoreUnmapped() throws Exception {
assertAcked(prepareCreate("index1")
.addMapping("parent_type", "nested_type", "type=nested")
.addMapping("child_type", "_parent", "type=parent_type")
);
createIndex("index2");
client().prepareIndex("index1", "parent_type", "1").setSource("nested_type", Collections.singletonMap("key", "value")).get();
client().prepareIndex("index1", "child_type", "2").setParent("1").setSource("{}", XContentType.JSON).get();
client().prepareIndex("index2", "type", "3").setSource("key", "value").get();
refresh();
SearchResponse response = client().prepareSearch("index1", "index2")
.setQuery(boolQuery()
.should(nestedQuery("nested_type", matchAllQuery(), ScoreMode.None).ignoreUnmapped(true)
.innerHit(new InnerHitBuilder(), true))
.should(termQuery("key", "value"))
)
.addSort("_uid", SortOrder.ASC)
.get();
assertNoFailures(response);
assertHitCount(response, 2);
assertThat(response.getHits().getAt(0).getId(), equalTo("1"));
response = client().prepareSearch("index1", "index2")
.setQuery(boolQuery()
.should(hasChildQuery("child_type", matchAllQuery(), ScoreMode.None).ignoreUnmapped(true)
.innerHit(new InnerHitBuilder(), true))
.should(termQuery("key", "value"))
)
.addSort("_uid", SortOrder.ASC)
.get();
assertNoFailures(response);
assertHitCount(response, 2);
assertThat(response.getHits().getAt(0).getId(), equalTo("1"));
}
示例10: randomCollapseBuilder
import org.elasticsearch.index.query.InnerHitBuilder; //导入依赖的package包/类
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;
}
示例11: testBuild
import org.elasticsearch.index.query.InnerHitBuilder; //导入依赖的package包/类
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`");
}
}
}
示例12: getMetaAlertsForAlert
import org.elasticsearch.index.query.InnerHitBuilder; //导入依赖的package包/类
/**
* Given an alert GUID, retrieve all associated meta alerts.
* @param alertGuid The GUID of the child alert
* @return The Elasticsearch response containing the meta alerts
*/
protected SearchResponse getMetaAlertsForAlert(String alertGuid) {
QueryBuilder qb = boolQuery()
.must(
nestedQuery(
ALERT_FIELD,
boolQuery()
.must(termQuery(ALERT_FIELD + "." + Constants.GUID, alertGuid)),
ScoreMode.None
).innerHit(new InnerHitBuilder())
)
.must(termQuery(STATUS_FIELD, MetaAlertStatus.ACTIVE.getStatusString()));
return queryAllResults(qb);
}
示例13: populateBuffer
import org.elasticsearch.index.query.InnerHitBuilder; //导入依赖的package包/类
@Override
protected void populateBuffer() {
// not used yet or returned empty results
if (lastDate == null) {
lastDate = new Date();
lastStartOffset = 0;
}
// been running same query for too long and paging deep?
else if (maxStartOffset != -1 && lastStartOffset > maxStartOffset) {
LOG.info("Reached max start offset {}", lastStartOffset);
lastStartOffset = 0;
}
String formattedLastDate = ISODateTimeFormat.dateTimeNoMillis().print(
lastDate.getTime());
LOG.info("{} Populating buffer with nextFetchDate <= {}", logIdprefix,
formattedLastDate);
QueryBuilder queryBuilder = QueryBuilders.rangeQuery("nextFetchDate")
.lte(formattedLastDate);
if (filterQuery != null) {
queryBuilder = boolQuery().must(queryBuilder).filter(
QueryBuilders.queryStringQuery(filterQuery));
}
SearchRequestBuilder srb = client.prepareSearch(indexName)
.setTypes(docType).setSearchType(SearchType.QUERY_THEN_FETCH)
.setQuery(queryBuilder).setFrom(lastStartOffset)
.setSize(maxBucketNum).setExplain(false);
// https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-preference.html
// _shards:2,3
if (shardID != -1) {
srb.setPreference("_shards:" + shardID);
}
if (StringUtils.isNotBlank(totalSortField)) {
FieldSortBuilder sorter = SortBuilders.fieldSort(totalSortField)
.order(SortOrder.ASC);
srb.addSort(sorter);
}
CollapseBuilder collapse = new CollapseBuilder(partitionField);
srb.setCollapse(collapse);
// group expansion -> sends sub queries for each bucket
if (maxURLsPerBucket > 1) {
InnerHitBuilder ihb = new InnerHitBuilder();
ihb.setSize(maxURLsPerBucket);
ihb.setName("urls_per_bucket");
// sort within a bucket
if (StringUtils.isNotBlank(bucketSortField)) {
List<SortBuilder<?>> sorts = new LinkedList<>();
FieldSortBuilder bucketsorter = SortBuilders.fieldSort(
bucketSortField).order(SortOrder.ASC);
sorts.add(bucketsorter);
ihb.setSorts(sorts);
}
collapse.setInnerHits(ihb);
}
// dump query to log
LOG.debug("{} ES query {}", logIdprefix, srb.toString());
timeStartESQuery = System.currentTimeMillis();
isInESQuery.set(true);
srb.execute(this);
}
示例14: CollapseContext
import org.elasticsearch.index.query.InnerHitBuilder; //导入依赖的package包/类
public CollapseContext(MappedFieldType fieldType, InnerHitBuilder innerHit) {
this.fieldType = fieldType;
this.innerHit = innerHit;
}
示例15: getInnerHit
import org.elasticsearch.index.query.InnerHitBuilder; //导入依赖的package包/类
/** The inner hit options to expand the collapsed results **/
public InnerHitBuilder getInnerHit() {
return innerHit;
}