三个表单字段表示经验和技能以及位置我想使用技能或具有位置的技能或具有经验的技能进行搜索..但是这里的技能是强制性的..我只写有技能的搜索,但我想用不同的方式进行搜索,就像上面提到的那样,请帮助我
在这里我搜索技能..代码
// 客户端类
public class ElasticClient
{
private static JestClient client = null;
private ElasticClient(){
}
public static JestClient getClient()
{
String clusterIP = "localhost";
String port = "9200";
//setup client
JestClientFactory factory = new JestClientFactory();
factory.setHttpClientConfig(new HttpClientConfig
.Builder("http://" + clusterIP + ":" + port)
.multiThreaded(true)
.build());
JestClient client = factory.getObject();
return client;
}
}
//模型类
package com.elastic.model;
import io.searchbox.annotations.JestId;
public class SearchModel {
@JestId
private String _id;
private String CurrentJobTitles;
private String Experience;
private String skills;
public SearchModel(String _id, String currentJobTitles, String experience,
String skills) {
super();
this._id = _id;
CurrentJobTitles = currentJobTitles;
Experience = experience;
this.skills = skills;
}
public String get_id() {
return _id;
}
public void set_id(String _id) {
this._id = _id;
}
public String getCurrentJobTitles() {
return CurrentJobTitles;
}
public void setCurrentJobTitles(String currentJobTitles) {
CurrentJobTitles = currentJobTitles;
}
public String getExperience() {
return Experience;
}
public void setExperience(String experience) {
Experience = experience;
}
public String getSkills() {
return skills;
}
public void setSkills(String skills) {
this.skills = skills;
}
@Override
public String toString() {
return
"_id='" + _id + '\'' +
", CurrentJobTitles='" + CurrentJobTitles + '\'' +
", Experience='" + Experience + '\'' +
", skills='" + skills + '\''
;
}
}
//控制器类
@Controller
public class SearchController {
@RequestMapping(value = "/doSearch.htm", method = RequestMethod.POST)
public List<SearchModel> getMessages(@RequestParam("skills") String skills, Model model)
{
System.out.println(skills);
SearchResource search = new SearchResource();
List<SearchModel> list = new ArrayList<SearchModel>();
list = search.searchCouple(searchKey);
System.out.println(list);
model.addAttribute("list",list);
return list;
}
}
// 服务类
public List<SearchModel> searchCouple(String skills)
{
List<SearchModel> list = new ArrayList<SearchModel>();
JestClient client = ElasticClient.getClient();
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
searchSourceBuilder.query(QueryBuilders.queryString("*"+skills+"*"));
Search search = (Search) new Search.Builder(searchSourceBuilder.toString())
.addIndex("eix")
.addType("articles")
.build();
try
{
SearchResult result = client.execute(search);
List<SearchResult.Hit<SearchModel, Void>> list1 = result.getHits(SearchModel.class);
if(!list1.isEmpty())
{
for(int i=0; i<list1.size(); i++)
{
Hit<SearchModel, Void> p = list1.get(i);
SearchModel e = p.source;
list.add(e);
System.out.println(list);
}
}
} catch (Exception e) {
e.printStackTrace();
}
finally {
client.shutdownClient();
}
return list;
}
}