Search in sources :

Example 41 with Document

use of com.rbmhtechnology.vind.api.Document in project vind by RBMHTechnology.

the class SearchService method createNewsItem.

private Document createNewsItem(String _id, String _title, ZonedDateTime _created, int _ranking, String... _categories) {
    Document document = newsItems.createDoc(_id);
    document.setValue(title, _title);
    document.setValue(created, _created);
    document.setValues(category, Arrays.asList(_categories));
    document.setValue(ranking, _ranking);
    return document;
}
Also used : Document(com.rbmhtechnology.vind.api.Document)

Example 42 with Document

use of com.rbmhtechnology.vind.api.Document in project vind by RBMHTechnology.

the class SearchService method index.

public boolean index() {
    GuardianNewsIterator iterator = new GuardianNewsIterator(this.guardianApiKey);
    while (iterator.hasNext()) {
        for (GuardianNewsItem item : iterator.getNext()) {
            Document document = newsItems.createDoc(item.getId());
            document.setValue(title, item.getWebTitle());
            document.setValue(publicationDate, item.getWebPublicationDate());
            document.setValue(category, item.getSectionName());
            document.setValue(kind, item.getType());
            document.setValue(url, item.getWebUrl());
            server.index(document);
        }
        server.commit();
    }
    return true;
}
Also used : GuardianNewsItem(com.rbmhtechnology.vind.demo.step5.guardian.GuardianNewsItem) GuardianNewsIterator(com.rbmhtechnology.vind.demo.step5.guardian.GuardianNewsIterator) Document(com.rbmhtechnology.vind.api.Document)

Example 43 with Document

use of com.rbmhtechnology.vind.api.Document in project vind by RBMHTechnology.

the class SolrSearchServer method getUpdatedSolrDocument.

