java 多字段搜索,ElasticSearch-matchPhraseQuery API以搜索多个字段

Am searching for specific field which is having ngram tokenizer and am querying that field (code) using matchPhraseQuery and it is working fine.

Now, i want to search with 3 field. How can we do this.?

Please find my java code which am searching for only one field (code).

SearchRequest searchRequest = new SearchRequest(INDEX);

searchRequest.types(TYPE);

SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();

QueryBuilder qb = QueryBuilders.matchPhraseQuery("code", code);

searchSourceBuilder.query(qb);

searchSourceBuilder.size(10);

searchRequest.source(searchSourceBuilder);

Please find my mappings details below :

PUT products

{

"settings": {

"analysis": {

"analyzer": {

"custom_analyzer": {

"type": "custom",

"tokenizer": "ngram",

"char_filter": [

"html_strip"

],

"filter": [

"lowercase",

"asciifolding"

]

}

}

}

},

"mappings": {

"doc": {

"properties": {

"code": {

"type": "text",

"analyzer": "custom_analyzer"

},

"attribute" : {

"type" : "text",

"analyzer" : "custom_analyzer"

},

"term" : {

"type" : "text",

"analyzer" : "custom_analyzer"

}

}

}

}

}

Now, i want to make a search query for 3 fields code, attribute, term

I have tried the below java code, which is not working as expected :

BoolQueryBuilder orQuery = QueryBuilders.boolQuery();

QueryBuilder qb1 = QueryBuilders.matchPhraseQuery("catalog_keywords", keyword);

QueryBuilder qb2 = QueryBuilders.matchPhraseQuery("product_keywords", keyword);

orQuery.should(qb1);

orQuery.should(qb2);

orQuery.minimumShouldMatch(1);

searchSourceBuilder.query(orQuery);

searchSourceBuilder.size(10);

searchRequest.source(searchSourceBuilder);

My Input Query :

Logi

Output am getting like :

"Materiały, programy doborowe | Marketing | Katalogi, broszury"

Which is totally irrelavant to my query. Expected results is, Logiciels

And my field having value with delimiters |, so i just want only the exact match of the word/character. It should not print with all the delimiters and all.

解决方案

GET products/_search

{

"query": {

"bool": {

"must": [

{

"match_phrase": {

"FIELD": "PHRASE"

}

},

{

"match_phrase": {

"FIELD": "PHRASE"

}

},

{

"match_phrase": {

"FIELD": "PHRASE"

}

}

]

}

}

}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值