Search in sources :

Example 46 with StoredField

use of org.apache.lucene.document.StoredField in project lucene-solr by apache.

the class BaseStoredFieldsFormatTestCase method testNumericField.

public void testNumericField() throws Exception {
    Directory dir = newDirectory();
    RandomIndexWriter w = new RandomIndexWriter(random(), dir);
    final int numDocs = atLeast(500);
    final Number[] answers = new Number[numDocs];
    final Class<?>[] typeAnswers = new Class<?>[numDocs];
    for (int id = 0; id < numDocs; id++) {
        Document doc = new Document();
        final Field nf;
        final Number answer;
        final Class<?> typeAnswer;
        if (random().nextBoolean()) {
            // float/double
            if (random().nextBoolean()) {
                final float f = random().nextFloat();
                answer = Float.valueOf(f);
                nf = new StoredField("nf", f);
                typeAnswer = Float.class;
            } else {
                final double d = random().nextDouble();
                answer = Double.valueOf(d);
                nf = new StoredField("nf", d);
                typeAnswer = Double.class;
            }
        } else {
            // int/long
            if (random().nextBoolean()) {
                final int i = random().nextInt();
                answer = Integer.valueOf(i);
                nf = new StoredField("nf", i);
                typeAnswer = Integer.class;
            } else {
                final long l = random().nextLong();
                answer = Long.valueOf(l);
                nf = new StoredField("nf", l);
                typeAnswer = Long.class;
            }
        }
        doc.add(nf);
        answers[id] = answer;
        typeAnswers[id] = typeAnswer;
        doc.add(new StoredField("id", id));
        doc.add(new IntPoint("id", id));
        doc.add(new NumericDocValuesField("id", id));
        w.addDocument(doc);
    }
    final DirectoryReader r = w.getReader();
    w.close();
    assertEquals(numDocs, r.numDocs());
    for (LeafReaderContext ctx : r.leaves()) {
        final LeafReader sub = ctx.reader();
        final NumericDocValues ids = DocValues.getNumeric(sub, "id");
        for (int docID = 0; docID < sub.numDocs(); docID++) {
            final Document doc = sub.document(docID);
            final Field f = (Field) doc.getField("nf");
            assertTrue("got f=" + f, f instanceof StoredField);
            assertEquals(docID, ids.nextDoc());
            assertEquals(answers[(int) ids.longValue()], f.numericValue());
        }
    }
    r.close();
    dir.close();
}
Also used : Document(org.apache.lucene.document.Document) IntPoint(org.apache.lucene.document.IntPoint) StringField(org.apache.lucene.document.StringField) StoredField(org.apache.lucene.document.StoredField) NumericDocValuesField(org.apache.lucene.document.NumericDocValuesField) Field(org.apache.lucene.document.Field) TextField(org.apache.lucene.document.TextField) IntPoint(org.apache.lucene.document.IntPoint) StoredField(org.apache.lucene.document.StoredField) NumericDocValuesField(org.apache.lucene.document.NumericDocValuesField) MMapDirectory(org.apache.lucene.store.MMapDirectory) Directory(org.apache.lucene.store.Directory)

Example 47 with StoredField

use of org.apache.lucene.document.StoredField in project lucene-solr by apache.

the class BaseStoredFieldsFormatTestCase method testWriteReadMerge.

