Search in sources :

Example 61 with SchemaField

use of org.apache.solr.schema.SchemaField in project lucene-solr by apache.

the class TestSearcherReuse method test.

public void test() throws Exception {
    // seed some docs & segments
    int numDocs = atLeast(1);
    for (int i = 1; i <= numDocs; i++) {
        // NOTE: starting at "1", we'll use id=0 later
        assertU(adoc("id", "" + i));
        if (random().nextBoolean()) {
            assertU(commit());
        }
    }
    // with random merge policies, a regular commit can cause a segment to be flushed that can kick off a background merge
    // that can cause a later commit to actually see changes and open a new searcher.  This should not be possible with optimize
    assertU(optimize());
    // seed a single query into the cache
    assertQ(req("*:*"), "//*[@numFound='" + numDocs + "']");
    final SolrQueryRequest baseReq = req("q", "foo");
    try {
        // we make no index changes in this block, so the searcher should always be the same
        // NOTE: we *have* to call getSearcher() in advance, it's a delayed binding
        final SolrIndexSearcher expectedSearcher = getMainSearcher(baseReq);
        assertU(commit());
        assertSearcherHasNotChanged(expectedSearcher);
        assertU(commit("openSearcher", "true"));
        assertSearcherHasNotChanged(expectedSearcher);
        assertU(commit("softCommit", "true"));
        assertSearcherHasNotChanged(expectedSearcher);
        assertU(commit("softCommit", "true", "openSearcher", "true"));
        assertSearcherHasNotChanged(expectedSearcher);
        assertU(delQ("id:match_no_documents"));
        assertU(commit("softCommit", "true", "openSearcher", "true"));
        assertSearcherHasNotChanged(expectedSearcher);
        // no doc has this id, yet
        assertU(delI("0"));
        assertU(commit("softCommit", "true", "openSearcher", "true"));
        assertSearcherHasNotChanged(expectedSearcher);
    } finally {
        baseReq.close();
    }
    // now do a variety of things that *should* always guarantee a new searcher
    SolrQueryRequest beforeReq;
    beforeReq = req("q", "foo");
    try {
        // NOTE: we *have* to call getSearcher() in advance: delayed binding
        SolrIndexSearcher before = getMainSearcher(beforeReq);
        assertU(delI("1"));
        assertU(commit());
        assertSearcherHasChanged(before);
    } finally {
        beforeReq.close();
    }
    beforeReq = req("q", "foo");
    try {
        // NOTE: we *have* to call getSearcher() in advance: delayed binding
        SolrIndexSearcher before = getMainSearcher(beforeReq);
        assertU(adoc("id", "0"));
        assertU(commit());
        assertSearcherHasChanged(before);
    } finally {
        beforeReq.close();
    }
    beforeReq = req("q", "foo");
    try {
        // NOTE: we *have* to call getSearcher() in advance: delayed binding
        SolrIndexSearcher before = getMainSearcher(beforeReq);
        assertU(delQ("id:[0 TO 5]"));
        assertU(commit());
        assertSearcherHasChanged(before);
    } finally {
        beforeReq.close();
    }
    beforeReq = req("q", "foo");
    try {
        // NOTE: we *have* to call getSearcher() in advance: delayed binding
        SolrIndexSearcher before = getMainSearcher(beforeReq);
        // create a new field & add it.
        assertTrue("schema not mutable", beforeReq.getSchema().isMutable());
        ManagedIndexSchema oldSchema = (ManagedIndexSchema) beforeReq.getSchema();
        SchemaField newField = oldSchema.newField("hoss", "string", Collections.<String, Object>emptyMap());
        IndexSchema newSchema = oldSchema.addField(newField);
        h.getCore().setLatestSchema(newSchema);
        // sanity check, later asserts assume this
        assertNotSame(oldSchema, newSchema);
        // the schema has changed - but nothing has requested a new Searcher yet
        assertSearcherHasNotChanged(before);
        // only now should we get a new searcher...
        assertU(commit("softCommit", "true", "openSearcher", "true"));
        assertSearcherHasChanged(before);
        // sanity that opening the new searcher was useful to get new schema...
        SolrQueryRequest afterReq = req("q", "foo");
        try {
            assertSame(newSchema, afterReq.getSchema());
            assertSame(newSchema, getMainSearcher(afterReq).getSchema());
        } finally {
            afterReq.close();
        }
    } finally {
        beforeReq.close();
    }
}
Also used : SchemaField(org.apache.solr.schema.SchemaField) SolrQueryRequest(org.apache.solr.request.SolrQueryRequest) ManagedIndexSchema(org.apache.solr.schema.ManagedIndexSchema) IndexSchema(org.apache.solr.schema.IndexSchema) ManagedIndexSchema(org.apache.solr.schema.ManagedIndexSchema)

