blob: fe40dff92ae8ae672eeca916269906665be611d5 [file] [log] [blame]
/*******************************************************************************
* Copyright (C) 2018 ANSYS medini Technologies AG
*
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* ANSYS medini Technologies AG - initial API and implementation
******************************************************************************/
package org.eclipse.opencert.elastic.search;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import org.eclipse.opencert.elastic.ElasticClient;
import org.eclipse.opencert.elastic.ElasticClientImpl;
/**
* Factory for the {@link ElasticFinder}.
*
* @author mauersberger
*/
public class ElasticFinderImpl implements ElasticFinder {
// XXX hack to get some API tests running
private Collection<Object> dummyData = null;
private ElasticClient client;
/**
* Factory method to create a new {@link ElasticFinder}. XXX This is an
* intermediate feature.
*
* @param data
* @return
*/
public static ElasticFinder onDummy(Object data) {
ElasticFinderImpl impl = new ElasticFinderImpl();
impl.dummyData = DummyData.INSTANCE.convert(data);
return impl;
}
public static ElasticFinder onDefaultClient() {
ElasticClient client = ElasticClientImpl.on("localhost", 9200, "http");
return onClient(client);
}
public static ElasticFinder onClient(ElasticClient client) {
ElasticFinderImpl impl = new ElasticFinderImpl();
impl.client = client;
return impl;
}
/*
* Intentionally private.
*/
private ElasticFinderImpl() {
// just to avoid instantiation, force using the factory API
}
@Override
public Set<Hit> search(String query, Map<String, Object> filters) {
if (dummyData != null) {
return searchInDummyData(query, filters);
}
try {
this.client.search("_all", query, filters);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return Collections.emptySet();
}
/*
* XXX Hack
*/
private Set<Hit> searchInDummyData(String query, Map<String, Object> filters) {
Set<Hit> hits = new HashSet<>();
for (Object data : dummyData) {
String document = DummyData.INSTANCE.asDocument(data);
if ((document.contains(query) || document.matches(query)) && matchFilters(document, filters)) {
matchFilters(document, filters);
Hit hit = new Hit();
// we count 1 for string match + number of filters
hit.score = 1 + (filters != null ? filters.size() : 0);
hit.objectId = DummyData.INSTANCE.getId(document);
hits.add(hit);
System.out.println(document);
}
}
return hits;
}
/*
* XXX Hack
*/
private boolean matchFilters(String document, Map<String, Object> filters) {
// check filters
if (filters == null || filters.isEmpty()) {
return true;
}
for (String name : filters.keySet()) {
String filter = name + ": " + filters.get(name).toString();
if (!document.contains(filter)) {
return false;
}
}
// all filters passed
return true;
}
}