Autopsy  4.19.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 import org.apache.solr.client.solrj.impl.BaseHttpSolrClient;
32 
38 final 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-2021 Basis Technology. Generated on: Fri Aug 6 2021
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.