public void testWriteReadMerge() throws IOException {
    // get another codec, other than the default: so we are merging segments across different codecs
    final Codec otherCodec;
    if ("SimpleText".equals(Codec.getDefault().getName())) {
        otherCodec = TestUtil.getDefaultCodec();
    } else {
        otherCodec = new SimpleTextCodec();
    }
    Directory dir = newDirectory();
    IndexWriterConfig iwConf = newIndexWriterConfig(new MockAnalyzer(random()));
    iwConf.setMaxBufferedDocs(RandomNumbers.randomIntBetween(random(), 2, 30));
    RandomIndexWriter iw = new RandomIndexWriter(random(), dir, iwConf);
    final int docCount = atLeast(200);
    final byte[][][] data = new byte[docCount][][];
    for (int i = 0; i < docCount; ++i) {
        final int fieldCount = rarely() ? RandomNumbers.randomIntBetween(random(), 1, 500) : RandomNumbers.randomIntBetween(random(), 1, 5);
        data[i] = new byte[fieldCount][];
        for (int j = 0; j < fieldCount; ++j) {
            final int length = rarely() ? random().nextInt(1000) : random().nextInt(10);
            final int max = rarely() ? 256 : 2;
            data[i][j] = randomByteArray(length, max);
        }
    }
    final FieldType type = new FieldType(StringField.TYPE_STORED);
    type.setIndexOptions(IndexOptions.NONE);
    type.freeze();
    IntPoint id = new IntPoint("id", 0);
    StoredField idStored = new StoredField("id", 0);
    for (int i = 0; i < data.length; ++i) {
        Document doc = new Document();
        doc.add(id);
        doc.add(idStored);
        id.setIntValue(i);
        idStored.setIntValue(i);
        for (int j = 0; j < data[i].length; ++j) {
            Field f = new Field("bytes" + j, data[i][j], type);
            doc.add(f);
        }
        iw.w.addDocument(doc);
        if (random().nextBoolean() && (i % (data.length / 10) == 0)) {
            iw.w.close();
            IndexWriterConfig iwConfNew = newIndexWriterConfig(new MockAnalyzer(random()));
            // test merging against a non-compressing codec
            if (iwConf.getCodec() == otherCodec) {
                iwConfNew.setCodec(Codec.getDefault());
            } else {
                iwConfNew.setCodec(otherCodec);
            }
            iwConf = iwConfNew;
            iw = new RandomIndexWriter(random(), dir, iwConf);
        }
    }
    for (int i = 0; i < 10; ++i) {
        final int min = random().nextInt(data.length);
        final int max = min + random().nextInt(20);
        iw.deleteDocuments(IntPoint.newRangeQuery("id", min, max - 1));
    }
    // force merges with deletions
    iw.forceMerge(2);
    iw.commit();
    final DirectoryReader ir = DirectoryReader.open(dir);
    assertTrue(ir.numDocs() > 0);
    int numDocs = 0;
    for (int i = 0; i < ir.maxDoc(); ++i) {
        final Document doc = ir.document(i);
        if (doc == null) {
            continue;
        }
        ++numDocs;
        final int docId = doc.getField("id").numericValue().intValue();
        assertEquals(data[docId].length + 1, doc.getFields().size());
        for (int j = 0; j < data[docId].length; ++j) {
            final byte[] arr = data[docId][j];
            final BytesRef arr2Ref = doc.getBinaryValue("bytes" + j);
            final byte[] arr2 = Arrays.copyOfRange(arr2Ref.bytes, arr2Ref.offset, arr2Ref.offset + arr2Ref.length);
            assertArrayEquals(arr, arr2);
        }
    }
    assertTrue(ir.numDocs() <= numDocs);
    ir.close();
    iw.deleteAll();
    iw.commit();
    iw.forceMerge(1);
    iw.close();
    dir.close();
}
Also used : SimpleTextCodec(org.apache.lucene.codecs.simpletext.SimpleTextCodec) Document(org.apache.lucene.document.Document) IntPoint(org.apache.lucene.document.IntPoint) FieldType(org.apache.lucene.document.FieldType) IntPoint(org.apache.lucene.document.IntPoint) StringField(org.apache.lucene.document.StringField) StoredField(org.apache.lucene.document.StoredField) NumericDocValuesField(org.apache.lucene.document.NumericDocValuesField) Field(org.apache.lucene.document.Field) TextField(org.apache.lucene.document.TextField) SimpleTextCodec(org.apache.lucene.codecs.simpletext.SimpleTextCodec) Codec(org.apache.lucene.codecs.Codec) StoredField(org.apache.lucene.document.StoredField) MockAnalyzer(org.apache.lucene.analysis.MockAnalyzer) BytesRef(org.apache.lucene.util.BytesRef) MMapDirectory(org.apache.lucene.store.MMapDirectory) Directory(org.apache.lucene.store.Directory)

Example 48 with StoredField

use of org.apache.lucene.document.StoredField in project lucene-solr by apache.

the class TestBlockJoin method testRandom.

