Elasticsearch query with multiple attributs and values - php

I try to construct, in php, an query with different attribut:
this following code work :
$searchParams = [
'body' => [
"from"=> 0,
"size"=> 30000,
'query' => [
'filtered'=> [
'filter' => [
'bool' => [
'must' => [
'terms' => [
'field_support' => [105,106,1896,1897]
]
]
]
]
]
]
]
];
But when i add "term" it's not working:
$searchParams = [
'body' => [
"from"=> 0,
"size"=> 30000,
'query' => [
'filtered'=> [
'filter' => [
'bool' => [
'must' => [
'terms' => [
'field_support' => [105,106,1896,1897]
],
'term' => [
'title' => ["le jeu de la dame"]
]
]
]
]
]
]
]
];
I don't understand why it's doesn't works.
Can somebody help me ? Thanks

You need to surround your terms and term query with another associative array, like this:
$searchParams = [
'body' => [
"from"=> 0,
"size"=> 30000,
'query' => [
'filtered'=> [
'filter' => [
'bool' => [
'must' => [
[
'terms' => [
'field_support' => [105,106,1896,1897]
]
],
[
'term' => [
'title' => ["le jeu de la dame"]
]
]
]
]
]
]
]
]
];
UPDATE
Variant with match
$searchParams = [
'body' => [
"from"=> 0,
"size"=> 30000,
'query' => [
'filtered'=> [
'query' => [
'match' => [
'title' => ["le jeu de la dame"]
]
],
'filter' => [
'terms' => [
'field_support' => [105,106,1896,1897]
]
]
]
]
]
];

Related

Why can't I use my analyzer and get answer 'failed to find analyze'?

I made my index with analyzer like in documentation (there).
This is my index create:
$params = [
'index' => 'mytestindex',
'body' => [
'settings' => [
'analysis' => [
'index_analyzer' => [
'my_index_analyzer' => [
'type' => 'custom',
'tokenizer' => 'standard',
'filter' => [
'lowercase',
'mynGram2'
],
],
],
'search_analyzer' => [
'my_search_analyzer' => [
'type' => 'custom',
'tokenizer' => 'standard',
'filter' => [
'standard',
'lowercase',
'mynGram2'
],
],
],
'filter' => [
'mynGram2' => [
'type' => 'nGram',
'min_gram' => 2,
'max_gram' => 20,
],
],
],
'max_ngram_diff' => 50,
],
],
];
$x = $this->obj->indices()->create($params);
Then i try use my analyzer:
$params = [
'index' => 'mytestindex',
'body' => [
'analyzer' => 'my_search_analyzer',
'text' => 'текст проверить чтобы'
],
];
$x = $this->obj->indices()->analyze($params);
But I get this message:
'{"error":{"root_cause":[{"type":"remote_transport_exception","reason":"[PEREGOVOR2][127.0.0.1:9300][indices:admin/analyze[s]]"}],"type":"illegal_argument_exception","reason":"failed
to find analyzer [my_search_analyzer]"},"status":400}'
So... what am I doing wrong? Why can't I use my analyzer and get answer 'failed to find analyze'?
You're not building your analyzer correctly. You only need one analyzer section in your settings:
$params = [
'index' => 'mytestindex',
'body' => [
'settings' => [
'analysis' => [
'analyzer' => [ <--- change this
'my_index_analyzer' => [
'type' => 'custom',
"tokenizer" => "standard",
'filter' => [
"lowercase",
"mynGram2"
],
],
'my_search_analyzer' => [
"type" => "custom",
"tokenizer" => "standard",
'filter' => [
"standard",
"lowercase",
"mynGram2"
],
],
],
'filter' => [
'mynGram2' => [
"type" => "nGram",
"min_gram" => 2,
"max_gram" => 20,
],
],
],
"max_ngram_diff" => "50",
],
],
];
$x = $this->obj->indices()->create($params);

If else statement in elasticsearch query construction

I'm using elasticsearch php and trying to optimize my query contraction in one place. Typical Elasticsearch query like:
$params = [
'index' => 'my_index',
'type' => 'my_type',
'body' => [
'query' => [
'bool' => [
'must' => [
[ 'match' => [ 'testField' => 'abc' ] ],
[ 'match' => [ 'testField2' => 'xyz' ] ],
]
]
]
]
];
So the question is, does it possible to put conditional query in $params before 'match' string maybe like:
<?php if (isset($_GET['query'])) [ 'match' => [ 'testField' => 'abc' ] ]; ?>
Thank you in any advice
You can use this:
<?php
$must = [[ 'match' => [ 'testField2' => 'xyz' ] ] ];
if (isset($_GET['query']))
$must[] = [ 'match' => [ 'testField' => 'abc' ] ];
$params = [
'index' => 'my_index',
'type' => 'my_type',
'body' => [
'query' => [
'bool' => [
'must' => $must
]
]
]
];
or this;
<?php
$params = [
'index' => 'my_index',
'type' => 'my_type',
'body' => [
'query' => [
'bool' => [
'must' => [
[ 'match' => [ 'testField2' => 'xyz' ] ],
],
]
]
]
];
if (isset($_GET['query']))
$params['body']['query']['bool']['must'][] = [ 'match' => [ 'testField' => 'abc' ] ];

Elasticsearch search results filtering

