Autopsy 4.22.1
Graphical digital forensics platform for The Sleuth Kit and other tools.
QueryTermHelper.java
Go to the documentation of this file.
1/*
2 * Autopsy Forensic Browser
3 *
4 * Copyright 2011-2019 Basis Technology Corp.
5 * Contact: carrier <at> sleuthkit <dot> org
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 */
19package org.sleuthkit.autopsy.keywordsearch;
20
21import org.apache.solr.client.solrj.SolrServerException;
22import org.apache.solr.client.solrj.request.FieldAnalysisRequest;
23import org.apache.solr.client.solrj.response.AnalysisResponseBase;
24import org.apache.solr.client.solrj.response.FieldAnalysisResponse;
25
26import java.util.HashMap;
27import java.util.Iterator;
28import java.util.List;
29import java.util.Map;
30import java.util.stream.Collectors;
31import org.apache.solr.client.solrj.impl.BaseHttpSolrClient;
32
38final class QueryTermHelper {
39
40 private QueryTermHelper() {}
41
45 static class Result {
49 final Map<String, List<String>> fieldTermsMap = new HashMap<>();
50 }
51
58 static Result parse(String query, List<Server.Schema> fields) throws KeywordSearchModuleException, NoOpenCoreException {
59 Server server = KeywordSearch.getServer();
60
61 FieldAnalysisRequest request = new FieldAnalysisRequest();
62 for (Server.Schema field : fields) {
63 request.addFieldName(field.toString());
64 }
65 // FieldAnalysisRequest requires to set its field value property,
66 // while the corresponding analysis.fieldvalue parameter is not needed in the API.
67 // Setting an empty value does not effect on the result.
68 request.setFieldValue("");
69 request.setQuery(query);
70
71 FieldAnalysisResponse response = new FieldAnalysisResponse();
72 try {
73 response.setResponse(server.request(request));
74 } catch (SolrServerException | BaseHttpSolrClient.RemoteSolrException e) {
75 throw new KeywordSearchModuleException(e);
76 }
77
78 Result result = new Result();
79 for (Map.Entry<String, FieldAnalysisResponse.Analysis> entry : response.getAllFieldNameAnalysis()) {
80 Iterator<AnalysisResponseBase.AnalysisPhase> it = entry.getValue().getQueryPhases().iterator();
81
82 // The last phase is the one which is used in the search process.
83 AnalysisResponseBase.AnalysisPhase lastPhase = null;
84 while (it.hasNext()) {
85 lastPhase = it.next();
86 }
87
88 if (lastPhase != null) {
89 List<String> tokens = lastPhase.getTokens().stream().map(AnalysisResponseBase.TokenInfo::getText).collect(Collectors.toList());
90 result.fieldTermsMap.put(entry.getKey(), tokens);
91 }
92 }
93
94 return result;
95 }
96}

Copyright © 2012-2024 Sleuth Kit Labs. Generated on:
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.