private SolrInputDocument getUpdatedSolrDocument(SolrInputDocument sdoc, SolrDocument updatedDoc, List<SolrInputDocument> nestedDocs) {
    // TODO:find a better way - non deprecated way
    // Create an input document from the original doc to be updated
    final SolrInputDocument inputDoc = ClientUtils.toSolrInputDocument(updatedDoc);
    // Add nested documents to the doc
    inputDoc.addChildDocuments(nestedDocs);
    // TODO: think about a cleaner solution
    // Update the original document
    sdoc.getFieldNames().stream().filter(// TODO: Add all the special fields or do the oposite check, whether it fits a dynamic Vind field
    fn -> !fn.equals(ID) && !fn.equals(TYPE) && !fn.equals("_version_")).forEach(fn -> {
        final ArrayList fieldOp = (ArrayList) sdoc.getFieldValues(fn);
        fieldOp.stream().forEach(op -> {
            final Set<String> keys = ((HashMap<String, String>) op).keySet();
            keys.stream().forEach(k -> {
                switch(UpdateOperations.valueOf(k)) {
                    case set:
                        inputDoc.setField(fn, ((HashMap<String, String>) op).get(k));
                        break;
                    case add:
                        inputDoc.addField(fn, ((HashMap<String, String>) op).get(k));
                        break;
                    case inc:
                        final Number fieldValue;
                        try {
                            fieldValue = NumberFormat.getInstance().parse((String) inputDoc.getFieldValue(fn));
                        } catch (ParseException e) {
                            throw new RuntimeException();
                        }
                        inputDoc.setField(fn, String.valueOf(fieldValue.floatValue() + 1));
                        break;
                    case remove:
                        inputDoc.removeField(fn);
                        break;
                    case removeregex:
                        final String fieldStringValue = (String) inputDoc.getFieldValue(fn);
                        final String regex = ((HashMap<String, String>) op).get(k);
                        if (regex.matches(fieldStringValue)) {
                            inputDoc.removeField(fn);
                        }
                        break;
                }
            });
        });
    });
    return inputDoc;
}
Also used : Delete(com.rbmhtechnology.vind.api.query.delete.Delete) SearchServerException(com.rbmhtechnology.vind.SearchServerException) URISyntaxException(java.net.URISyntaxException) ZonedDateTime(java.time.ZonedDateTime) LoggerFactory(org.slf4j.LoggerFactory) Asserts(org.apache.http.util.Asserts) StringUtils(org.apache.commons.lang3.StringUtils) SolrServerException(org.apache.solr.client.solrj.SolrServerException) Fieldname(com.rbmhtechnology.vind.solr.backend.SolrUtils.Fieldname) Facet(com.rbmhtechnology.vind.api.query.facet.Facet) UpdateOperation(com.rbmhtechnology.vind.api.query.update.UpdateOperation) ParseException(java.text.ParseException) FileSystemUtils(com.rbmhtechnology.vind.utils.FileSystemUtils) Path(java.nio.file.Path) SearchServer(com.rbmhtechnology.vind.api.SearchServer) Slice(com.rbmhtechnology.vind.api.query.division.Slice) AnnotationUtil(com.rbmhtechnology.vind.annotations.AnnotationUtil) SchemaResponse(org.apache.solr.client.solrj.response.schema.SchemaResponse) Update(com.rbmhtechnology.vind.api.query.update.Update) SchemaRequest(org.apache.solr.client.solrj.request.schema.SchemaRequest) DescriptorSuggestionSearch(com.rbmhtechnology.vind.api.query.suggestion.DescriptorSuggestionSearch) Instant(java.time.Instant) Collectors(java.util.stream.Collectors) com.rbmhtechnology.vind.api.query(com.rbmhtechnology.vind.api.query) RealTimeGet(com.rbmhtechnology.vind.api.query.get.RealTimeGet) Stream(java.util.stream.Stream) HttpSolrClient(org.apache.solr.client.solrj.impl.HttpSolrClient) SolrPingResponse(org.apache.solr.client.solrj.response.SolrPingResponse) SolrQuery(org.apache.solr.client.solrj.SolrQuery) com.rbmhtechnology.vind.api.result(com.rbmhtechnology.vind.api.result) ClientUtils(org.apache.solr.client.solrj.util.ClientUtils) ServiceProvider(com.rbmhtechnology.vind.api.ServiceProvider) SolrInputDocument(org.apache.solr.common.SolrInputDocument) Interval(com.rbmhtechnology.vind.api.query.facet.Interval) SolrRequest(org.apache.solr.client.solrj.SolrRequest) java.util(java.util) ExecutableSuggestionSearch(com.rbmhtechnology.vind.api.query.suggestion.ExecutableSuggestionSearch) FieldDescriptor(com.rbmhtechnology.vind.model.FieldDescriptor) NumberFormat(java.text.NumberFormat) UpdateOperations.set(com.rbmhtechnology.vind.api.query.update.Update.UpdateOperations.set) CollectionUtils(org.apache.commons.collections.CollectionUtils) SearchConfiguration(com.rbmhtechnology.vind.configure.SearchConfiguration) Logger(org.slf4j.Logger) StringSuggestionSearch(com.rbmhtechnology.vind.api.query.suggestion.StringSuggestionSearch) LatLng(com.rbmhtechnology.vind.model.value.LatLng) Resources(com.google.common.io.Resources) DateUtil(org.apache.solr.common.util.DateUtil) QueryResponse(org.apache.solr.client.solrj.response.QueryResponse) org.apache.solr.common.params(org.apache.solr.common.params) IOException(java.io.IOException) NamedList(org.apache.solr.common.util.NamedList) Document(com.rbmhtechnology.vind.api.Document) SolrClient(org.apache.solr.client.solrj.SolrClient) SolrDocument(org.apache.solr.common.SolrDocument) DocumentFactory(com.rbmhtechnology.vind.model.DocumentFactory) Page(com.rbmhtechnology.vind.api.query.division.Page) UpdateOperations(com.rbmhtechnology.vind.api.query.update.Update.UpdateOperations) SolrInputDocument(org.apache.solr.common.SolrInputDocument) ParseException(java.text.ParseException)

Example 44 with Document

use of com.rbmhtechnology.vind.api.Document in project vind by RBMHTechnology.

the class SolrSearchServer method createInputDocument.

