ElasticSearch-matchPhraseQuery API以搜索多个字段 [英] ElasticSearch - matchPhraseQuery API to search with multiple fields

查看:1951
本文介绍了ElasticSearch-matchPhraseQuery API以搜索多个字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Am搜索具有 ngram 标记生成器的特定字段,并使用 matchPhraseQuery 查询该字段(代码),并

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

现在,我想搜索3个字段。

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

请找到我的Java代码,它只搜索一个字段(代码)。

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"
      }
    }
  }
 }
}

现在,我想对3个字段进行搜索查询代码,属性,术语

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

我哈ve尝试了下面的Java代码,该代码无法正常工作:

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);

我的输入查询:

Logi

输出变得像:

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

这与我的查询完全无关。预期的结果是 Logiciels

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"
          }
        }
      ]
    }
  }
}

这篇关于ElasticSearch-matchPhraseQuery API以搜索多个字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