public void testRandom() throws Exception {
    // We build two indices at once: one normalized (which
    // ToParentBlockJoinQuery/Collector,
    // ToChildBlockJoinQuery can query) and the other w/
    // the same docs, just fully denormalized:
    final Directory dir = newDirectory();
    final Directory joinDir = newDirectory();
    final int maxNumChildrenPerParent = 20;
    final int numParentDocs = TestUtil.nextInt(random(), 100 * RANDOM_MULTIPLIER, 300 * RANDOM_MULTIPLIER);
    //final int numParentDocs = 30;
    // Values for parent fields:
    final String[][] parentFields = getRandomFields(numParentDocs / 2);
    // Values for child fields:
    final String[][] childFields = getRandomFields(numParentDocs);
    final boolean doDeletes = random().nextBoolean();
    final List<Integer> toDelete = new ArrayList<>();
    // TODO: parallel star join, nested join cases too!
    final RandomIndexWriter w = new RandomIndexWriter(random(), dir);
    final RandomIndexWriter joinW = new RandomIndexWriter(random(), joinDir);
    for (int parentDocID = 0; parentDocID < numParentDocs; parentDocID++) {
        Document parentDoc = new Document();
        Document parentJoinDoc = new Document();
        Field id = new StoredField("parentID", parentDocID);
        parentDoc.add(id);
        parentJoinDoc.add(id);
        parentJoinDoc.add(newStringField("isParent", "x", Field.Store.NO));
        id = new NumericDocValuesField("parentID", parentDocID);
        parentDoc.add(id);
        parentJoinDoc.add(id);
        parentJoinDoc.add(newStringField("isParent", "x", Field.Store.NO));
        for (int field = 0; field < parentFields.length; field++) {
            if (random().nextDouble() < 0.9) {
                String s = parentFields[field][random().nextInt(parentFields[field].length)];
                Field f = newStringField("parent" + field, s, Field.Store.NO);
                parentDoc.add(f);
                parentJoinDoc.add(f);
                f = new SortedDocValuesField("parent" + field, new BytesRef(s));
                parentDoc.add(f);
                parentJoinDoc.add(f);
            }
        }
        if (doDeletes) {
            parentDoc.add(new IntPoint("blockID", parentDocID));
            parentJoinDoc.add(new IntPoint("blockID", parentDocID));
        }
        final List<Document> joinDocs = new ArrayList<>();
        if (VERBOSE) {
            StringBuilder sb = new StringBuilder();
            sb.append("parentID=").append(parentDoc.get("parentID"));
            for (int fieldID = 0; fieldID < parentFields.length; fieldID++) {
                String s = parentDoc.get("parent" + fieldID);
                if (s != null) {
                    sb.append(" parent" + fieldID + "=" + s);
                }
            }
            System.out.println("  " + sb.toString());
        }
        final int numChildDocs = TestUtil.nextInt(random(), 1, maxNumChildrenPerParent);
        for (int childDocID = 0; childDocID < numChildDocs; childDocID++) {
            // Denormalize: copy all parent fields into child doc:
            Document childDoc = TestUtil.cloneDocument(parentDoc);
            Document joinChildDoc = new Document();
            joinDocs.add(joinChildDoc);
            Field childID = new StoredField("childID", childDocID);
            childDoc.add(childID);
            joinChildDoc.add(childID);
            childID = new NumericDocValuesField("childID", childDocID);
            childDoc.add(childID);
            joinChildDoc.add(childID);
            for (int childFieldID = 0; childFieldID < childFields.length; childFieldID++) {
                if (random().nextDouble() < 0.9) {
                    String s = childFields[childFieldID][random().nextInt(childFields[childFieldID].length)];
                    Field f = newStringField("child" + childFieldID, s, Field.Store.NO);
                    childDoc.add(f);
                    joinChildDoc.add(f);
                    f = new SortedDocValuesField("child" + childFieldID, new BytesRef(s));
                    childDoc.add(f);
                    joinChildDoc.add(f);
                }
            }
            if (VERBOSE) {
                StringBuilder sb = new StringBuilder();
                sb.append("childID=").append(joinChildDoc.get("childID"));
                for (int fieldID = 0; fieldID < childFields.length; fieldID++) {
                    String s = joinChildDoc.get("child" + fieldID);
                    if (s != null) {
                        sb.append(" child" + fieldID + "=" + s);
                    }
                }
                System.out.println("    " + sb.toString());
            }
            if (doDeletes) {
                joinChildDoc.add(new IntPoint("blockID", parentDocID));
            }
            w.addDocument(childDoc);
        }
        // Parent last:
        joinDocs.add(parentJoinDoc);
        joinW.addDocuments(joinDocs);
        if (doDeletes && random().nextInt(30) == 7) {
            toDelete.add(parentDocID);
        }
    }
    if (!toDelete.isEmpty()) {
        Query query = IntPoint.newSetQuery("blockID", toDelete);
        w.deleteDocuments(query);
        joinW.deleteDocuments(query);
    }
    final IndexReader r = w.getReader();
    w.close();
    final IndexReader joinR = joinW.getReader();
    joinW.close();
    if (VERBOSE) {
        System.out.println("TEST: reader=" + r);
        System.out.println("TEST: joinReader=" + joinR);
        Bits liveDocs = MultiFields.getLiveDocs(joinR);
        for (int docIDX = 0; docIDX < joinR.maxDoc(); docIDX++) {
            System.out.println("  docID=" + docIDX + " doc=" + joinR.document(docIDX) + " deleted?=" + (liveDocs != null && liveDocs.get(docIDX) == false));
        }
        PostingsEnum parents = MultiFields.getTermDocsEnum(joinR, "isParent", new BytesRef("x"));
        System.out.println("parent docIDs:");
        while (parents.nextDoc() != PostingsEnum.NO_MORE_DOCS) {
            System.out.println("  " + parents.docID());
        }
    }
    final IndexSearcher s = newSearcher(r, false);
    final IndexSearcher joinS = newSearcher(joinR);
    final BitSetProducer parentsFilter = new QueryBitSetProducer(new TermQuery(new Term("isParent", "x")));
    CheckJoinIndex.check(joinS.getIndexReader(), parentsFilter);
    final int iters = 200 * RANDOM_MULTIPLIER;
    for (int iter = 0; iter < iters; iter++) {
        if (VERBOSE) {
            System.out.println("TEST: iter=" + (1 + iter) + " of " + iters);
        }
        Query childQuery;
        if (random().nextInt(3) == 2) {
            final int childFieldID = random().nextInt(childFields.length);
            childQuery = new TermQuery(new Term("child" + childFieldID, childFields[childFieldID][random().nextInt(childFields[childFieldID].length)]));
        } else if (random().nextInt(3) == 2) {
            BooleanQuery.Builder bq = new BooleanQuery.Builder();
            final int numClauses = TestUtil.nextInt(random(), 2, 4);
            boolean didMust = false;
            for (int clauseIDX = 0; clauseIDX < numClauses; clauseIDX++) {
                Query clause;
                BooleanClause.Occur occur;
                if (!didMust && random().nextBoolean()) {
                    occur = random().nextBoolean() ? BooleanClause.Occur.MUST : BooleanClause.Occur.MUST_NOT;
                    clause = new TermQuery(randomChildTerm(childFields[0]));
                    didMust = true;
                } else {
                    occur = BooleanClause.Occur.SHOULD;
                    final int childFieldID = TestUtil.nextInt(random(), 1, childFields.length - 1);
                    clause = new TermQuery(new Term("child" + childFieldID, childFields[childFieldID][random().nextInt(childFields[childFieldID].length)]));
                }
                bq.add(clause, occur);
            }
            childQuery = bq.build();
        } else {
            BooleanQuery.Builder bq = new BooleanQuery.Builder();
            bq.add(new TermQuery(randomChildTerm(childFields[0])), BooleanClause.Occur.MUST);
            final int childFieldID = TestUtil.nextInt(random(), 1, childFields.length - 1);
            bq.add(new TermQuery(new Term("child" + childFieldID, childFields[childFieldID][random().nextInt(childFields[childFieldID].length)])), random().nextBoolean() ? BooleanClause.Occur.MUST : BooleanClause.Occur.MUST_NOT);
            childQuery = bq.build();
        }
        if (random().nextBoolean()) {
            childQuery = new RandomApproximationQuery(childQuery, random());
        }
        final ScoreMode agg = ScoreMode.values()[random().nextInt(ScoreMode.values().length)];
        final ToParentBlockJoinQuery childJoinQuery = new ToParentBlockJoinQuery(childQuery, parentsFilter, agg);
        // To run against the block-join index:
        final Query parentJoinQuery;
        // Same query as parentJoinQuery, but to run against
        // the fully denormalized index (so we can compare
        // results):
        final Query parentQuery;
        if (random().nextBoolean()) {
            parentQuery = childQuery;
            parentJoinQuery = childJoinQuery;
        } else {
            // AND parent field w/ child field
            final BooleanQuery.Builder bq = new BooleanQuery.Builder();
            final Term parentTerm = randomParentTerm(parentFields[0]);
            if (random().nextBoolean()) {
                bq.add(childJoinQuery, BooleanClause.Occur.MUST);
                bq.add(new TermQuery(parentTerm), BooleanClause.Occur.MUST);
            } else {
                bq.add(new TermQuery(parentTerm), BooleanClause.Occur.MUST);
                bq.add(childJoinQuery, BooleanClause.Occur.MUST);
            }
            final BooleanQuery.Builder bq2 = new BooleanQuery.Builder();
            if (random().nextBoolean()) {
                bq2.add(childQuery, BooleanClause.Occur.MUST);
                bq2.add(new TermQuery(parentTerm), BooleanClause.Occur.MUST);
            } else {
                bq2.add(new TermQuery(parentTerm), BooleanClause.Occur.MUST);
                bq2.add(childQuery, BooleanClause.Occur.MUST);
            }
            parentJoinQuery = bq.build();
            parentQuery = bq2.build();
        }
        final Sort parentSort = getRandomSort("parent", parentFields.length);
        final Sort childSort = getRandomSort("child", childFields.length);
        if (VERBOSE) {
            System.out.println("\nTEST: query=" + parentQuery + " joinQuery=" + parentJoinQuery + " parentSort=" + parentSort + " childSort=" + childSort);
        }
        // Merge both sorts:
        final List<SortField> sortFields = new ArrayList<>(Arrays.asList(parentSort.getSort()));
        sortFields.addAll(Arrays.asList(childSort.getSort()));
        final Sort parentAndChildSort = new Sort(sortFields.toArray(new SortField[sortFields.size()]));
        final TopDocs results = s.search(parentQuery, r.numDocs(), parentAndChildSort);
        if (VERBOSE) {
            System.out.println("\nTEST: normal index gets " + results.totalHits + " hits; sort=" + parentAndChildSort);
            final ScoreDoc[] hits = results.scoreDocs;
            for (int hitIDX = 0; hitIDX < hits.length; hitIDX++) {
                final Document doc = s.doc(hits[hitIDX].doc);
                //System.out.println("  score=" + hits[hitIDX].score + " parentID=" + doc.get("parentID") + " childID=" + doc.get("childID") + " (docID=" + hits[hitIDX].doc + ")");
                System.out.println("  parentID=" + doc.get("parentID") + " childID=" + doc.get("childID") + " (docID=" + hits[hitIDX].doc + ")");
                FieldDoc fd = (FieldDoc) hits[hitIDX];
                if (fd.fields != null) {
                    System.out.print("    " + fd.fields.length + " sort values: ");
                    for (Object o : fd.fields) {
                        if (o instanceof BytesRef) {
                            System.out.print(((BytesRef) o).utf8ToString() + " ");
                        } else {
                            System.out.print(o + " ");
                        }
                    }
                    System.out.println();
                }
            }
        }
        TopDocs joinedResults = joinS.search(parentJoinQuery, numParentDocs);
        SortedMap<Integer, TopDocs> joinResults = new TreeMap<>();
        for (ScoreDoc parentHit : joinedResults.scoreDocs) {
            ParentChildrenBlockJoinQuery childrenQuery = new ParentChildrenBlockJoinQuery(parentsFilter, childQuery, parentHit.doc);
            TopDocs childTopDocs = joinS.search(childrenQuery, maxNumChildrenPerParent, childSort);
            final Document parentDoc = joinS.doc(parentHit.doc);
            joinResults.put(Integer.valueOf(parentDoc.get("parentID")), childTopDocs);
        }
        final int hitsPerGroup = TestUtil.nextInt(random(), 1, 20);
        if (VERBOSE) {
            System.out.println("\nTEST: block join index gets " + (joinResults == null ? 0 : joinResults.size()) + " groups; hitsPerGroup=" + hitsPerGroup);
            if (joinResults != null) {
                for (Map.Entry<Integer, TopDocs> entry : joinResults.entrySet()) {
                    System.out.println("  group parentID=" + entry.getKey() + " (docID=" + entry.getKey() + ")");
                    for (ScoreDoc childHit : entry.getValue().scoreDocs) {
                        final Document doc = joinS.doc(childHit.doc);
                        //              System.out.println("    score=" + childHit.score + " childID=" + doc.get("childID") + " (docID=" + childHit.doc + ")");
                        System.out.println("    childID=" + doc.get("childID") + " child0=" + doc.get("child0") + " (docID=" + childHit.doc + ")");
                    }
                }
            }
        }
        if (results.totalHits == 0) {
            assertEquals(0, joinResults.size());
        } else {
            compareHits(r, joinR, results, joinResults);
            TopDocs b = joinS.search(childJoinQuery, 10);
            for (ScoreDoc hit : b.scoreDocs) {
                Explanation explanation = joinS.explain(childJoinQuery, hit.doc);
                Document document = joinS.doc(hit.doc - 1);
                int childId = Integer.parseInt(document.get("childID"));
                //System.out.println("  hit docID=" + hit.doc + " childId=" + childId + " parentId=" + document.get("parentID"));
                assertTrue(explanation.isMatch());
                assertEquals(hit.score, explanation.getValue(), 0.0f);
                Matcher m = Pattern.compile("Score based on ([0-9]+) child docs in range from ([0-9]+) to ([0-9]+), best match:").matcher(explanation.getDescription());
                assertTrue("Block Join description not matches", m.matches());
                assertTrue("Matched children not positive", Integer.parseInt(m.group(1)) > 0);
                assertEquals("Wrong child range start", hit.doc - 1 - childId, Integer.parseInt(m.group(2)));
                assertEquals("Wrong child range end", hit.doc - 1, Integer.parseInt(m.group(3)));
                Explanation childWeightExplanation = explanation.getDetails()[0];
                if ("sum of:".equals(childWeightExplanation.getDescription())) {
                    childWeightExplanation = childWeightExplanation.getDetails()[0];
                }
                assertTrue("Wrong child weight description", childWeightExplanation.getDescription().startsWith("weight(child"));
            }
        }
        // Test joining in the opposite direction (parent to
        // child):
        // Get random query against parent documents:
        final Query parentQuery2;
        if (random().nextInt(3) == 2) {
            final int fieldID = random().nextInt(parentFields.length);
            parentQuery2 = new TermQuery(new Term("parent" + fieldID, parentFields[fieldID][random().nextInt(parentFields[fieldID].length)]));
        } else if (random().nextInt(3) == 2) {
            BooleanQuery.Builder bq = new BooleanQuery.Builder();
            final int numClauses = TestUtil.nextInt(random(), 2, 4);
            boolean didMust = false;
            for (int clauseIDX = 0; clauseIDX < numClauses; clauseIDX++) {
                Query clause;
                BooleanClause.Occur occur;
                if (!didMust && random().nextBoolean()) {
                    occur = random().nextBoolean() ? BooleanClause.Occur.MUST : BooleanClause.Occur.MUST_NOT;
                    clause = new TermQuery(randomParentTerm(parentFields[0]));
                    didMust = true;
                } else {
                    occur = BooleanClause.Occur.SHOULD;
                    final int fieldID = TestUtil.nextInt(random(), 1, parentFields.length - 1);
                    clause = new TermQuery(new Term("parent" + fieldID, parentFields[fieldID][random().nextInt(parentFields[fieldID].length)]));
                }
                bq.add(clause, occur);
            }
            parentQuery2 = bq.build();
        } else {
            BooleanQuery.Builder bq = new BooleanQuery.Builder();
            bq.add(new TermQuery(randomParentTerm(parentFields[0])), BooleanClause.Occur.MUST);
            final int fieldID = TestUtil.nextInt(random(), 1, parentFields.length - 1);
            bq.add(new TermQuery(new Term("parent" + fieldID, parentFields[fieldID][random().nextInt(parentFields[fieldID].length)])), random().nextBoolean() ? BooleanClause.Occur.MUST : BooleanClause.Occur.MUST_NOT);
            parentQuery2 = bq.build();
        }
        if (VERBOSE) {
            System.out.println("\nTEST: top down: parentQuery2=" + parentQuery2);
        }
        // Maps parent query to child docs:
        final ToChildBlockJoinQuery parentJoinQuery2 = new ToChildBlockJoinQuery(parentQuery2, parentsFilter);
        // To run against the block-join index:
        Query childJoinQuery2;
        // Same query as parentJoinQuery, but to run against
        // the fully denormalized index (so we can compare
        // results):
        Query childQuery2;
        if (random().nextBoolean()) {
            childQuery2 = parentQuery2;
            childJoinQuery2 = parentJoinQuery2;
        } else {
            final Term childTerm = randomChildTerm(childFields[0]);
            if (random().nextBoolean()) {
                // filtered case
                childJoinQuery2 = parentJoinQuery2;
                childJoinQuery2 = new BooleanQuery.Builder().add(childJoinQuery2, Occur.MUST).add(new TermQuery(childTerm), Occur.FILTER).build();
            } else {
                // AND child field w/ parent query:
                final BooleanQuery.Builder bq = new BooleanQuery.Builder();
                if (random().nextBoolean()) {
                    bq.add(parentJoinQuery2, BooleanClause.Occur.MUST);
                    bq.add(new TermQuery(childTerm), BooleanClause.Occur.MUST);
                } else {
                    bq.add(new TermQuery(childTerm), BooleanClause.Occur.MUST);
                    bq.add(parentJoinQuery2, BooleanClause.Occur.MUST);
                }
                childJoinQuery2 = bq.build();
            }
            if (random().nextBoolean()) {
                // filtered case
                childQuery2 = parentQuery2;
                childQuery2 = new BooleanQuery.Builder().add(childQuery2, Occur.MUST).add(new TermQuery(childTerm), Occur.FILTER).build();
            } else {
                final BooleanQuery.Builder bq2 = new BooleanQuery.Builder();
                if (random().nextBoolean()) {
                    bq2.add(parentQuery2, BooleanClause.Occur.MUST);
                    bq2.add(new TermQuery(childTerm), BooleanClause.Occur.MUST);
                } else {
                    bq2.add(new TermQuery(childTerm), BooleanClause.Occur.MUST);
                    bq2.add(parentQuery2, BooleanClause.Occur.MUST);
                }
                childQuery2 = bq2.build();
            }
        }
        final Sort childSort2 = getRandomSort("child", childFields.length);
        // Search denormalized index:
        if (VERBOSE) {
            System.out.println("TEST: run top down query=" + childQuery2 + " sort=" + childSort2);
        }
        final TopDocs results2 = s.search(childQuery2, r.numDocs(), childSort2);
        if (VERBOSE) {
            System.out.println("  " + results2.totalHits + " totalHits:");
            for (ScoreDoc sd : results2.scoreDocs) {
                final Document doc = s.doc(sd.doc);
                System.out.println("  childID=" + doc.get("childID") + " parentID=" + doc.get("parentID") + " docID=" + sd.doc);
            }
        }
        // Search join index:
        if (VERBOSE) {
            System.out.println("TEST: run top down join query=" + childJoinQuery2 + " sort=" + childSort2);
        }
        TopDocs joinResults2 = joinS.search(childJoinQuery2, joinR.numDocs(), childSort2);
        if (VERBOSE) {
            System.out.println("  " + joinResults2.totalHits + " totalHits:");
            for (ScoreDoc sd : joinResults2.scoreDocs) {
                final Document doc = joinS.doc(sd.doc);
                final Document parentDoc = getParentDoc(joinR, parentsFilter, sd.doc);
                System.out.println("  childID=" + doc.get("childID") + " parentID=" + parentDoc.get("parentID") + " docID=" + sd.doc);
            }
        }
        compareChildHits(r, joinR, results2, joinResults2);
    }
    r.close();
    joinR.close();
    dir.close();
    joinDir.close();
}
Also used : ArrayList(java.util.ArrayList) Document(org.apache.lucene.document.Document) StoredField(org.apache.lucene.document.StoredField) NumericDocValuesField(org.apache.lucene.document.NumericDocValuesField) PostingsEnum(org.apache.lucene.index.PostingsEnum) Term(org.apache.lucene.index.Term) IntPoint(org.apache.lucene.document.IntPoint) Bits(org.apache.lucene.util.Bits) Map(java.util.Map) TreeMap(java.util.TreeMap) SortedMap(java.util.SortedMap) RandomIndexWriter(org.apache.lucene.index.RandomIndexWriter) Matcher(java.util.regex.Matcher) Occur(org.apache.lucene.search.BooleanClause.Occur) StringField(org.apache.lucene.document.StringField) StoredField(org.apache.lucene.document.StoredField) NumericDocValuesField(org.apache.lucene.document.NumericDocValuesField) SortedDocValuesField(org.apache.lucene.document.SortedDocValuesField) Field(org.apache.lucene.document.Field) SortedDocValuesField(org.apache.lucene.document.SortedDocValuesField) BytesRef(org.apache.lucene.util.BytesRef) Directory(org.apache.lucene.store.Directory) TreeMap(java.util.TreeMap) IntPoint(org.apache.lucene.document.IntPoint) IndexReader(org.apache.lucene.index.IndexReader)

