Java Code Examples for org.apache.solr.common.params.ModifiableSolrParams#add()

The following examples show how to use org.apache.solr.common.params.ModifiableSolrParams#add() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AbstractXJoinTestCase.java    From BioSolr with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("rawtypes")
protected NamedList test(ModifiableSolrParams params, String componentName) {
  SolrCore core = h.getCore();

  SearchComponent sc = core.getSearchComponent(componentName);
  assertTrue("XJoinSearchComponent not found in solrconfig", sc != null);
    
  QParserPlugin qp = core.getQueryPlugin("xjoin");
  assertTrue("XJoinQParserPlugin not found in solrconfig", qp != null);
  
  params.add("q", "*:*");
  params.add("fq", "{!xjoin}" + componentName);

  SolrQueryResponse rsp = new SolrQueryResponse();
  rsp.add("responseHeader", new SimpleOrderedMap<>());
  SolrQueryRequest req = new LocalSolrQueryRequest(core, params);

  SolrRequestHandler handler = core.getRequestHandler("standard");
  handler.handleRequest(req, rsp);
  req.close();
  assertNull(rsp.getException());
    
  return rsp.getValues();
}
 
Example 2
Source File: TestMerge.java    From BioSolr with Apache License 2.0 6 votes vote down vote up
/**
 * Default values in the schema should not override existing values.
 */
@Test
public void testDefaultExists() throws Exception {
  try (SolrCore core = h.getCoreContainer().getCore("merge")) {
    ModifiableSolrParams params = new ModifiableSolrParams();
    params.add("q", "*:*");
    params.add("shards", "shard4/");
    params.add("fl", "default");

    SolrDocumentList docs = queryDocs(core, "merge", params);
    assertEquals(1, docs.size());
    
    SolrDocument doc0 = docs.get(0);
    assertEquals("xxx", doc0.getFieldValue("default"));
  }   
}
 
Example 3
Source File: SuggestionRequestHandlerTest.java    From vind with Apache License 2.0 6 votes vote down vote up
@Test
public void sortingTest() {
    ModifiableSolrParams params = new ModifiableSolrParams();

    params.add(SuggestionRequestParams.SUGGESTION,"true");
    params.add(CommonParams.QT,"/suggester");
    params.add(CommonParams.Q,"s");
    params.add(SuggestionRequestParams.SUGGESTION_FIELD,"dynamic_multi_stored_suggest_analyzed_name");
    params.add(SuggestionRequestParams.SUGGESTION_FIELD,"dynamic_multi_stored_suggest_analyzed_place");

    SolrQueryRequest req = new LocalSolrQueryRequest( core, params );

    assertQ("suggester - test single sorting for 's' with 2 facets", req,
            "//response/lst[@name='suggestions']/lst[@name='suggestion_facets']/lst[@name='dynamic_multi_stored_suggest_analyzed_name']/int[1][.='2']",
            "//response/lst[@name='suggestions']/lst[@name='suggestion_facets']/lst[@name='dynamic_multi_stored_suggest_analyzed_name']/int[2][.='1']",
            "//response/lst[@name='suggestions']/lst[@name='suggestion_facets']/lst[@name='dynamic_multi_stored_suggest_analyzed_place']/int[1][.='2']");

    //TODO fix
    /*assertQ("suggester - test multi sorting for 's' with 2 facets",req,
            "//response/lst[@name='suggestions']/lst[@name='suggestion_facets']/lst[1]/int[@name='count'][.='2']",
            "//response/lst[@name='suggestions']/lst[@name='suggestion_facets']/lst[2]/int[@name='count'][.='2']",
            "//response/lst[@name='suggestions']/lst[@name='suggestion_facets']/lst[3]/int[@name='count'][.='1']");
   */
}
 