Example 62 with SchemaField

use of org.apache.solr.schema.SchemaField in project lucene-solr by apache.

the class TestSolr4Spatial method testSpatialConfig.

@Test
public void testSpatialConfig() throws Exception {
    try (SolrCore core = h.getCoreInc()) {
        IndexSchema schema = core.getLatestSchema();
        // BBox Config
        // Make sure the subfields are not stored
        SchemaField sub = schema.getField("bbox" + BBoxStrategy.SUFFIX_MINX);
        assertFalse(sub.stored());
        // Make sure solr field type is also not stored
        BBoxField bbox = (BBoxField) schema.getField("bbox").getType();
        BBoxStrategy strategy = bbox.getStrategy("bbox");
        assertFalse(strategy.getFieldType().stored());
    }
}
Also used : SchemaField(org.apache.solr.schema.SchemaField) BBoxStrategy(org.apache.solr.legacy.BBoxStrategy) SolrCore(org.apache.solr.core.SolrCore) BBoxField(org.apache.solr.schema.BBoxField) IndexSchema(org.apache.solr.schema.IndexSchema) Test(org.junit.Test)

Example 63 with SchemaField

use of org.apache.solr.schema.SchemaField in project lucene-solr by apache.

the class StatsComponentTest method testFieldStatisticsDocValuesAndMultiValued.

// SOLR-6024
public void testFieldStatisticsDocValuesAndMultiValued() throws Exception {
    SolrCore core = h.getCore();
    // precondition for the test
    SchemaField catDocValues = core.getLatestSchema().getField("cat_docValues");
    assertTrue("schema no longer satisfies test requirements: cat_docValues no longer multivalued", catDocValues.multiValued());
    assertTrue("schema no longer satisfies test requirements: cat_docValues fieldtype no longer single valued", !catDocValues.getType().isMultiValued());
    assertTrue("schema no longer satisfies test requirements: cat_docValues no longer has docValues", catDocValues.hasDocValues());
    List<FldType> types = new ArrayList<>();
    types.add(new FldType("id", ONE_ONE, new SVal('A', 'Z', 4, 4)));
    types.add(new FldType("cat_docValues", new IRange(2, 2), new SVal('a', 'z', 1, 30)));
    Doc d1 = createDoc(types);
    d1.getValues("id").set(0, "1");
    d1.getValues("cat_docValues").set(0, "test");
    d1.getValues("cat_docValues").set(1, "testtw");
    updateJ(toJSON(d1), null);
    Doc d2 = createDoc(types);
    d2.getValues("id").set(0, "2");
    d2.getValues("cat_docValues").set(0, "test");
    d2.getValues("cat_docValues").set(1, "testtt");
    updateJ(toJSON(d2), null);
    assertU(commit());
    Map<String, String> args = new HashMap<>();
    args.put(CommonParams.Q, "*:*");
    args.put(StatsParams.STATS, "true");
    args.put(StatsParams.STATS_FIELD, "cat_docValues");
    args.put("indent", "true");
    SolrQueryRequest req = new LocalSolrQueryRequest(core, new MapSolrParams(args));
    assertQ("test min/max on docValues and multiValued", req, "//lst[@name='cat_docValues']/str[@name='min'][.='test']", "//lst[@name='cat_docValues']/str[@name='max'][.='testtw']");
    assertQ("cardinality", req("q", "*:*", "stats", "true", "stats.field", "{!cardinality=true}cat_docValues"), "//lst[@name='cat_docValues']/long[@name='cardinality'][.='3']");
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) SolrCore(org.apache.solr.core.SolrCore) ArrayList(java.util.ArrayList) SchemaField(org.apache.solr.schema.SchemaField) LocalSolrQueryRequest(org.apache.solr.request.LocalSolrQueryRequest) SolrQueryRequest(org.apache.solr.request.SolrQueryRequest) LocalSolrQueryRequest(org.apache.solr.request.LocalSolrQueryRequest) MapSolrParams(org.apache.solr.common.params.MapSolrParams)