Example 49 with StoredField

use of org.apache.lucene.document.StoredField in project lucene-solr by apache.

the class RandomSpatialOpFuzzyPrefixTreeTest method newDoc.

//Override so we can index parts of a pair separately, resulting in the detailLevel
// being independent for each shape vs the whole thing
@Override
protected Document newDoc(String id, Shape shape) {
    Document doc = new Document();
    doc.add(new StringField("id", id, Field.Store.YES));
    if (shape != null) {
        Collection<Shape> shapes;
        if (shape instanceof ShapePair) {
            shapes = new ArrayList<>(2);
            shapes.add(((ShapePair) shape).shape1);
            shapes.add(((ShapePair) shape).shape2);
        } else {
            shapes = Collections.singleton(shape);
        }
        for (Shape shapei : shapes) {
            for (Field f : strategy.createIndexableFields(shapei)) {
                doc.add(f);
            }
        }
        if (//just for diagnostics
        storeShape)
            doc.add(new StoredField(strategy.getFieldName(), shape.toString()));
    }
    return doc;
}
Also used : StringField(org.apache.lucene.document.StringField) StoredField(org.apache.lucene.document.StoredField) Field(org.apache.lucene.document.Field) StoredField(org.apache.lucene.document.StoredField) Shape(org.locationtech.spatial4j.shape.Shape) StringField(org.apache.lucene.document.StringField) Document(org.apache.lucene.document.Document)

