原生rest:
对“不好”进行分词, "operator": "and"
意思是同时满足。
POST corpus_details_22/_search
{
"query": {
"dis_max": {
"queries": [
{
"match": {
"sourceContent": {
"query": "不好",
"operator": "and"
}
}
}
]
}
}
}
结果:
{
"took" : 0,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 3,
"relation" : "eq"
},
"max_score" : 2.4820914,
"hits" : [
{
"_index" : "corpus_details_22",
"_type" : "_doc",
"_id" : "gqYAxYMBXTGsShgCkhpA",
"_score" : 2.4820914,
"_source" : {
"targetContent" : "haobuhao .",
"sourceContent" : "好不好。",
"realmCode" : 0,
"sourceLanguageId" : 1,
"corpusScore" : 1,
"id" : 5,
"targetLanguageId" : 2
}
},
{
"_index" : "corpus_details_22",
"_type" : "_doc",
"_id" : "g6YAxYMBXTGsShgCkhqB",
"_score" : 2.2032433,
"_source" : {
"targetContent" : "nibuhao .",
"sourceContent" : "你不好。",
"realmCode" : 0,
"sourceLanguageId" : 1,
"corpusScore" : 0.4,
"id" : 6,
"targetLanguageId" : 2
}
},
{
"_index" : "corpus_details_22",
"_type" : "_doc",
"_id" : "gKYAxYMBXTGsShgCkRq5",
"_score" : 1.9686708,
"_source" : {
"targetContent" : "not very good .",
"sourceContent" : "不怎么好。",
"realmCode" : 0,
"sourceLanguageId" : 1,
"corpusScore" : 1,
"id" : 3,
"targetLanguageId" : 2
}
}
]
}
}
java RestHighLevelClient
public CorpusDetailsLikesVo getLikesData(String indexName, String target) throws IOException {
RestHighLevelClient client = RestHighLevelClientPoolUtil.getClient();
SearchRequest request = new SearchRequest(indexName);
SearchSourceBuilder builder = new SearchSourceBuilder();
DisMaxQueryBuilder disMaxQuery = QueryBuilders.disMaxQuery();
// 复合查询
disMaxQuery.innerQueries().add(
QueryBuilders.matchQuery(
"sourceContent", target
).operator(Operator.AND)
);
// 组装查询条件
builder.query(disMaxQuery);
// 返回数据
request.source(builder);
SearchResponse response = client.search(request, RequestOptions.DEFAULT);
for (SearchHit hit : response.getHits().getHits()) {
Map<String, Object> sourceAsMap = hit.getSourceAsMap();
if (!sourceAsMap.isEmpty()) {
Object res = BeanUtil.mapToBean(sourceAsMap, Object.class, false);
}
}
client.close();
}