Example 64 with SchemaField

use of org.apache.solr.schema.SchemaField in project lucene-solr by apache.

the class StatsComponentTest method testFieldStatisticsDocValuesAndMultiValuedDouble.

public void testFieldStatisticsDocValuesAndMultiValuedDouble() throws Exception {
    SolrCore core = h.getCore();
    String fieldName = "cat_floatDocValues";
    // precondition for the test
    SchemaField catDocValues = core.getLatestSchema().getField(fieldName);
    assertTrue("schema no longer satisfies test requirements: cat_docValues no longer multivalued", catDocValues.multiValued());
    assertTrue("schema no longer satisfies test requirements: cat_docValues fieldtype no longer single valued", !catDocValues.getType().isMultiValued());
    assertTrue("schema no longer satisfies test requirements: cat_docValues no longer has docValues", catDocValues.hasDocValues());
    List<FldType> types = new ArrayList<>();
    types.add(new FldType("id", ONE_ONE, new SVal('A', 'Z', 4, 4)));
    types.add(new FldType(fieldName, ONE_ONE, new FVal(0, 0)));
    Doc d1 = createDocValuesDocument(types, fieldName, "1", -1, 3, 5);
    updateJ(toJSON(d1), null);
    Doc d2 = createDocValuesDocument(types, fieldName, "2", 3, -2, 6);
    updateJ(toJSON(d2), null);
    Doc d3 = createDocValuesDocument(types, fieldName, "3", 16, -3, 11);
    updateJ(toJSON(d3), null);
    assertU(commit());
    final SolrParams baseParams = params(CommonParams.Q, "*:*", "indent", "true", StatsParams.STATS, "true");
    SolrQueryRequest req1 = req(baseParams, StatsParams.STATS_CALC_DISTINCT, "true", StatsParams.STATS_FIELD, fieldName);
    SolrQueryRequest req2 = req(baseParams, StatsParams.STATS_FIELD, "{!min=true, max=true, count=true, sum=true, mean=true, stddev=true, sumOfSquares=true, missing=true, calcdistinct=true}" + fieldName);
    SolrQueryRequest req3 = req(baseParams, StatsParams.STATS_FIELD, "{!min=true, max=true, count=true, sum=true, mean=true, stddev=true, sumOfSquares=true, missing=true, countDistinct=true, distinctValues=true}" + fieldName);
    for (SolrQueryRequest req : new SolrQueryRequest[] { req1, req2, req3 }) {
        assertQ("test status on docValues and multiValued: " + req.toString(), req, "//lst[@name='" + fieldName + "']/double[@name='min'][.='-3.0']", "//lst[@name='" + fieldName + "']/double[@name='max'][.='16.0']", "//lst[@name='" + fieldName + "']/long[@name='count'][.='12']", "//lst[@name='" + fieldName + "']/double[@name='sum'][.='38.0']", "//lst[@name='" + fieldName + "']/double[@name='mean'][.='3.1666666666666665']", "//lst[@name='" + fieldName + "']/double[@name='stddev'][.='5.638074031784151']", "//lst[@name='" + fieldName + "']/double[@name='sumOfSquares'][.='470.0']", "//lst[@name='" + fieldName + "']/long[@name='missing'][.='0']", "//lst[@name='" + fieldName + "']/long[@name='countDistinct'][.='9']", // always comes along with countDistinct
        "count(//lst[@name='" + fieldName + "']/arr[@name='distinctValues']/float)=9", // if new default stats are added, this will break - update test to assert values for each
        "count(//lst[@name='" + fieldName + "']/*)=10");
    }
    assertQ("cardinality", req("q", "*:*", "stats", "true", "stats.field", "{!cardinality=true}" + fieldName), "//lst[@name='" + fieldName + "']/long[@name='cardinality'][.='9']");
}
Also used : SchemaField(org.apache.solr.schema.SchemaField) SolrQueryRequest(org.apache.solr.request.SolrQueryRequest) LocalSolrQueryRequest(org.apache.solr.request.LocalSolrQueryRequest) SolrCore(org.apache.solr.core.SolrCore) ArrayList(java.util.ArrayList) SolrParams(org.apache.solr.common.params.SolrParams) MapSolrParams(org.apache.solr.common.params.MapSolrParams)

