Skip to content
Permalink
Browse files
AMBARI-24847 - Logsearch: Cannot search for a term that includes spac…
…es or dashes (#18)
  • Loading branch information
kasakrisz committed Nov 12, 2018
1 parent 2c7d5f0 commit 9c834609e8205ccd9e96a103a6160e0cb313b803
Showing 9 changed files with 598 additions and 51 deletions.
@@ -59,6 +59,16 @@
<artifactId>solr-solrj</artifactId>
<version>${solr.version}</version>
</dependency>
<dependency>
<groupId>org.apache.solr</groupId>
<artifactId>solr-core</artifactId>
<version>${solr.version}</version>
</dependency>
<dependency>
<groupId>org.apache.solr</groupId>
<artifactId>solr-dataimporthandler</artifactId>
<version>${solr.version}</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
@@ -0,0 +1,115 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.ambari.logsearch.solr;

import static org.apache.ambari.logsearch.solr.SolrConstants.AuditLogConstants.AUDIT_EVTTIME;
import static org.apache.ambari.logsearch.solr.SolrConstants.CommonLogConstants.ID;
import static org.apache.ambari.logsearch.solr.SolrConstants.CommonLogConstants.LOG_MESSAGE;
import static org.apache.ambari.logsearch.solr.SolrConstants.ServiceLogConstants.LEVEL;
import static org.apache.ambari.logsearch.solr.SolrConstants.ServiceLogConstants.LOGTIME;

import java.io.File;
import java.io.IOException;
import java.time.OffsetDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.List;

import org.apache.commons.io.FileUtils;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.embedded.EmbeddedSolrServer;
import org.apache.solr.client.solrj.request.CoreAdminRequest;
import org.apache.solr.client.solrj.response.PivotField;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocumentList;
import org.apache.solr.common.SolrInputDocument;
import org.apache.solr.common.util.NamedList;
import org.apache.solr.core.NodeConfig;
import org.apache.solr.core.SolrResourceLoader;

public class Solr {
public static final DateTimeFormatter SOLR_DATETIME_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSX");

public static Solr core(String coreName) throws IOException, SolrServerException {
assert coreName != null;

String targetLocation = Solr.class.getProtectionDomain().getCodeSource().getLocation().getFile() + "..";
String logsearchConfigSetDir = targetLocation + "/../../ambari-logsearch-server/src/main/configsets";
File targetConfigSetDir = new File(targetLocation + "/configsets");
if (targetConfigSetDir.exists())
FileUtils.deleteDirectory(targetConfigSetDir);
FileUtils.copyDirectory(new File(logsearchConfigSetDir), targetConfigSetDir);
String solrHome = targetLocation + "/solr";
File solrHomeDir = new File(solrHome);
if (solrHomeDir.exists())
FileUtils.deleteDirectory(solrHomeDir);
solrHomeDir.mkdirs();

SolrResourceLoader solrResourceLoader = new SolrResourceLoader(solrHomeDir.toPath());

NodeConfig config = new NodeConfig.NodeConfigBuilder("embeddedSolrServerNode", solrResourceLoader)
.setConfigSetBaseDirectory(targetConfigSetDir.getAbsolutePath())
.build();

EmbeddedSolrServer embeddedSolrServer = new EmbeddedSolrServer(config, coreName);

CoreAdminRequest.Create createRequest = new CoreAdminRequest.Create();
createRequest.setCoreName(coreName);
createRequest.setConfigSet(coreName);
embeddedSolrServer.request(createRequest);

return new Solr(embeddedSolrServer);
}

private final EmbeddedSolrServer server;

private Solr(EmbeddedSolrServer solrServer) {
server = solrServer;
}

public void addDoc(String id, String logMessage) throws SolrServerException, IOException {
SolrInputDocument doc = new SolrInputDocument();
doc.addField(ID, id);
doc.addField(LOGTIME, new Date(OffsetDateTime.now().toInstant().toEpochMilli()));
doc.addField(AUDIT_EVTTIME, new Date(OffsetDateTime.now().toInstant().toEpochMilli()));
doc.addField(LOG_MESSAGE, logMessage);
doc.addField(LEVEL, "INFO");
server.add(doc);
server.commit();
}

public SolrDocumentList executeQuery(SolrQuery solrQuery) throws SolrServerException, IOException {
return server.query(solrQuery).getResults();
}

public NamedList<List<PivotField>> executeFacetQuery(SolrQuery solrQuery) throws SolrServerException, IOException {
QueryResponse qResp = server.query(solrQuery);
return qResp.getFacetPivot();
}

public void clear() throws Exception {
server.deleteByQuery("*:*");
server.commit();
}

public void close() throws IOException {
server.close();
}
}
@@ -0,0 +1,60 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.ambari.logsearch.solr;

import org.apache.solr.common.SolrDocument;
import org.hamcrest.Description;
import org.hamcrest.TypeSafeDiagnosingMatcher;

public class SolrDocumentMatcher extends TypeSafeDiagnosingMatcher<SolrDocument> {

public static SolrDocumentMatcher solrDoc(String expectedId) {
return new SolrDocumentMatcher(expectedId);
}

private final String expectedId;

private SolrDocumentMatcher(String expectedId) {
this.expectedId = expectedId;
}

@Override
protected boolean matchesSafely(SolrDocument item, Description mismatchDescription) {
if (!item.containsKey("id")) {
mismatchDescription.appendText("SolrDocument without 'id' field.");
return false;
}
if (!expectedId.equals(item.get("id"))) {
describe(mismatchDescription, item.get("id"));
return false;
}
return true;
}

@Override
public void describeTo(Description description) {
describe(description, expectedId);
}

private void describe(Description description, Object id) {
description.appendText("SolrDocument[id=");
description.appendValue(id);
description.appendText("]");
}
}

0 comments on commit 9c83460

Please sign in to comment.