Search in sources :

Example 26 with MapSolrParams

use of org.apache.solr.common.params.MapSolrParams in project xwiki-platform by xwiki.

the class XWikiDismaxQParserPluginTest method withFieldAliasesWhenNoSupportedLocales.

@Test
public void withFieldAliasesWhenNoSupportedLocales() {
    Map<String, String> parameters = new HashMap<String, String>();
    parameters.put("qf", "comment^0.40");
    parameters.put("xwiki.multilingualFields", "title, comment");
    SolrParams paramsWithAliases = plugin.withFieldAliases("title:text", new MapSolrParams(parameters));
    // Aliases for the ROOT locale.
    assertEquals("title__", paramsWithAliases.get("f.title.qf"));
    assertEquals("comment__", paramsWithAliases.get("f.comment.qf"));
}
Also used : MapSolrParams(org.apache.solr.common.params.MapSolrParams) HashMap(java.util.HashMap) MapSolrParams(org.apache.solr.common.params.MapSolrParams) SolrParams(org.apache.solr.common.params.SolrParams) Test(org.junit.Test)

Example 27 with MapSolrParams

use of org.apache.solr.common.params.MapSolrParams in project metron by apache.

the class SolrMetaAlertSearchDao method search.

@Override
public SearchResponse search(SearchRequest searchRequest) throws InvalidSearchException {
    // Need to wrap such that two things are true
    // 1. The provided query is true OR nested query on the alert field is true
    // 2. Metaalert is active OR it's not a metaalert
    String activeStatusClause = MetaAlertConstants.STATUS_FIELD + ":" + MetaAlertStatus.ACTIVE.getStatusString();
    String metaalertTypeClause = config.getSourceTypeField() + ":" + MetaAlertConstants.METAALERT_TYPE;
    // Use the 'v=' form in order to ensure complex clauses are properly handled.
    // Per the docs, the 'which=' clause should be used to identify all metaalert parents, not to
    // filter
    // Status is a filter on parents and must be done outside the '!parent' construct
    String parentChildQuery = "(+" + activeStatusClause + " +" + "{!parent which=" + metaalertTypeClause + " v='" + searchRequest.getQuery() + "'})";
    // Put everything together to get our full query
    // The '-metaalert:[* TO *]' construct is to ensure the field doesn't exist on or is empty for
    // plain alerts.
    // Also make sure that it's not a metaalert
    String fullQuery = "(" + searchRequest.getQuery() + " AND -" + MetaAlertConstants.METAALERT_FIELD + ":[* TO *]" + " AND " + "-" + metaalertTypeClause + ")" + " OR " + parentChildQuery;
    LOG.debug("MetaAlert search query {}", fullQuery);
    searchRequest.setQuery(fullQuery);
    // Build the custom field list
    List<String> fields = searchRequest.getFields();
    String fieldList = "*";
    if (fields != null) {
        fieldList = StringUtils.join(fields, ",");
    }
    LOG.debug("MetaAlert Search Field list {}", fullQuery);
    SearchResponse results = solrSearchDao.search(searchRequest, fieldList);
    LOG.debug("MetaAlert Search Number of results {}", results.getResults().size());
    // wildcard).
    if (fieldList.contains("*") || fieldList.contains(config.getSourceTypeField())) {
        List<String> metaalertGuids = new ArrayList<>();
        for (SearchResult result : results.getResults()) {
            if (result.getSource().get(config.getSourceTypeField()).equals(MetaAlertConstants.METAALERT_TYPE)) {
                // Then we need to add it to the list to retrieve child alerts in a second query.
                metaalertGuids.add(result.getId());
            }
        }
        LOG.debug("MetaAlert Search guids requiring retrieval: {}", metaalertGuids);
        // If we have any metaalerts in our result, attach the full data.
        if (metaalertGuids.size() > 0) {
            Map<String, String> params = new HashMap<>();
            params.put("fl", fieldList + ",[child parentFilter=" + metaalertTypeClause + " limit=999]");
            SolrParams solrParams = new MapSolrParams(params);
            try {
                SolrDocumentList solrDocumentList = solrClient.getById(METAALERTS_COLLECTION, metaalertGuids, solrParams);
                Map<String, Document> guidToDocuments = new HashMap<>();
                for (SolrDocument doc : solrDocumentList) {
                    Document document = SolrUtilities.toDocument(doc);
                    guidToDocuments.put(document.getGuid(), document);
                }
                // Run through our results and update them with the full metaalert
                for (SearchResult result : results.getResults()) {
                    Document fullDoc = guidToDocuments.get(result.getId());
                    if (fullDoc != null) {
                        result.setSource(fullDoc.getDocument());
                    }
                }
            } catch (SolrServerException | IOException e) {
                throw new InvalidSearchException("Error when retrieving child alerts for metaalerts", e);
            }
        }
    }
    return results;
}
Also used : HashMap(java.util.HashMap) SolrServerException(org.apache.solr.client.solrj.SolrServerException) ArrayList(java.util.ArrayList) SearchResult(org.apache.metron.indexing.dao.search.SearchResult) SolrDocumentList(org.apache.solr.common.SolrDocumentList) IOException(java.io.IOException) Document(org.apache.metron.indexing.dao.update.Document) SolrDocument(org.apache.solr.common.SolrDocument) SearchResponse(org.apache.metron.indexing.dao.search.SearchResponse) MapSolrParams(org.apache.solr.common.params.MapSolrParams) SolrDocument(org.apache.solr.common.SolrDocument) InvalidSearchException(org.apache.metron.indexing.dao.search.InvalidSearchException) SolrParams(org.apache.solr.common.params.SolrParams) MapSolrParams(org.apache.solr.common.params.MapSolrParams)