I am new to Elasticsearch and I am using REST API for PHP to play around with data returned. I am using following code to retrieve data.
$params = [
'index' => 'my_search',
'type' => 'mytype',
'from' => 0,
'size' => 10,
'body' => [
'query' => [
'bool' => [
'must' => [
[ 'match' => [ 'validated' => true ] ],
[ 'match' => [ 'image' => true ] ]
]
]
],
'sort' => [
'created_at' => [ 'order' => 'asc']
]
]
];
Above code returns data perfectly matching "validated=>true" and "image=>true".
Further I want to add open text search like we use /_search/?q=Apple macbook. I have tried to use match, multi_match, query_string options, but couldn't get success.
So, I want to retrieve results from ES that have "validated=>true", "image=>true" and matches with text "Apple macbook".
Thanks in advance.
You can try with query_string or simple_query_string
$params = [
'index' => 'my_search',
'type' => 'mytype',
'from' => 0,
'size' => 10,
'body' => [
'query' => [
'bool' => [
'must' => [
[ 'match' => [ 'validated' => true ] ],
[ 'match' => [ 'image' => true ] ],
[ 'query_string' => [ 'query' => 'Apple macbook' ] ]
]
]
],
'sort' => [
'created_at' => [ 'order' => 'asc']
]
]
];
$params = [
'index' => 'my_search',
'type' => 'mytype',
'from' => 0,
'size' => 10,
'body' => [
'query' => [
'bool' => [
'must' => [
[ 'match' => [ 'validated' => true ] ],
[ 'match' => [ 'image' => true ] ],
[ 'simple_query_string' => [ 'query' => 'Apple macbook' ] ]
]
]
],
'sort' => [
'created_at' => [ 'order' => 'asc']
]
]
];
you can also do this by
enabling all_field mapping for your index, you can do that by following the below URL
https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping-all-field.html
and then use the below ES query:
$params = [
'index' => 'my_search',
'type' => 'mytype',
'from' => 0,
'size' => 10,
'body' => [
'query' => [
'bool' => [
'must' => [
[ 'match' => [ '_all' => 'Apple macbook' ] ],
[ 'match' => [ 'validated' => true ] ],
[ 'match' => [ 'image' => true ] ]
]
]
],
'sort' => [
'created_at' => [ 'order' => 'asc']
]
]
];

ElasticSearch 6.2 - aggs return : unknown_named_object_exception

$params2 = [
'index' => 'index',
'type' => "items",
'body' => [
'aggs' => [
"types" => [
"filter" => [
"bool" => [
"should" => [
["term" => ["type_id" => 1]],
["term" => ["type_id" => 2]]
]
]
],
"aggs" => [
"types" =>[
["terms" => ["field" => "type_id","size" => 4]],
"aggs" =>[
"top" => [
["top_hits" => ["size" => 2]]
]
]
]
]
]
],
]
];
when i pass this params to $elastic->search($params2);
its return me this exception
{"error":{"root_cause":[{"type":"unknown_named_object_exception","reason":"Unknown BaseAggregationBuilder [0]","line":1,"col":117}],"type":"unknown_named_object_exception","reason":"Unknown BaseAggregationBuilder [0]","line":1,"col":117},"status":400}
i am using ErickTamayo/laravel-scout-elastic package
You need to remove the square brackets around terms and top_hits
$params2 = [
'index' => 'index',
'type' => "items",
'body' => [
'aggs' => [
"types" => [
"filter" => [
"bool" => [
"should" => [
["term" => ["type_id" => 1]],
["term" => ["type_id" => 2]]
]
]
],
"aggs" => [
"types" =>[
"terms" => ["field" => "type_id","size" => 4],
"aggs" =>[
"top" => [
"top_hits" => ["size" => 2]
]
]
]
]
]
],
]
];

Querying Elasticsearch with PHP

I'm having a little trouble translating some of the queries I use for elasticsearch into PHP readable queries.
For example this simple query works:
$query = $elastic->search([
body' => [
'query' => [
'match' => [
'myfield' => 'mymatchingresult'
]
]
]
]);
But what I'm trying to get to work follow below. There isn't an error, it just doesn't run. I must not be understanding the structure. The same query if placed in something like google extension sense seems to work. (With the php '=>' converted to ':' etc.)
$query = $elastic->search([
'body' => [
'query' => [
'filtered' => [
'query' => [
'query_string' => [
'query' => '*',
'analyze_wildcard' => 'true'
]
],
'filter' => [
'bool' => [
'must' => [
'query' => [
'query_string' => [
'analyze_wildcard' => 'true',
'query' => 'cn:name'
]
],
'range' => [
'#timestamp' => [
'from' => '2012-05-01',
'to' => '2016-05-01'
]
]
]
]
]
]
]
]
]);
Thank you for the help!
-John
As far as I can tell, the constraints in your bool/must filter must be enclosed in square brackets, i.e. bool/must should be a pure array, not an associative array.
Like this:
$query = $elastic->search([
'body' => [
'query' => [
'filtered' => [
'query' => [
'query_string' => [
'query' => '*',
'analyze_wildcard' => 'true'
]
],
'filter' => [
'bool' => [
'must' => [
[
'query' => [
'query_string' => [
'analyze_wildcard' => 'true',
'query' => 'cn:name'
]
]
],
[
'range' => [
'#timestamp' => [
'from' => '2012-05-01',
'to' => '2016-05-01'
]
]
]
]
]
]
]
]
]
]);

Categories