Example 4
Source File: TestRedisQParserPluginIT.java    From solr-redis with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldOrderDocumentsLrange() {
  final String[] doc1 = {"id", "1", "string_field", "element1"};
  final String[] doc2 = {"id", "2", "string_field", "element2"};
  final String[] doc3 = {"id", "3", "string_field", "element3"};
  final String[] doc4 = {"id", "4", "string_field", "element4"};
  assertU(adoc(doc1));
  assertU(adoc(doc2));
  assertU(adoc(doc3));
  assertU(adoc(doc4));
  assertU(commit());

  jedis.rpush("test_list2", "element3");
  jedis.rpush("test_list2", "element4");
  jedis.rpush("test_list2", "element1");
  jedis.rpush("test_list2", "element2");

  final ModifiableSolrParams params = new ModifiableSolrParams();
  params.add("q", "{!redis command=lrange key=test_list2}string_field");
  params.add("sort", "id asc");
  assertQ(req(params), "*[count(//doc)=4]", "//result/doc[1]/str[@name='id'][.='1']",
      "//result/doc[2]/str[@name='id'][.='2']", "//result/doc[3]/str[@name='id'][.='3']",
      "//result/doc[4]/str[@name='id'][.='4']");
}
 
Example 5
Source File: OverseerCollectionMessageHandler.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
/**
 * Send request to all replicas of a slice
 * @return List of replicas which is not live for receiving the request
 */
public List<Replica> sliceCmd(ClusterState clusterState, ModifiableSolrParams params, Replica.State stateMatcher,
              Slice slice, ShardHandler shardHandler) {
  List<Replica> notLiveReplicas = new ArrayList<>();
  for (Replica replica : slice.getReplicas()) {
    if ((stateMatcher == null || Replica.State.getState(replica.getStr(ZkStateReader.STATE_PROP)) == stateMatcher)) {
      if (clusterState.liveNodesContain(replica.getStr(ZkStateReader.NODE_NAME_PROP))) {
        // For thread safety, only simple clone the ModifiableSolrParams
        ModifiableSolrParams cloneParams = new ModifiableSolrParams();
        cloneParams.add(params);
        cloneParams.set(CoreAdminParams.CORE, replica.getStr(ZkStateReader.CORE_NAME_PROP));

        sendShardRequest(replica.getStr(ZkStateReader.NODE_NAME_PROP), cloneParams, shardHandler);
      } else {
        notLiveReplicas.add(replica);
      }
    }
  }
  return notLiveReplicas;
}
 
Example 6
Source File: TestXJoinQParserPlugin.java    From BioSolr with Apache License 2.0 5 votes vote down vote up
private static Query parse(String v) throws SyntaxError {
  ModifiableSolrParams localParams = new ModifiableSolrParams();
  localParams.add(QueryParsing.V, v);
  QParserPlugin qpp = core.getQueryPlugin(PARSER_NAME);
  QParser qp = qpp.createParser(null, localParams, null, req);
  return qp.parse();
}
 
Example 7
Source File: TestTaggedQueryHighlighterIT.java    From solr-redis with Apache License 2.0 5 votes vote down vote up
@Test
public void testNoHighlightingForWrongTag() {
  final String[] doc1 = {"id", "1", "string_field", "test1"};
  assertU(adoc(doc1));
  assertU(commit());

  ModifiableSolrParams params = new ModifiableSolrParams();
  params.add("q", "{!redis command=smembers key=test_set1 v=string_field tag=test_tag}");
  params.add("hl", "true");
  params.add("hl.fl", "wrong_tag");
  assertQ(req(params),
          "*[count(//doc)=1]",
          "count(//lst[(@name='highlighting')]/*)=0");
}
 
Example 8
Source File: TestMerge.java    From BioSolr with Apache License 2.0 5 votes vote down vote up
/**
 * Fields should be checked for requiredness even if not stored (but only if in field list).
 */
@Test(expected=MergeException.MissingRequiredField.class)
public void testNonStoredRequired() throws Exception {
  try (SolrCore core = h.getCoreContainer().getCore("merge")) {
    ModifiableSolrParams params = new ModifiableSolrParams();
    params.add("q", "*:*");
    params.add("shards", "shard4/");
    params.add("fl", "required");

    queryThrow(core, "merge", params);
  }   
}
 