private SolrInputDocument createInputDocument(Document doc) {
    final SolrInputDocument document = new SolrInputDocument();
    // add fields
    doc.listFieldDescriptors().values().stream().filter(doc::hasValue).forEach(descriptor -> doc.getFieldContexts(descriptor).stream().forEach(context -> Stream.of(UseCase.values()).forEach(useCase -> {
        final String fieldname = getFieldname(descriptor, useCase, context);
        if (Objects.nonNull(fieldname)) {
            final Object value = doc.getContextualizedValue(descriptor, context);
            final Object caseValue = SolrUtils.FieldValue.getFieldCaseValue(value, descriptor, useCase);
            if (Objects.nonNull(caseValue)) {
                document.addField(fieldname, toSolrJType(caseValue));
            }
        }
    })));
    // add subdocuments
    if (doc.hasChildren()) {
        doc.getChildren().forEach(childDocument -> document.addChildDocument(createInputDocument(childDocument)));
    }
    document.addField(ID, doc.getId());
    document.addField(TYPE, doc.getType());
    return document;
}
Also used : Delete(com.rbmhtechnology.vind.api.query.delete.Delete) SearchServerException(com.rbmhtechnology.vind.SearchServerException) URISyntaxException(java.net.URISyntaxException) ZonedDateTime(java.time.ZonedDateTime) LoggerFactory(org.slf4j.LoggerFactory) Asserts(org.apache.http.util.Asserts) StringUtils(org.apache.commons.lang3.StringUtils) SolrServerException(org.apache.solr.client.solrj.SolrServerException) Fieldname(com.rbmhtechnology.vind.solr.backend.SolrUtils.Fieldname) Facet(com.rbmhtechnology.vind.api.query.facet.Facet) UpdateOperation(com.rbmhtechnology.vind.api.query.update.UpdateOperation) ParseException(java.text.ParseException) FileSystemUtils(com.rbmhtechnology.vind.utils.FileSystemUtils) Path(java.nio.file.Path) SearchServer(com.rbmhtechnology.vind.api.SearchServer) Slice(com.rbmhtechnology.vind.api.query.division.Slice) AnnotationUtil(com.rbmhtechnology.vind.annotations.AnnotationUtil) SchemaResponse(org.apache.solr.client.solrj.response.schema.SchemaResponse) Update(com.rbmhtechnology.vind.api.query.update.Update) SchemaRequest(org.apache.solr.client.solrj.request.schema.SchemaRequest) DescriptorSuggestionSearch(com.rbmhtechnology.vind.api.query.suggestion.DescriptorSuggestionSearch) Instant(java.time.Instant) Collectors(java.util.stream.Collectors) com.rbmhtechnology.vind.api.query(com.rbmhtechnology.vind.api.query) RealTimeGet(com.rbmhtechnology.vind.api.query.get.RealTimeGet) Stream(java.util.stream.Stream) HttpSolrClient(org.apache.solr.client.solrj.impl.HttpSolrClient) SolrPingResponse(org.apache.solr.client.solrj.response.SolrPingResponse) SolrQuery(org.apache.solr.client.solrj.SolrQuery) com.rbmhtechnology.vind.api.result(com.rbmhtechnology.vind.api.result) ClientUtils(org.apache.solr.client.solrj.util.ClientUtils) ServiceProvider(com.rbmhtechnology.vind.api.ServiceProvider) SolrInputDocument(org.apache.solr.common.SolrInputDocument) Interval(com.rbmhtechnology.vind.api.query.facet.Interval) SolrRequest(org.apache.solr.client.solrj.SolrRequest) java.util(java.util) ExecutableSuggestionSearch(com.rbmhtechnology.vind.api.query.suggestion.ExecutableSuggestionSearch) FieldDescriptor(com.rbmhtechnology.vind.model.FieldDescriptor) NumberFormat(java.text.NumberFormat) UpdateOperations.set(com.rbmhtechnology.vind.api.query.update.Update.UpdateOperations.set) CollectionUtils(org.apache.commons.collections.CollectionUtils) SearchConfiguration(com.rbmhtechnology.vind.configure.SearchConfiguration) Logger(org.slf4j.Logger) StringSuggestionSearch(com.rbmhtechnology.vind.api.query.suggestion.StringSuggestionSearch) LatLng(com.rbmhtechnology.vind.model.value.LatLng) Resources(com.google.common.io.Resources) DateUtil(org.apache.solr.common.util.DateUtil) QueryResponse(org.apache.solr.client.solrj.response.QueryResponse) org.apache.solr.common.params(org.apache.solr.common.params) IOException(java.io.IOException) NamedList(org.apache.solr.common.util.NamedList) Document(com.rbmhtechnology.vind.api.Document) SolrClient(org.apache.solr.client.solrj.SolrClient) SolrDocument(org.apache.solr.common.SolrDocument) DocumentFactory(com.rbmhtechnology.vind.model.DocumentFactory) Page(com.rbmhtechnology.vind.api.query.division.Page) UpdateOperations(com.rbmhtechnology.vind.api.query.update.Update.UpdateOperations) SolrInputDocument(org.apache.solr.common.SolrInputDocument)

