三个表单字段意味着经验和技能和位置我想要搜索技能或技能与位置或技能与经验..但这里技能是mandiatory ..我只写搜索技能但我想要搜索与上面提到的不同pls帮助我
这里我搜索技能..代码
//客户端类
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;
}
}
答案 0 :(得分:1)
如果你只想要技能:
GET /hotlist/candidate/_search
{
"query": {
"bool": {
"must": [
{
"query_string": {
"default_field": "skills",
"query": "java,html"
}
}
]
}
}
}
所以在Java API中应该是:
searchSourceBuilder.query(QueryBuilders.boolQuery()
.must(QueryBuilders.queryStringQuery(skills).defaultField("skills"));
如果你想要技能和JobLocations:
GET /hotlist/candidate/_search
{
"query": {
"bool": {
"must": [
{
"query_string": {
"default_field": "skills",
"query": "java,html"
}
},
{
"term": {
"JobLocations": {
"value": "hyd"
}
}
}
]
}
}
}
在Java API中:
searchSourceBuilder.query(QueryBuilders.boolQuery()
.must(QueryBuilders.queryStringQuery(skills).defaultField("skills"))
.must(QueryBuilders.termQuery("JobLocations", jobLocations))
);
如果你想要所有:
GET /hotlist/candidate/_search
{
"query": {
"bool": {
"must": [
{
"query_string": {
"default_field": "skills",
"query": "java,html"
}
},
{
"term": {
"JobLocations": {
"value": "hyd"
}
}
},
{
"term": {
"Experience": {
"value": "1"
}
}
}
]
}
}
}
在Java API中:
searchSourceBuilder.query(QueryBuilders.boolQuery()
.must(QueryBuilders.queryStringQuery(skills).defaultField("skills"))
.must(QueryBuilders.termQuery("JobLocations", jobLocations))
.must(QueryBuilders.termQuery("Experience", experience))
);
所以你应该有类似的东西:
public List<SearchModel> searchCouple(String skills, String experience, String location)
{
List<SearchModel> list = new ArrayList<SearchModel>();
JestClient client = ElasticClient.getClient();
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
if (!StringUtils.isEmpty(experience)) {
if (!StringUtils.isEmpty(location)) {
//location and experience are present
searchSourceBuilder.query(QueryBuilders.boolQuery()
.must(QueryBuilders.queryStringQuery(skills).defaultField("skills"))
.must(QueryBuilders.termQuery("JobLocations", jobLocations))
.must(QueryBuilders.termQuery("Experience", experience))
);
} else {
//Only experience is present
searchSourceBuilder.query(QueryBuilders.boolQuery()
.must(QueryBuilders.queryStringQuery(skills).defaultField("skills"))
.must(QueryBuilders.termQuery("Experience", experience))
);
}
} else if (!StringUtils.isEmpty(location)) {
//Only location is present
searchSourceBuilder.query(QueryBuilders.boolQuery()
.must(QueryBuilders.queryStringQuery(skills).defaultField("skills"))
.must(QueryBuilders.termQuery("JobLocations", jobLocations))
);
} else {
//Neither location and experience are present
searchSourceBuilder.query(QueryBuilders.boolQuery()
.must(QueryBuilders.queryStringQuery(skills).defaultField("skills"))
);
}
//[...]
答案 1 :(得分:0)
使用下面定义的bool query
。
if (!StringUtils.isEmpty(experience) && !StringUtils.isEmpty(location))
searchSourceBuilder.query(QueryBuilders.boolQuery().must(QueryBuilders.queryString("*"+skills+"*")).
must(QueryBuilders.queryString("*"+experience+"*").defaultOperator("AND"))
.must(QueryBuilders.queryString("*"+location+"*").defaultOperator("AND")));
else if (!StringUtils.isEmpty(experience))
searchSourceBuilder.query(QueryBuilders.boolQuery().must(QueryBuilders.queryString("*"+skills+"*")).
must(QueryBuilders.queryString("*"+experience+"*").defaultOperator("AND")));
else if (!StringUtils.isEmpty(location))
searchSourceBuilder.query(QueryBuilders.boolQuery().must(QueryBuilders.queryString("*"+skills+"*")).
must(QueryBuilders.queryString("*"+location+"*").defaultOperator("AND")));
else
searchSourceBuilder.query(QueryBuilders.boolQuery().must(QueryBuilders.queryString("*"+skills+"*")));