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!!
CodePudding user response:
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.
CodePudding user response:
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": []}},
]
}
}
}
}
}