Example 9
Source File: TestXJoinQParserPlugin.java    From BioSolr with Apache License 2.0 5 votes vote down vote up
private static Query parse(String v) throws SyntaxError {
  ModifiableSolrParams localParams = new ModifiableSolrParams();
  localParams.add(QueryParsing.V, v);
  QParserPlugin qpp = core.getQueryPlugin(PARSER_NAME);
  QParser qp = qpp.createParser(null, localParams, null, req);
  return qp.parse();
}
 
Example 10
Source File: CarrotClusteringEngineTest.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Test
public void testCustomTokenizer() throws Exception {
  final ModifiableSolrParams params = new ModifiableSolrParams();
  params.add(CarrotParams.TITLE_FIELD_NAME, "title");
  params.add(CarrotParams.SNIPPET_FIELD_NAME, "snippet");

  final List<String> labels = getLabels(checkEngine(
      getClusteringEngine("custom-duplicating-tokenizer"), 1, 15, new TermQuery(new Term("title",
          "field")), params).get(0));
  
  // The custom test tokenizer duplicates each token's text
  assertTrue("First token", labels.get(0).contains("TitleTitle"));
}
 
Example 11
Source File: SimpleFacetsTest.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
private ModifiableSolrParams getRandomParamsDate() {
  String field = new String[]{"range_facet_dt_dv", "a_tdt", "bday"}[random().nextInt(3)];
  ModifiableSolrParams params = new ModifiableSolrParams();
  Date[] dates = new Date[2];
  do {
    dates[0] = new Date((long)(random().nextDouble()*(new Date().getTime()) * (random().nextBoolean()?-1:1)));
    dates[1] = new Date((long)(random().nextDouble()*(new Date().getTime()) * (random().nextBoolean()?-1:1)));
  } while (dates[0].equals(dates[1]));
  Arrays.sort(dates);
  long dateDiff = (dates[1].getTime() - dates[0].getTime())/1000;
  String gapUnit;
  if (dateDiff < 1000) {
    gapUnit = DATE_GAP_UNITS[random().nextInt(DATE_GAP_UNITS.length)];
  } else if (dateDiff < 10000){
    gapUnit = DATE_GAP_UNITS[1 + random().nextInt(DATE_GAP_UNITS.length - 1)];
  } else if (dateDiff < 100000){
    gapUnit = DATE_GAP_UNITS[2 + random().nextInt(DATE_GAP_UNITS.length - 2)];
  } else if (dateDiff < 1000000){
    gapUnit = DATE_GAP_UNITS[3 + random().nextInt(DATE_GAP_UNITS.length - 3)];
  } else {
    gapUnit = DATE_GAP_UNITS[4 + random().nextInt(DATE_GAP_UNITS.length - 4)];
  }
  int gapNum = random().nextInt(100) + 1;
  
  params.add(FacetParams.FACET_RANGE_START, dates[0].toInstant().toString());
  params.add(FacetParams.FACET_RANGE_END, dates[1].toInstant().toString());
  params.add(FacetParams.FACET_RANGE_GAP, String.format(Locale.ROOT, "+%d%s", gapNum, gapUnit));
  addCommonRandomRangeParams(params);
  params.add(FacetParams.FACET_RANGE, field);
  return params;
}
 
Example 12
Source File: TestRandomFlRTGCloud.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
/**
 * Given an ordered list of values to include in a (key) param, randomly groups them (ie: comma separated) 
 * into actual param key=values which are returned as a new SolrParams instance
 */
private static SolrParams buildCommaSepParams(final Random rand, final String key, Collection<String> values) {
  ModifiableSolrParams result = new ModifiableSolrParams();
  List<String> copy = new ArrayList<>(values);
  while (! copy.isEmpty()) {
    List<String> slice = copy.subList(0, random().nextInt(1 + copy.size()));
    result.add(key,String.join(",",slice));
    slice.clear();
  }
  return result;
}
 
