3
votes

I have been searching elastic search documentation for searching but unfortunately I am unable to understand how should i learn elastic search topics by topics.

Here i want to search my_deezer index for all the songs that has artist = "blah" and year = 2004.

I am using PHP client and its returning errors when i am supplying multiple fields to match.

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

Also, I am looking for some links as how can i perform different query actions such as search where field x is greater than / less than / not equals to some value.

Thanks

// Error

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
What errors is it returning?mferly
updated in questionRaheel
Try adding 'query' => [ 'must' => [ 'match => [... note the 'must' => [mferly
For greater/less than try 'query' => [ 'must' => [ [ 'range' => [ 'year' => [ 'gte' => 2001 ] ] ] ] ] where gte means greater than/equal to and replace with lte for less than/equal tomferly

1 Answers

2
votes

You have two condition , you can use bool must query, the query will be

{

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

The second part for getting result greater than less than , there is range query

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

OR

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