Autopsy  4.14.0
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  */
19 package org.sleuthkit.autopsy.keywordsearch;
20 
21 import org.apache.solr.client.solrj.SolrServerException;
22 import org.apache.solr.client.solrj.request.FieldAnalysisRequest;
23 import org.apache.solr.client.solrj.response.AnalysisResponseBase;
24 import org.apache.solr.client.solrj.response.FieldAnalysisResponse;
25 
26 import java.util.HashMap;
27 import java.util.Iterator;
28 import java.util.List;
29 import java.util.Map;
30 import java.util.stream.Collectors;
31 
37 final class QueryTermHelper {
38 
39  private QueryTermHelper() {}
40 
44  static class Result {
48  final Map<String, List<String>> fieldTermsMap = new HashMap<>();
49  }
50 
57  static Result parse(String query, List<Server.Schema> fields) throws KeywordSearchModuleException, NoOpenCoreException {
58  Server server = KeywordSearch.getServer();
59 
60  FieldAnalysisRequest request = new FieldAnalysisRequest();
61  for (Server.Schema field : fields) {
62  request.addFieldName(field.toString());
63  }
64  // FieldAnalysisRequest requires to set its field value property,
65  // while the corresponding analysis.fieldvalue parameter is not needed in the API.
66  // Setting an empty value does not effect on the result.
67  request.setFieldValue("");
68  request.setQuery(query);
69 
70  FieldAnalysisResponse response = new FieldAnalysisResponse();
71  try {
72  response.setResponse(server.request(request));
73  } catch (SolrServerException e) {
74  throw new KeywordSearchModuleException(e);
75  }
76 
77  Result result = new Result();
78  for (Map.Entry<String, FieldAnalysisResponse.Analysis> entry : response.getAllFieldNameAnalysis()) {
79  Iterator<AnalysisResponseBase.AnalysisPhase> it = entry.getValue().getQueryPhases().iterator();
80 
81  // The last phase is the one which is used in the search process.
82  AnalysisResponseBase.AnalysisPhase lastPhase = null;
83  while (it.hasNext()) {
84  lastPhase = it.next();
85  }
86 
87  if (lastPhase != null) {
88  List<String> tokens = lastPhase.getTokens().stream().map(AnalysisResponseBase.TokenInfo::getText).collect(Collectors.toList());
89  result.fieldTermsMap.put(entry.getKey(), tokens);
90  }
91  }
92 
93  return result;
94  }
95 }

Copyright © 2012-2020 Basis Technology. Generated on: Wed Apr 8 2020
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.