Example 13
Source File: PackageStoreAPI.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
private void writeRawFile(SolrQueryRequest req, SolrQueryResponse rsp, String path) {
  ModifiableSolrParams solrParams = new ModifiableSolrParams();
  solrParams.add(CommonParams.WT, FILE_STREAM);
  req.setParams(SolrParams.wrapDefaults(solrParams, req.getParams()));
  rsp.add(FILE_STREAM, (SolrCore.RawWriter) os -> {
    packageStore.get(path, (it) -> {
      try {
        org.apache.commons.io.IOUtils.copy(it.getInputStream(), os);
      } catch (IOException e) {
        throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, "Error reading file" + path);
      }
    }, false);

  });
}
 
Example 14
Source File: SpellCheckCollatorTest.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Test
@SuppressWarnings({"unchecked", "rawtypes"})
public void testCollateWithFilter() throws Exception
{
  SolrCore core = h.getCore();
  SearchComponent speller = core.getSearchComponent("spellcheck");
  assertTrue("speller is null and it shouldn't be", speller != null);

  ModifiableSolrParams params = new ModifiableSolrParams();
  params.add(SpellCheckComponent.COMPONENT_NAME, "true");
  params.add(SpellingParams.SPELLCHECK_BUILD, "true");
  params.add(SpellingParams.SPELLCHECK_COUNT, "10");
  params.add(SpellingParams.SPELLCHECK_COLLATE, "true");
  params.add(SpellingParams.SPELLCHECK_MAX_COLLATION_TRIES, "10");
  params.add(SpellingParams.SPELLCHECK_MAX_COLLATIONS, "10");
  params.add(CommonParams.Q, "lowerfilt:(+fauth +home +loane)");
  params.add(CommonParams.FQ, "NOT(id:1)");

  //Because a FilterQuery is applied which removes doc id#1 from possible hits, we would
  //not want the collations to return us "lowerfilt:(+faith +hope +loaves)" as this only matches doc id#1.
  SolrRequestHandler handler = core.getRequestHandler("/spellCheckCompRH");
  SolrQueryResponse rsp = new SolrQueryResponse();
  rsp.addResponseHeader(new SimpleOrderedMap());
  SolrQueryRequest req = new LocalSolrQueryRequest(core, params);
  handler.handleRequest(req, rsp);
  req.close();
  NamedList values = rsp.getValues();
  NamedList spellCheck = (NamedList) values.get("spellcheck");
  NamedList collationHolder = (NamedList) spellCheck.get("collations");
  List<String> collations = collationHolder.getAll("collation");
  assertTrue(collations.size() > 0);
  for(String collation : collations) {
    assertTrue(!collation.equals("lowerfilt:(+faith +hope +loaves)"));
  }
}
 
Example 15
Source File: CollectionAdminRequest.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
@Override
public SolrParams getParams() {
  ModifiableSolrParams params = new ModifiableSolrParams(super.getParams());
  params.add(CoreAdminParams.COLLECTION, collection);
  assert ((null == routeKey) ^ (null == shard));
  if (null != shard) {
    params.add(CoreAdminParams.SHARD, shard);
  }
  if (null != routeKey) {
    params.add(ShardParams._ROUTE_, routeKey);
  }
  if (node != null) {
    params.add(CoreAdminParams.NODE, node);
  }
  if (instanceDir != null)  {
    params.add(CoreAdminParams.INSTANCE_DIR, instanceDir);
  }
  if (dataDir != null)  {
    params.add(CoreAdminParams.DATA_DIR, dataDir);
  }
  if (ulogDir != null) {
    params.add(CoreAdminParams.ULOG_DIR, ulogDir);
  }
  if (coreName != null) {
    params.add(CoreAdminParams.NAME, coreName);
  }
  if (type != null) {
    params.add(ZkStateReader.REPLICA_TYPE, type.name());
  }
  if (properties != null) {
    addProperties(params, properties);
  }
  if (nrtReplicas != null)  {
    params.add(NRT_REPLICAS, String.valueOf(nrtReplicas));
  }
  if (tlogReplicas != null)  {
    params.add(TLOG_REPLICAS, String.valueOf(tlogReplicas));
  }
  if (pullReplicas != null)  {
    params.add(PULL_REPLICAS, String.valueOf(pullReplicas));
  }
  if (createNodeSet != null)  {
    params.add(CREATE_NODE_SET_PARAM, createNodeSet);
  }
  return params;
}
 