Example 50 with StoredField

use of org.apache.lucene.document.StoredField in project lucene-solr by apache.

the class TestTermQueryPrefixGridStrategy method testNGramPrefixGridLosAngeles.

@Test
public void testNGramPrefixGridLosAngeles() throws IOException {
    SpatialContext ctx = SpatialContext.GEO;
    TermQueryPrefixTreeStrategy prefixGridStrategy = new TermQueryPrefixTreeStrategy(new QuadPrefixTree(ctx), "geo");
    Shape point = ctx.makePoint(-118.243680, 34.052230);
    Document losAngeles = new Document();
    losAngeles.add(new StringField("name", "Los Angeles", Field.Store.YES));
    for (Field field : prefixGridStrategy.createIndexableFields(point)) {
        losAngeles.add(field);
    }
    //just for diagnostics
    losAngeles.add(new StoredField(prefixGridStrategy.getFieldName(), point.toString()));
    addDocumentsAndCommit(Arrays.asList(losAngeles));
    // This won't work with simple spatial context...
    SpatialArgsParser spatialArgsParser = new SpatialArgsParser();
// TODO... use a non polygon query
//    SpatialArgs spatialArgs = spatialArgsParser.parse(
//        "Intersects(POLYGON((-127.00390625 39.8125,-112.765625 39.98828125,-111.53515625 31.375,-125.94921875 30.14453125,-127.00390625 39.8125)))",
//        new SimpleSpatialContext());
//    Query query = prefixGridStrategy.makeQuery(spatialArgs, fieldInfo);
//    SearchResults searchResults = executeQuery(query, 1);
//    assertEquals(1, searchResults.numFound);
}
Also used : Field(org.apache.lucene.document.Field) StringField(org.apache.lucene.document.StringField) StoredField(org.apache.lucene.document.StoredField) StoredField(org.apache.lucene.document.StoredField) SpatialContext(org.locationtech.spatial4j.context.SpatialContext) Shape(org.locationtech.spatial4j.shape.Shape) QuadPrefixTree(org.apache.lucene.spatial.prefix.tree.QuadPrefixTree) SpatialArgsParser(org.apache.lucene.spatial.query.SpatialArgsParser) StringField(org.apache.lucene.document.StringField) Document(org.apache.lucene.document.Document) Test(org.junit.Test)