Example 28 with MapSolrParams

use of org.apache.solr.common.params.MapSolrParams in project lucene-solr by apache.

the class TestSolrQueryParser method testAutoTerms.

// automatically use TermsQuery when appropriate
@Test
public void testAutoTerms() throws Exception {
    SolrQueryRequest req = req();
    QParser qParser;
    Query q, qq;
    Map<String, String> sowFalseParamsMap = new HashMap<>();
    sowFalseParamsMap.put("sow", "false");
    Map<String, String> sowTrueParamsMap = new HashMap<>();
    sowTrueParamsMap.put("sow", "true");
    List<MapSolrParams> paramMaps = Arrays.asList(// no sow param (i.e. the default sow value) 
    new MapSolrParams(Collections.emptyMap()), new MapSolrParams(sowFalseParamsMap), new MapSolrParams(sowTrueParamsMap));
    for (MapSolrParams params : paramMaps) {
        // relevance query should not be a filter
        qParser = QParser.getParser("foo_s:(a b c)", req);
        qParser.setParams(params);
        q = qParser.getQuery();
        assertEquals(3, ((BooleanQuery) q).clauses().size());
        // small filter query should still use BooleanQuery
        if (QueryParser.TERMS_QUERY_THRESHOLD > 3) {
            qParser = QParser.getParser("foo_s:(a b c)", req);
            qParser.setParams(params);
            // this may change in the future
            qParser.setIsFilter(true);
            q = qParser.getQuery();
            assertEquals(3, ((BooleanQuery) q).clauses().size());
        }
        // large relevancy query should use BooleanQuery
        // TODO: we may decide that string fields shouldn't have relevance in the future... change to a text field w/o a stop filter if so
        qParser = QParser.getParser("foo_s:(a b c d e f g h i j k l m n o p q r s t u v w x y z)", req);
        qParser.setParams(params);
        q = qParser.getQuery();
        assertEquals(26, ((BooleanQuery) q).clauses().size());
        // large filter query should use TermsQuery
        qParser = QParser.getParser("foo_s:(a b c d e f g h i j k l m n o p q r s t u v w x y z)", req);
        // this may change in the future
        qParser.setIsFilter(true);
        qParser.setParams(params);
        q = qParser.getQuery();
        assertEquals(26, ((TermInSetQuery) q).getTermData().size());
        // large numeric filter query should use TermsQuery (for trie fields)
        qParser = QParser.getParser("foo_ti:(1 2 3 4 5 6 7 8 9 10 20 19 18 17 16 15 14 13 12 11)", req);
        // this may change in the future
        qParser.setIsFilter(true);
        qParser.setParams(params);
        q = qParser.getQuery();
        assertEquals(20, ((TermInSetQuery) q).getTermData().size());
        // for point fields large filter query should use PointInSetQuery
        qParser = QParser.getParser("foo_pi:(1 2 3 4 5 6 7 8 9 10 20 19 18 17 16 15 14 13 12 11)", req);
        // this may change in the future
        qParser.setIsFilter(true);
        qParser.setParams(params);
        q = qParser.getQuery();
        assertTrue(q instanceof PointInSetQuery);
        assertEquals(20, ((PointInSetQuery) q).getPackedPoints().size());
        // a filter() clause inside a relevancy query should be able to use a TermsQuery
        qParser = QParser.getParser("foo_s:aaa filter(foo_s:(a b c d e f g h i j k l m n o p q r s t u v w x y z))", req);
        qParser.setParams(params);
        q = qParser.getQuery();
        assertEquals(2, ((BooleanQuery) q).clauses().size());
        qq = ((BooleanQuery) q).clauses().get(0).getQuery();
        if (qq instanceof TermQuery) {
            qq = ((BooleanQuery) q).clauses().get(1).getQuery();
        }
        if (qq instanceof FilterQuery) {
            qq = ((FilterQuery) qq).getQuery();
        }
        assertEquals(26, ((TermInSetQuery) qq).getTermData().size());
        // test mixed boolean query, including quotes (which shouldn't matter)
        qParser = QParser.getParser("foo_s:(a +aaa b -bbb c d e f bar_s:(qqq www) g h i j k l m n o p q r s t u v w x y z)", req);
        // this may change in the future
        qParser.setIsFilter(true);
        qParser.setParams(params);
        q = qParser.getQuery();
        assertEquals(4, ((BooleanQuery) q).clauses().size());
        qq = null;
        for (BooleanClause clause : ((BooleanQuery) q).clauses()) {
            qq = clause.getQuery();
            if (qq instanceof TermInSetQuery)
                break;
        }
        assertEquals(26, ((TermInSetQuery) qq).getTermData().size());
        // test terms queries of two different fields (LUCENE-7637 changed to require all terms be in the same field)
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < 17; i++) {
            char letter = (char) ('a' + i);
            sb.append("foo_s:" + letter + " bar_s:" + letter + " ");
        }
        qParser = QParser.getParser(sb.toString(), req);
        // this may change in the future
        qParser.setIsFilter(true);
        qParser.setParams(params);
        q = qParser.getQuery();
        assertEquals(2, ((BooleanQuery) q).clauses().size());
        for (BooleanClause clause : ((BooleanQuery) q).clauses()) {
            qq = clause.getQuery();
            assertEquals(17, ((TermInSetQuery) qq).getTermData().size());
        }
    }
    req.close();
}
Also used : BooleanQuery(org.apache.lucene.search.BooleanQuery) TermQuery(org.apache.lucene.search.TermQuery) Query(org.apache.lucene.search.Query) PointInSetQuery(org.apache.lucene.search.PointInSetQuery) ConstantScoreQuery(org.apache.lucene.search.ConstantScoreQuery) TermInSetQuery(org.apache.lucene.search.TermInSetQuery) TermQuery(org.apache.lucene.search.TermQuery) FilterQuery(org.apache.solr.query.FilterQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) BoostQuery(org.apache.lucene.search.BoostQuery) HashMap(java.util.HashMap) PointInSetQuery(org.apache.lucene.search.PointInSetQuery) FilterQuery(org.apache.solr.query.FilterQuery) BooleanClause(org.apache.lucene.search.BooleanClause) SolrQueryRequest(org.apache.solr.request.SolrQueryRequest) MapSolrParams(org.apache.solr.common.params.MapSolrParams) TermInSetQuery(org.apache.lucene.search.TermInSetQuery) Test(org.junit.Test)

