static SearchContext createSearchContext(String indexName, String parentType, String childType)
     throws IOException {
   Settings settings =
       Settings.builder().put(IndexMetaData.SETTING_VERSION_CREATED, Version.V_1_6_0).build();
   IndexService indexService = createIndex(indexName, settings);
   MapperService mapperService = indexService.mapperService();
   // Parent/child parsers require that the parent and child type to be presented in mapping
   // Sometimes we want a nested object field in the parent type that triggers nonNestedDocsFilter
   // to be used
   mapperService.merge(
       parentType,
       new CompressedXContent(
           PutMappingRequest.buildFromSimplifiedDef(
                   parentType,
                   "nested_field",
                   random().nextBoolean() ? "type=nested" : "type=object")
               .string()),
       true,
       false);
   mapperService.merge(
       childType,
       new CompressedXContent(
           PutMappingRequest.buildFromSimplifiedDef(
                   childType,
                   "_parent",
                   "type=" + parentType,
                   CHILD_SCORE_NAME,
                   "type=double,doc_values=false")
               .string()),
       true,
       false);
   return createSearchContext(indexService);
 }
 @Override
 protected void initializeAdditionalMappings(MapperService mapperService) throws IOException {
   mapperService.merge(
       PARENT_TYPE,
       new CompressedXContent(
           PutMappingRequest.buildFromSimplifiedDef(
                   PARENT_TYPE,
                   STRING_FIELD_NAME,
                   "type=text",
                   STRING_FIELD_NAME_2,
                   "type=keyword",
                   INT_FIELD_NAME,
                   "type=integer",
                   DOUBLE_FIELD_NAME,
                   "type=double",
                   BOOLEAN_FIELD_NAME,
                   "type=boolean",
                   DATE_FIELD_NAME,
                   "type=date",
                   OBJECT_FIELD_NAME,
                   "type=object")
               .string()),
       MapperService.MergeReason.MAPPING_UPDATE,
       false);
   mapperService.merge(
       CHILD_TYPE,
       new CompressedXContent(
           PutMappingRequest.buildFromSimplifiedDef(
                   CHILD_TYPE,
                   "_parent",
                   "type=" + PARENT_TYPE,
                   STRING_FIELD_NAME,
                   "type=text",
                   STRING_FIELD_NAME_2,
                   "type=keyword",
                   INT_FIELD_NAME,
                   "type=integer",
                   DOUBLE_FIELD_NAME,
                   "type=double",
                   BOOLEAN_FIELD_NAME,
                   "type=boolean",
                   DATE_FIELD_NAME,
                   "type=date",
                   OBJECT_FIELD_NAME,
                   "type=object")
               .string()),
       MapperService.MergeReason.MAPPING_UPDATE,
       false);
   mapperService.merge(
       "just_a_type",
       new CompressedXContent(PutMappingRequest.buildFromSimplifiedDef("just_a_type").string()),
       MapperService.MergeReason.MAPPING_UPDATE,
       false);
 }
 @Override
 protected void initializeAdditionalMappings(MapperService mapperService) throws IOException {
   mapperService.merge(
       "nested_doc",
       new CompressedXContent(
           PutMappingRequest.buildFromSimplifiedDef(
                   "nested_doc",
                   STRING_FIELD_NAME,
                   "type=text",
                   INT_FIELD_NAME,
                   "type=integer",
                   DOUBLE_FIELD_NAME,
                   "type=double",
                   BOOLEAN_FIELD_NAME,
                   "type=boolean",
                   DATE_FIELD_NAME,
                   "type=date",
                   OBJECT_FIELD_NAME,
                   "type=object",
                   GEO_POINT_FIELD_NAME,
                   "type=geo_point",
                   "nested1",
                   "type=nested")
               .string()),
       MapperService.MergeReason.MAPPING_UPDATE,
       false);
 }
 public void setUp() throws Exception {
   super.setUp();
   MapperService mapperService = queryParserService().mapperService;
   mapperService.merge(
       PARENT_TYPE,
       new CompressedXContent(
           PutMappingRequest.buildFromSimplifiedDef(
                   PARENT_TYPE,
                   STRING_FIELD_NAME,
                   "type=string",
                   INT_FIELD_NAME,
                   "type=integer",
                   DOUBLE_FIELD_NAME,
                   "type=double",
                   BOOLEAN_FIELD_NAME,
                   "type=boolean",
                   DATE_FIELD_NAME,
                   "type=date",
                   OBJECT_FIELD_NAME,
                   "type=object")
               .string()),
       false,
       false);
   mapperService.merge(
       CHILD_TYPE,
       new CompressedXContent(
           PutMappingRequest.buildFromSimplifiedDef(
                   CHILD_TYPE,
                   "_parent",
                   "type=" + PARENT_TYPE,
                   STRING_FIELD_NAME,
                   "type=string",
                   INT_FIELD_NAME,
                   "type=integer",
                   DOUBLE_FIELD_NAME,
                   "type=double",
                   BOOLEAN_FIELD_NAME,
                   "type=boolean",
                   DATE_FIELD_NAME,
                   "type=date",
                   OBJECT_FIELD_NAME,
                   "type=object")
               .string()),
       false,
       false);
 }
  static SearchContext createSearchContext(String indexName, String parentType, String childType)
      throws IOException {
    final Index index = new Index(indexName);
    final IdCache idCache = new SimpleIdCache(index, ImmutableSettings.EMPTY);
    final CacheRecycler cacheRecycler = new CacheRecycler(ImmutableSettings.EMPTY);
    Settings settings = ImmutableSettings.EMPTY;
    MapperService mapperService = MapperTestUtils.newMapperService(index, settings);
    final IndexService indexService = new SimpleIdCacheTests.StubIndexService(mapperService);
    idCache.setIndexService(indexService);
    // Id_cache is now registered as document type listener, so we can add mappings.
    mapperService.merge(
        childType,
        new CompressedString(
            PutMappingRequest.buildFromSimplifiedDef(childType, "_parent", "type=" + parentType)
                .string()),
        true);

    ThreadPool threadPool = new ThreadPool();
    NodeSettingsService nodeSettingsService = new NodeSettingsService(settings);
    IndicesFilterCache indicesFilterCache =
        new IndicesFilterCache(settings, threadPool, cacheRecycler, nodeSettingsService);
    WeightedFilterCache filterCache = new WeightedFilterCache(index, settings, indicesFilterCache);
    return new TestSearchContext(cacheRecycler, idCache, indexService, filterCache);
  }
  public void testResetRootDocId() throws Exception {
    Directory directory = newDirectory();
    IndexWriterConfig iwc = new IndexWriterConfig(null);
    iwc.setMergePolicy(NoMergePolicy.INSTANCE);
    RandomIndexWriter indexWriter = new RandomIndexWriter(random(), directory, iwc);

    List<Document> documents = new ArrayList<>();

    // 1 segment with, 1 root document, with 3 nested sub docs
    Document document = new Document();
    document.add(
        new Field(UidFieldMapper.NAME, "type#1", UidFieldMapper.Defaults.NESTED_FIELD_TYPE));
    document.add(
        new Field(TypeFieldMapper.NAME, "__nested_field", TypeFieldMapper.Defaults.FIELD_TYPE));
    documents.add(document);
    document = new Document();
    document.add(
        new Field(UidFieldMapper.NAME, "type#1", UidFieldMapper.Defaults.NESTED_FIELD_TYPE));
    document.add(
        new Field(TypeFieldMapper.NAME, "__nested_field", TypeFieldMapper.Defaults.FIELD_TYPE));
    documents.add(document);
    document = new Document();
    document.add(
        new Field(UidFieldMapper.NAME, "type#1", UidFieldMapper.Defaults.NESTED_FIELD_TYPE));
    document.add(
        new Field(TypeFieldMapper.NAME, "__nested_field", TypeFieldMapper.Defaults.FIELD_TYPE));
    documents.add(document);
    document = new Document();
    document.add(new Field(UidFieldMapper.NAME, "type#1", UidFieldMapper.Defaults.FIELD_TYPE));
    document.add(new Field(TypeFieldMapper.NAME, "test", TypeFieldMapper.Defaults.FIELD_TYPE));
    documents.add(document);
    indexWriter.addDocuments(documents);
    indexWriter.commit();

    documents.clear();
    // 1 segment with:
    // 1 document, with 1 nested subdoc
    document = new Document();
    document.add(
        new Field(UidFieldMapper.NAME, "type#2", UidFieldMapper.Defaults.NESTED_FIELD_TYPE));
    document.add(
        new Field(TypeFieldMapper.NAME, "__nested_field", TypeFieldMapper.Defaults.FIELD_TYPE));
    documents.add(document);
    document = new Document();
    document.add(new Field(UidFieldMapper.NAME, "type#2", UidFieldMapper.Defaults.FIELD_TYPE));
    document.add(new Field(TypeFieldMapper.NAME, "test", TypeFieldMapper.Defaults.FIELD_TYPE));
    documents.add(document);
    indexWriter.addDocuments(documents);
    documents.clear();
    // and 1 document, with 1 nested subdoc
    document = new Document();
    document.add(
        new Field(UidFieldMapper.NAME, "type#3", UidFieldMapper.Defaults.NESTED_FIELD_TYPE));
    document.add(
        new Field(TypeFieldMapper.NAME, "__nested_field", TypeFieldMapper.Defaults.FIELD_TYPE));
    documents.add(document);
    document = new Document();
    document.add(new Field(UidFieldMapper.NAME, "type#3", UidFieldMapper.Defaults.FIELD_TYPE));
    document.add(new Field(TypeFieldMapper.NAME, "test", TypeFieldMapper.Defaults.FIELD_TYPE));
    documents.add(document);
    indexWriter.addDocuments(documents);

    indexWriter.commit();
    indexWriter.close();

    IndexService indexService = createIndex("test");
    DirectoryReader directoryReader = DirectoryReader.open(directory);
    directoryReader =
        ElasticsearchDirectoryReader.wrap(directoryReader, new ShardId(indexService.index(), 0));
    IndexSearcher searcher = new IndexSearcher(directoryReader);

    indexService
        .mapperService()
        .merge(
            "test",
            new CompressedXContent(
                PutMappingRequest.buildFromSimplifiedDef("test", "nested_field", "type=nested")
                    .string()),
            MapperService.MergeReason.MAPPING_UPDATE,
            false);
    SearchContext searchContext = createSearchContext(indexService);
    AggregationContext context = new AggregationContext(searchContext);

    AggregatorFactories.Builder builder = AggregatorFactories.builder();
    NestedAggregatorBuilder factory = new NestedAggregatorBuilder("test", "nested_field");
    builder.addAggregator(factory);
    AggregatorFactories factories = builder.build(context, null);
    searchContext.aggregations(new SearchContextAggregations(factories));
    Aggregator[] aggs = factories.createTopLevelAggregators();
    BucketCollector collector = BucketCollector.wrap(Arrays.asList(aggs));
    collector.preCollection();
    // A regular search always exclude nested docs, so we use NonNestedDocsFilter.INSTANCE here
    // (otherwise MatchAllDocsQuery would be sufficient)
    // We exclude root doc with uid type#2, this will trigger the bug if we don't reset the root doc
    // when we process a new segment, because
    // root doc type#3 and root doc type#1 have the same segment docid
    BooleanQuery.Builder bq = new BooleanQuery.Builder();
    bq.add(Queries.newNonNestedFilter(), Occur.MUST);
    bq.add(new TermQuery(new Term(UidFieldMapper.NAME, "type#2")), Occur.MUST_NOT);
    searcher.search(new ConstantScoreQuery(bq.build()), collector);
    collector.postCollection();

    Nested nested = (Nested) aggs[0].buildAggregation(0);
    // The bug manifests if 6 docs are returned, because currentRootDoc isn't reset the previous
    // child docs from the first segment are emitted as hits.
    assertThat(nested.getDocCount(), equalTo(4L));

    directoryReader.close();
    directory.close();
  }
Exemple #7
0
 /**
  * A specialized simplified mapping source method, takes the form of simple properties definition:
  * ("field1", "type=string,store=true").
  */
 public CreateIndexRequest mapping(String type, Object... source) {
   mapping(type, PutMappingRequest.buildFromSimplifiedDef(type, source));
   return this;
 }