Aggregations

StoredField (org.apache.lucene.document.StoredField)109 Document (org.apache.lucene.document.Document)97 Directory (org.apache.lucene.store.Directory)72 StringField (org.apache.lucene.document.StringField)43 Field (org.apache.lucene.document.Field)40 MockAnalyzer (org.apache.lucene.analysis.MockAnalyzer)39 RandomIndexWriter (org.apache.lucene.index.RandomIndexWriter)36 NumericDocValuesField (org.apache.lucene.document.NumericDocValuesField)34 BytesRef (org.apache.lucene.util.BytesRef)34 TextField (org.apache.lucene.document.TextField)30 IndexReader (org.apache.lucene.index.IndexReader)29 IndexSearcher (org.apache.lucene.search.IndexSearcher)26 IntPoint (org.apache.lucene.document.IntPoint)24 SortedDocValuesField (org.apache.lucene.document.SortedDocValuesField)23 TopDocs (org.apache.lucene.search.TopDocs)23 SortField (org.apache.lucene.search.SortField)22 SortedNumericDocValuesField (org.apache.lucene.document.SortedNumericDocValuesField)21 Sort (org.apache.lucene.search.Sort)21 BinaryDocValuesField (org.apache.lucene.document.BinaryDocValuesField)18 MatchAllDocsQuery (org.apache.lucene.search.MatchAllDocsQuery)18