Example 65 with SchemaField

use of org.apache.solr.schema.SchemaField in project lucene-solr by apache.

the class StatsComponentTest method testStatsFacetMultivaluedErrorHandling.

public void testStatsFacetMultivaluedErrorHandling() throws Exception {
    SolrCore core = h.getCore();
    SchemaField foo_ss = core.getLatestSchema().getField("foo_ss");
    assertU(adoc("id", "1", "active_i", "1", "foo_ss", "aa"));
    assertU(commit());
    assertU(adoc("id", "2", "active_i", "1", "foo_ss", "bb"));
    assertU(adoc("id", "3", "active_i", "5", "foo_ss", "aa"));
    assertU(commit());
    assertTrue("schema no longer satisfies test requirements: foo_ss no longer multivalued", foo_ss.multiValued());
    assertTrue("schema no longer satisfies test requirements: foo_ss's fieldtype no longer single valued", !foo_ss.getType().isMultiValued());
    assertQEx("no failure trying to get stats facet on foo_ss", req("q", "*:*", "stats", "true", "stats.field", "active_i", "stats.facet", "foo_ss"), 400);
}
Also used : SchemaField(org.apache.solr.schema.SchemaField) SolrCore(org.apache.solr.core.SolrCore)

Aggregations

SchemaField (org.apache.solr.schema.SchemaField)167 SolrException (org.apache.solr.common.SolrException)45 FieldType (org.apache.solr.schema.FieldType)37 ArrayList (java.util.ArrayList)36 IndexSchema (org.apache.solr.schema.IndexSchema)32 NamedList (org.apache.solr.common.util.NamedList)27 BytesRef (org.apache.lucene.util.BytesRef)20 IOException (java.io.IOException)17 Query (org.apache.lucene.search.Query)17 SolrIndexSearcher (org.apache.solr.search.SolrIndexSearcher)17 HashMap (java.util.HashMap)16 Document (org.apache.lucene.document.Document)16 SolrParams (org.apache.solr.common.params.SolrParams)16 SimpleOrderedMap (org.apache.solr.common.util.SimpleOrderedMap)16 SolrQueryRequest (org.apache.solr.request.SolrQueryRequest)15 IndexableField (org.apache.lucene.index.IndexableField)14 Map (java.util.Map)13 LeafReaderContext (org.apache.lucene.index.LeafReaderContext)13 SolrDocument (org.apache.solr.common.SolrDocument)13 SolrInputDocument (org.apache.solr.common.SolrInputDocument)13