Example 16
Source File: TupleStream.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings({"unchecked"})
public static List<String> getShards(String zkHost,
                                     String collection,
                                     StreamContext streamContext,
                                     SolrParams requestParams)
    throws IOException {
  Map<String, List<String>> shardsMap = null;
  List<String> shards = new ArrayList<>();

  if(streamContext != null) {
    shardsMap = (Map<String, List<String>>)streamContext.get("shards");
  }

  if(shardsMap != null) {
    //Manual Sharding
    shards = shardsMap.get(collection);
  } else {
    //SolrCloud Sharding
    CloudSolrClient cloudSolrClient =
        Optional.ofNullable(streamContext.getSolrClientCache()).orElseGet(SolrClientCache::new).getCloudSolrClient(zkHost);
    ZkStateReader zkStateReader = cloudSolrClient.getZkStateReader();
    ClusterState clusterState = zkStateReader.getClusterState();
    Slice[] slices = CloudSolrStream.getSlices(collection, zkStateReader, true);
    Set<String> liveNodes = clusterState.getLiveNodes();


    ModifiableSolrParams solrParams = new ModifiableSolrParams(streamContext.getRequestParams());
    solrParams.add(requestParams);

    RequestReplicaListTransformerGenerator requestReplicaListTransformerGenerator =
        Optional.ofNullable(streamContext.getRequestReplicaListTransformerGenerator()).orElseGet(RequestReplicaListTransformerGenerator::new);

    ReplicaListTransformer replicaListTransformer = requestReplicaListTransformerGenerator.getReplicaListTransformer(solrParams);

    for(Slice slice : slices) {
      List<Replica> sortedReplicas = new ArrayList<>();
      for(Replica replica : slice.getReplicas()) {
        if(replica.getState() == Replica.State.ACTIVE && liveNodes.contains(replica.getNodeName())) {
          sortedReplicas.add(replica);
        }
      }

      replicaListTransformer.transform(sortedReplicas);
      if (sortedReplicas.size() > 0) {
        shards.add(sortedReplicas.get(0).getCoreUrl());
      }
    }
  }
  Object core = streamContext.get("core");
  if (streamContext != null && streamContext.isLocal() && core != null) {
    shards.removeIf(shardUrl -> !shardUrl.contains((CharSequence) core));
  }

  return shards;
}
 
Example 17
Source File: IssueExampleTest.java    From vind with Apache License 2.0 4 votes vote down vote up
public void test_MBV_357() {

        ModifiableSolrParams params = new ModifiableSolrParams();

        params.add(SuggestionRequestParams.SUGGESTION,"true");
        params.add(CommonParams.QT,"/suggester");
        params.add(CommonParams.Q,"0");
        params.add(SuggestionRequestParams.SUGGESTION_FIELD,"dynamic_multi_stored_suggest_analyzed_index");
        params.add(CommonParams.FQ,"notvalidfield:ASSET");
        params.add(SuggestionRequestParams.SUGGESTION_DF,"suggestions");

        SolrQueryRequest req = new LocalSolrQueryRequest( core, params );

        assertQEx("no error for 'notvalidfield'", "undefined field notvalidfield", req, SolrException.ErrorCode.BAD_REQUEST);

    }
 
Example 18
Source File: GraphHandler.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
private SolrParams adjustParams(SolrParams params) {
  ModifiableSolrParams adjustedParams = new ModifiableSolrParams();
  adjustedParams.add(params);
  adjustedParams.add(CommonParams.OMIT_HEADER, "true");
  return adjustedParams;
}
 