Example 29 with MapSolrParams

use of org.apache.solr.common.params.MapSolrParams in project lucene-solr by apache.

the class StatsComponentTest method testFieldStatisticsDocValuesAndMultiValuedIntegerFacetStats.

public void testFieldStatisticsDocValuesAndMultiValuedIntegerFacetStats() throws Exception {
    SolrCore core = h.getCore();
    String fieldName = "cat_intDocValues";
    // 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 IRange(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());
    Map<String, String> args = new HashMap<>();
    args.put(CommonParams.Q, "*:*");
    args.put(StatsParams.STATS, "true");
    args.put(StatsParams.STATS_FIELD, fieldName);
    args.put(StatsParams.STATS_FACET, fieldName);
    args.put(StatsParams.STATS_CALC_DISTINCT, "true");
    args.put("indent", "true");
    SolrQueryRequest req = new LocalSolrQueryRequest(core, new MapSolrParams(args));
    assertQEx("can not use FieldCache on multivalued field: cat_intDocValues", req, 400);
}
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 30 with MapSolrParams

use of org.apache.solr.common.params.MapSolrParams in project lucene-solr by apache.

the class StatsComponentTest method testFieldStatisticsDocValuesAndMultiValuedInteger.

public void testFieldStatisticsDocValuesAndMultiValuedInteger() throws Exception {
    SolrCore core = h.getCore();
    String fieldName = "cat_intDocValues";
    // 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 IRange(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());
    Map<String, String> args = new HashMap<>();
    args.put(CommonParams.Q, "*:*");
    args.put(StatsParams.STATS, "true");
    args.put(StatsParams.STATS_FIELD, fieldName);
    args.put(StatsParams.STATS_CALC_DISTINCT, "true");
    args.put("indent", "true");
    SolrQueryRequest req = new LocalSolrQueryRequest(core, new MapSolrParams(args));
    assertQ("test min/max on docValues and multiValued", 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 + "']/long[@name='countDistinct'][.='9']", "//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']");
    assertQ("cardinality", req("q", "*:*", "stats", "true", "stats.field", "{!cardinality=true}" + fieldName), "//lst[@name='" + fieldName + "']/long[@name='cardinality'][.='9']");
}
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)

