ElasticSearch - matchPhraseQuery API to search wit

2019-09-03 14:16发布

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.

1条回答
倾城 Initia
2楼-- · 2019-09-03 15:02

Use bool query :

GET products/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match_phrase": {
            "FIELD": "PHRASE"
          }
        },
        {
          "match_phrase": {
            "FIELD": "PHRASE"
          }
        },
        {
          "match_phrase": {
            "FIELD": "PHRASE"
          }
        }
      ]
    }
  }
}
查看更多
登录 后发表回答