Example 19
Source File: RewriteFacetParametersComponent.java    From SearchServices with GNU Lesser General Public License v3.0 4 votes vote down vote up
private void fixFilterQueries(ModifiableSolrParams fixed, SolrParams params, ResponseBuilder rb)
{
    for(Iterator<String> it = params.getParameterNamesIterator(); it.hasNext(); /**/)
    {
        String name = it.next();
        
        if(name.equals("fq"))
        {
            String[] values = params.getParams(name);
            if(values != null)
            {
                String[] fixedValues = new String[values.length];
                for(int i = 0; i < values.length; i++)
                {
                    String value = values[i];
                    if(value.startsWith("{!"))
                    {
                        fixedValues[i] = value;
                    }
                    else
                    {
                        if(value.startsWith("contentSize():"))
                        {
                            value = "cm:content.size:" + removeQuotes(value.substring("contentSize():".length()));
                        }
                        else if(value.startsWith("mimetype():"))
                        {
                            value = removeQuotes(value.substring("mimetype():".length()));
                            ArrayList<String> expand = MimetypeGroupingQParserPlugin.getReverseMappings().get(value);
                            if(expand == null)
                            {
                                value = "cm:content.mimetype:\""+value+"\"";
                            }
                            else
                            {
                                StringBuilder builder = new StringBuilder();
                                builder.append("cm:content.mimetype:(");
                                for(int j = 0; j < expand.size(); j++)
                                {
                                    if(j > 0)
                                    {
                                        builder.append(" OR ");
                                    }
                                    builder.append('"');
                                    builder.append(expand.get(j));
                                    builder.append('"');
                                }
                                builder.append(')');
                                value = builder.toString();
                                
                            }
                        }
                        fixedValues[i] = "{!afts}"+value;
                    }
                }
                fixed.add(name, fixedValues);
            }
            
        }
    }
}
 
Example 20
Source File: StreamExpressionTest.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
@Test
public void testSearchFacadeStream() throws Exception {

  new UpdateRequest()
      .add(id, "0", "a_s", "hello0", "a_i", "0", "a_f", "0")
      .add(id, "2", "a_s", "hello2", "a_i", "2", "a_f", "0")
      .add(id, "3", "a_s", "hello3", "a_i", "3", "a_f", "3")
      .add(id, "4", "a_s", "hello4", "a_i", "4", "a_f", "4")
      .add(id, "1", "a_s", "hello1", "a_i", "1", "a_f", "1")
      .commit(cluster.getSolrClient(), COLLECTIONORALIAS);

  List<Tuple> tuples;
  StreamContext streamContext = new StreamContext();
  SolrClientCache solrClientCache = new SolrClientCache();
  streamContext.setSolrClientCache(solrClientCache);
  List<String> shardUrls = TupleStream.getShards(cluster.getZkServer().getZkAddress(), COLLECTIONORALIAS, streamContext);

  try {
    StringBuilder buf = new StringBuilder();
    for (String shardUrl : shardUrls) {
      if (buf.length() > 0) {
        buf.append(",");
      }
      buf.append(shardUrl);
    }

    ModifiableSolrParams solrParams = new ModifiableSolrParams();
    solrParams.add("qt", "/stream");
    solrParams.add("expr", "sort(search("+COLLECTIONORALIAS+"), by=\"a_i asc\")");
    SolrStream solrStream = new SolrStream(shardUrls.get(0), solrParams);
    solrStream.setStreamContext(streamContext);
    tuples = getTuples(solrStream);
    assert (tuples.size() == 5);
    assertOrder(tuples, 0, 1, 2, 3, 4);
    assertLong(tuples.get(0), "a_i", 0);
    assertDouble(tuples.get(0), "a_f", 0);
    assertString(tuples.get(0), "a_s", "hello0");

    assertLong(tuples.get(1), "a_i", 1);
    assertDouble(tuples.get(1), "a_f", 1);
    assertString(tuples.get(1), "a_s", "hello1");

    assertLong(tuples.get(2), "a_i", 2);
    assertDouble(tuples.get(2), "a_f", 0);
    assertString(tuples.get(2), "a_s", "hello2");

    assertLong(tuples.get(3), "a_i", 3);
    assertDouble(tuples.get(3), "a_f", 3);
    assertString(tuples.get(3), "a_s", "hello3");

    assertLong(tuples.get(4), "a_i", 4);
    assertDouble(tuples.get(4), "a_f", 4);
    assertString(tuples.get(4), "a_s", "hello4");

  } finally {
    solrClientCache.close();
  }
}