Aggregations

MapSolrParams (org.apache.solr.common.params.MapSolrParams)50 HashMap (java.util.HashMap)33 SolrCore (org.apache.solr.core.SolrCore)24 LocalSolrQueryRequest (org.apache.solr.request.LocalSolrQueryRequest)20 SolrQueryRequest (org.apache.solr.request.SolrQueryRequest)19 SolrParams (org.apache.solr.common.params.SolrParams)14 Test (org.junit.Test)14 LinkedHashMap (java.util.LinkedHashMap)11 ArrayList (java.util.ArrayList)10 SolrQueryResponse (org.apache.solr.response.SolrQueryResponse)10 SolrException (org.apache.solr.common.SolrException)6 UpdateRequestHandler (org.apache.solr.handler.UpdateRequestHandler)6 SolrQueryRequestBase (org.apache.solr.request.SolrQueryRequestBase)6 Map (java.util.Map)4 ModifiableSolrParams (org.apache.solr.common.params.ModifiableSolrParams)4 Utils.makeMap (org.apache.solr.common.util.Utils.makeMap)4 IOException (java.io.IOException)3 SchemaField (org.apache.solr.schema.SchemaField)3 StringWriter (java.io.StringWriter)2 Collections.singletonMap (java.util.Collections.singletonMap)2