如何在弹性搜索中进行多个字段匹配

时间:2016-06-10 20:29:13

标签: php elasticsearch

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

在这里,我想在my_deezer索引中搜索所有具有artist =“blah”和year = 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?]; }]

1 个答案:

答案 0 :(得分:2)

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

{

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

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

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

OR

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