Example 45 with Document

use of com.rbmhtechnology.vind.api.Document in project vind by RBMHTechnology.

the class SolrSearchServer method execute.

@Override
public SearchResult execute(FulltextSearch search, DocumentFactory factory) {
    final SolrQuery query = buildSolrQuery(search, factory);
    // query
    try {
        solrClientLogger.debug(">>> query({})", query.toString());
        final QueryResponse response = solrClient.query(query, SolrRequest.METHOD.POST);
        if (response != null) {
            final Map<String, Integer> childCounts = SolrUtils.getChildCounts(response);
            final List<Document> documents = SolrUtils.Result.buildResultList(response.getResults(), childCounts, factory, search.getSearchContext());
            final FacetResults facetResults = SolrUtils.Result.buildFacetResult(response, factory, search.getChildrenFactory(), search.getFacets(), search.getSearchContext());
            switch(search.getResultSet().getType()) {
                case page:
                    {
                        return new PageResult(response.getResults().getNumFound(), response.getQTime(), documents, search, facetResults, this, factory).setElapsedTime(response.getElapsedTime());
                    }
                case slice:
                    {
                        return new SliceResult(response.getResults().getNumFound(), response.getQTime(), documents, search, facetResults, this, factory).setElapsedTime(response.getElapsedTime());
                    }
                default:
                    return new PageResult(response.getResults().getNumFound(), response.getQTime(), documents, search, facetResults, this, factory).setElapsedTime(response.getElapsedTime());
            }
        } else {
            throw new SolrServerException("Null result from SolrClient");
        }
    } catch (SolrServerException | IOException e) {
        throw new SearchServerException("Cannot issue query", e);
    }
}
Also used : SolrServerException(org.apache.solr.client.solrj.SolrServerException) IOException(java.io.IOException) SolrInputDocument(org.apache.solr.common.SolrInputDocument) Document(com.rbmhtechnology.vind.api.Document) SolrDocument(org.apache.solr.common.SolrDocument) SolrQuery(org.apache.solr.client.solrj.SolrQuery) QueryResponse(org.apache.solr.client.solrj.response.QueryResponse) SearchServerException(com.rbmhtechnology.vind.SearchServerException)

Aggregations

Document (com.rbmhtechnology.vind.api.Document)50 Test (org.junit.Test)40 SolrInputDocument (org.apache.solr.common.SolrInputDocument)36 SearchServer (com.rbmhtechnology.vind.api.SearchServer)33 SearchResult (com.rbmhtechnology.vind.api.result.SearchResult)27 FulltextSearch (com.rbmhtechnology.vind.api.query.FulltextSearch)23 ZonedDateTime (java.time.ZonedDateTime)20 Interval (com.rbmhtechnology.vind.api.query.facet.Interval)13 Delete (com.rbmhtechnology.vind.api.query.delete.Delete)12 MultiValueFieldDescriptor (com.rbmhtechnology.vind.model.MultiValueFieldDescriptor)12 PageResult (com.rbmhtechnology.vind.api.result.PageResult)11 LatLng (com.rbmhtechnology.vind.model.value.LatLng)11 IOException (java.io.IOException)11 SolrQuery (org.apache.solr.client.solrj.SolrQuery)11 SolrServerException (org.apache.solr.client.solrj.SolrServerException)11 QueryResponse (org.apache.solr.client.solrj.response.QueryResponse)11 SuggestionResult (com.rbmhtechnology.vind.api.result.SuggestionResult)10 SearchConfiguration (com.rbmhtechnology.vind.configure.SearchConfiguration)10 java.util (java.util)10 SolrClient (org.apache.solr.client.solrj.SolrClient)10