【问题标题】:How to do multiple field match in elastic search如何在弹性搜索中进行多字段匹配
【发布时间】:2016-06-10 20:29:12
【问题描述】:

我一直在搜索弹性搜索文档以进行搜索,但不幸的是我无法理解如何按主题学习弹性搜索主题。

在这里,我想在 my_deezer 索引中搜索所有艺术家 = "blah" 且年份 = 2004 的歌曲。

当我提供多个字段进行匹配时,我正在使用 PHP 客户端及其返回错误。

use Elasticsearch\ClientBuilder;

$client = ClientBuilder::create()->build();

$params = [
    'index' => 'my_deezer',
    'type' => 'song',
    'body' => [
        'query' => [
            'match' => [
                'artist' => 'blah',
                'year' => 2004
            ]   
        ]
    ]
];

try {
   $response = $client->search($params); 
    print_r($response);
} catch (Exception $e) {
    echo $e->getMessage();
}

另外,我正在寻找一些链接,以了解如何执行不同的查询操作,例如搜索字段 x 大于/小于/不等于某个值的位置。

谢谢

// 错误

SearchPhaseExecutionException[Failed to execute phase [query], all shards failed; shardFailures {[hIY53G4ySJm0xQz0Lpqjxg][my_deezer][0]: SearchParseException[[my_deezer][0]: from[-1],size[-1]: Parse Failure [Failed to parse source [{"query":{"match":{"artist":"jal","year":2001}}}]]]; nested: QueryParsingException[[my_deezer] [match] query parsed in simplified form, with direct field name, but included more options than just the field name, possibly use its 'options' form, with 'query' element?]; }{[hIY53G4ySJm0xQz0Lpqjxg][my_deezer][1]: SearchParseException[[my_deezer][1]: from[-1],size[-1]: Parse Failure [Failed to parse source [{"query":{"match":{"artist":"jal","year":2001}}}]]]; nested: QueryParsingException[[my_deezer] [match] query parsed in simplified form, with direct field name, but included more options than just the field name, possibly use its 'options' form, with 'query' element?]; }{[hIY53G4ySJm0xQz0Lpqjxg][my_deezer][2]: SearchParseException[[my_deezer][2]: from[-1],size[-1]: Parse Failure [Failed to parse source [{"query":{"match":{"artist":"jal","year":2001}}}]]]; nested: QueryParsingException[[my_deezer] [match] query parsed in simplified form, with direct field name, but included more options than just the field name, possibly use its 'options' form, with 'query' element?]; }{[hIY53G4ySJm0xQz0Lpqjxg][my_deezer][3]: SearchParseException[[my_deezer][3]: from[-1],size[-1]: Parse Failure [Failed to parse source [{"query":{"match":{"artist":"jal","year":2001}}}]]]; nested: QueryParsingException[[my_deezer] [match] query parsed in simplified form, with direct field name, but included more options than just the field name, possibly use its 'options' form, with 'query' element?]; }{[hIY53G4ySJm0xQz0Lpqjxg][my_deezer][4]: SearchParseException[[my_deezer][4]: from[-1],size[-1]: Parse Failure [Failed to parse source [{"query":{"match":{"artist":"jal","year":2001}}}]]]; nested: QueryParsingException[[my_deezer] [match] query parsed in simplified form, with direct field name, but included more options than just the field name, possibly use its 'options' form, with 'query' element?]; }]

【问题讨论】:

  • 返回什么错误?
  • 有问题更新
  • 尝试添加'query' => [ 'must' => [ 'match => [...注意'must' => [
  • 对于大于/小于尝试'query' => [ 'must' => [ [ 'range' => [ 'year' => [ 'gte' => 2001 ] ] ] ] ],其中gte表示大于/等于并用lte替换小于/等于

标签: php elasticsearch


【解决方案1】:

你有两个条件,你可以使用bool必须查询,查询将是

{

  "query": {
  "bool" : {
    "should" : [
            {
                "term" : { "artist" : "blah" }
            },
            {
                "term" : { "year" : "2004" }
            }
        ]
  }
}

第二部分获取大于小于的结果,有范围查询

 {
    "range" : {
           "age" : { "from" : 10, "to" : 20 }
       }
 }

{
    "range" : {
        "age" : {
            "gte" : 10,
            "lte" : 20
        }
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-08-05
    • 1970-01-01
    • 2020-02-04
    • 2021-01-24
    • 2019-02-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多