I'm trying to have multiple queries in ELASTIC SEARCH However I just want to have an operator AND to separate them as condition, for example as in SQL,
SELECT * FROM table where P=1 AND Q=2
In such query, we find only those results which will fulfil both conditions. In my case, P=1 is my first query and Q=2 is my second one.
How can I get the results for such query. Im using PHP.
Here's my Code:
// MY QUERY 1
$query["bool"]["must"][]["multi_match"] = array(
"query"=>$string,
"operator" => "and",
"fields" => array("firstname", "lastname")
);
// MY QUERY 2
$query["bool"]["must"][]["multi_match"] = array(
'query' =>$string2,
'fields' => array("firstname1", "firstname2")
);
// SEARCH
$return = $client->search(
array(
'index' => 'my_index',
'type' => 'typ',
'body' => array(
'query' => $query,
'size' => 100,
),
)
);
You can use a filter query with an and operator like this. Try it out.
{
"filter": {
"and": [
{
"query": {
"bool": {
"must": [
{
"multi_match": {
"query": "searchTerm1",
"fields": [
"firstname",
"lastname"
]
}
}
]
}
}
},
{
"query": {
"bool": {
"must": [
{
"multi_match": {
"query": "searchTerm2",
"fields": [
"firstname1",
"firstname2"
]
}
}
]
}
}
}
]
}
}
Related
I'm new to ElasticSearch and don't really understand how the queries works...
My indexing example
{
"_index" : "indexing001",
"_type" : "_doc",
"_id" : "3",
"_version" : 1,
"_seq_no" : 242,
"_primary_term" : 2,
"found" : true,
"_source" : {
"type" : 1,
"sub_type" : null,
"user" : {
"id" : 1,
"name" : "tk6z2 gcnouvqmr"
},
"editor_user" : [ ],
"content" : [
{
"title" : "Title #3",
"short_text" : "Article #3 short text",
"full_text" : "Article #3 full text",
"locale" : "de-DE"
}
],
"flags" : [ ],
"location" : [ ],
"start_date" : 1658793600,
"end_date" : 1658793600,
"_users" : [ ]
}
}
I want to query the text to match the field content.title and content.short_text, query user by _users field.
For example my function is:
public static function search(
string $text = '',
int $user = 0
): array
{
try {
$model = new someModelClass();
$fields = [
'content.title',
'content.short_text',
];
$result = $model::find()->query( [
'bool' => [
'should' => [
'multi_match' => [
'query' => $text,
'fields' => $fields,
],
],
'filter' => [
[ 'term' => [ '_users.id' => $user ] ],
[ 'term' => [ '_users' => [] ] ],
]
],
] )->all();
return $result;
}
catch ( Exception $e ) {
throw new Exception( $e->getMessage() );
}
}
convert it to SQL it should be something like: SELECT * FROM 'indexing001' WHERE (content.title LIKE %search% OR content.short_text LIKE %search%) AND (users.id = 1 OR users = '')
How to write it in ElasticSearch query?
Thanks in advance!!
Well in that case i would recommend to use the Elasticsearch-PHP client.
Please install appropriate client using composer.
For a match query like below
curl -XGET 'localhost:9200/my_index/_search' -d '{
"query" : {
"match" : {
"testField" : "abc"
}
}
}'
You can make a query in your PHP Script like this
$params = [
'index' => 'my_index',
'body' => [
'query' => [
'match' => [
'testField' => 'abc'
]
]
]
];
$results = $client->search($params);
Check more operations here.
I'll try to reproduce your SQL and provide JSON query example, u will ez adapt it to PHP query method.
{
"query": {
"bool": {
"should": [
{"multi_match": {
"fields": ["content.title", "content.short_text"],
"query": "%query here%"
}}
],
"filter": {
"bool": {
"should": [
{"match": {"user.id": "%id here%"}},
{"match": {"user": []}},
]
}
}
}
}
}
I am very new to the elasticsearch and I need to prepare the query which combines OR, AND and IN operators.
What I want to achieve is having something like this in SQL:
SELECT * FROM tableWHERE (field_1 = 'foo' AND field_2 IN(1,2,3) ) OR ('field_1 = 'bar' AND field_2 IN(2, 3, 4) );
I am using elastic with PHP and have started with something like this:
'query' => [
'bool' => [
'should' => [
[
'match' => [
'field_1' => 'foo',
],
],
[
'match' => [
'field_1' => 'bar',
],
],
],
],
],
However, I cannot add query parameters to achive my desired result. Can you please help me with that?
You need to combine bool/must/should query clauses. For IN operator you can use terms query in elasticsearch
Try out this below query
{
"query": {
"bool": {
"should": [
{
"bool": {
"must": [
{
"match": {
"field_1": "foo"
}
},
{
"terms": {
"field_2": [
1,
2,
3
]
}
}
]
}
},
{
"bool": {
"must": [
{
"match": {
"field_1": "bar"
}
},
{
"terms": {
"field_2": [
2,
3,
4
]
}
}
]
}
}
]
}
}
}
I have the following mapping:
'contract' => [
'type' => 'nested',
'include_in_parent' => true,
'properties' => [
'started_at' => ['type' => 'date', 'format' => 'yyyy-MM-dd'],
'ended_at' => ['type' => 'date', 'format' => 'yyyy-MM-dd'],
],
],
I'm trying to write a query that searches for all active contracts; a contract is active when now is between started_at and anded_at, or started_at < now if the contract is undetermined (ended_at is null).
my try was the following, but still doesn't handle both the cases:
'bool' => [
'must' => [
['range' => ['applications.contract.started_at' => ['lte' => 'now']]],
['range' => ['applications.contract.ended_at' => ['gte' => 'now']]],
],
],
Any hint would be awesome, thanks folks!
When you have "nullable" field as "ended_at" you need to combine "range" and "must_not" into "should" because elasticsearch does not index NULL value (as a result it's a missing field)
So your query will look something like this:
{
"query": {
"bool": {
"must": [
{
"range": {"applications.contract.started_at": {"lte": "now"}}
},
"should": [
{
"range": {
"applications.contract.ended_at": {"gte": "now"}
}
},
{
"bool": {
"must_not": {
"exists": "applications.contract.ended_at"
}
}
}
]
}
}
}
also, I've noticed that you are using "nested" documents so the query above must be wrapped into a nested query
{
"bool": {
"must": {
"nested": {
"path": "contract",
"query": {
"bool": {
"must": [
{
"range": {"applications.contract.started_at": {"lte": "now"}}
}
],
"should": [
{
"range": {
"applications.contract.ended_at": {"gte": "now"}
}
},
{
"bool": {
"must_not": {
"exists": {"field": "applications.contract.ended_at"}
}
}
}
]
}
}
}
}
}
}
I am trying to create an elasticsearch query and for that I need to get my array in a particular shape but I am not able to get it.
desired output
"query": {
"bool": {
"must": {
"bool": {
"must": [
"match": {
"active": 1
},
"bool": {
"should": [
{
"match": {
"workspace_type.keyword": "lounge"
}
},
{
"match": {
"workspace_type.keyword": " cafe"
}
}
]
}
]
}
}
}
}
My source code
$query = [
'query' => [
'bool' => [
'must' => [
'bool' => [
'must' => []
]
]
]
]
];
$active_query = [
'match' => [
"active" => 1
]
];
$query['query']['bool']['must']['bool']['must'][] = $active_query;
$workspace_type_query = [
'match' => [
"workspace_type.keyword" => "lounge"
]
];
$workspace_type_object = (object) $workspace_type_query;
$query['query']['bool']['must']['bool']['must']['bool']['should'][] = $workspace_type_object;
only issue with this is I am not able to get must[] (the one above match and active ) and it was either coming as an indexed object like
{
"0": {
"match": {
"active": 1
}
}
}
or just an object like this
{
"match": {
"active": 1
}
}
but both these cases are not acceptable for the elasticsearch query that I am trying to generate .. any assistance would be highly appreciated.
I think the extra index that you create for must is wrong.
When you change it to
$query['query']['bool']['must']['bool']['must'] = $active_query;
it seems to work.
The result is
{
"query": {
"bool": {
"must": {
"bool": {
"must": {
"match": {
"active": 1
}
}
}
}
}
}
}
I'm trying to use a PHP mongo library to "aggregate" on a data structure like this:
{
"_id": 100,
"name": "Joe",
"pets":[
{
"name": "Kill me",
"animal": "Frog"
},
{
"name": "Petrov",
"animal": "Cat"
},
{
"name": "Joe",
"animal": "Frog"
}
]
},
{
"_id": 101,
"name": "Jane",
"pets":[
{
"name": "James",
"animal": "Hedgehog"
},
{
"name": "Franklin",
"animal": "Frog"
}
}
For example, if I want to get all subdocuments where the animal is a frog. Note that I do NOT want all matching "super-documents" (i.e. the ones with _id). I want to get an ARRAY that looks like this:
[
{
"name": "Kill me",
"animal": "Frog"
},
{
"name": "Joe",
"animal": "Frog"
},
{
"name": "Franklin",
"animal": "Frog"
}
]
What syntax am I supposed to use (in PHP) to accomplish this? I know it has to do with aggregate, but I couldn't find anything that matches this specific scenario.
You can use below aggregation. $match to find documents where array has a value of Frog and $unwind the pets array. $match where document has Frog and final step is to group the matching documents into array.
<?php
$mongo = new MongoDB\Driver\Manager("mongodb://localhost:27017");
$pipeline =
[
[
'$match' =>
[
'pets.animal' => 'Frog',
],
],
[
'$unwind' =>'$pets',
],
[
'$match' =>
[
'pets.animal' => 'Frog',
],
],
[
'$group' =>
[
'_id' => null,
'animals' => ['$push' => '$pets'],
],
],
];
$command = new \MongoDB\Driver\Command([
'aggregate' => 'insert_collection_name',
'pipeline' => $pipeline
]);
$cursor = $mongo->executeCommand('insert_db_name', $command);
foreach($cursor as $key => $document) {
//do something
}
?>