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": []}},
]
}
}
}
}
}
Hello Good Developers,
I am using jenssegers/laravel-mongodb package to query my MongoDB from Laravel.
Here's Fiddle for my query: https://mongoplayground.net/p/qzbNN8Siy-3
I have following JSON
[{
"id": "GLOBAL_EDUCATION",
"general_name": "GLOBAL_EDUCATION",
"display_name": "GLOBAL_EDUCATION",
"profile_section_id": 0,
"translated": [
{
"con_lang": "US-EN",
"country_code": "US",
"language_code": "EN",
"text": "What is the highest level of education you have completed?",
"hint": null
},
{
"con_lang": "US-ES",
"country_code": "US",
"language_code": "ES",
"text": "\u00bfCu\u00e1l es su nivel de educaci\u00f3n?",
"hint": null
}...
{
....
}
]
I am trying to run following command
db.collection.find({ 'id': "GLOBAL_EDUCATION" },{_id:0, id:1, general_name:1, translated:{ $elemMatch: {con_lang: "US-EN"} }})
Expecting result like this
[
{
"general_name": "GLOBAL_EDUCATION",
"id": "GLOBAL_EDUCATION",
"translated": [
{
"con_lang": "US-EN",
"country_code": "US",
"hint": null,
"language_code": "EN",
"text": "What is the highest level of education you have completed?"
}
]
}
]
Everything is fine while query directly in MoDB but issue arise when I am trying this in Laravel.
I've tried every possible known function from MongoDB package. but Not able to do this.
here's my Array
$findArray = [
[
'id' => "GLOBAL_EDUCATION",
],
[
'_id' => 0,
'id' => 1,
'general_name' => 1,
'translated' => [
'$elemMatch' => ['con_lang' => "US-EN"]
],
]
];
$model = GlobalQuestions::raw()->find($findArray) //OR
$data = GlobalQuestions::raw(function($collection) use ($findArray){
return $collection->find($findArray);
});
What I am doing wrong here, is this kind of Find() not possible here and I've to do this by aggregation?
Since no-one answered this, I am posting the solution if someone is having the same issue.
Doing some more R&D on the same I was able to do this using where and Project as well by Aggregation Pipelines.
----- Using Where() and Project() ------
$projectArray = [
'_id' => 0,
'id' => 1,
'general_name' => 1,
'translated' => [
'$elemMatch' => ['con_lang' => "FR-FR"]
],
];
$data = GlobalQuestions::where('id', '=', 'GLOBAL_EDUCATION')
->project($projectArray)
->get();
--- Using Aggregation and $unwind ---
$data = GlobalQuestions::raw(function($collection) {
return $collection->aggregate([
[
'$match' => [
'id' => "GLOBAL_EDUCATION"
]
],
[
'$unwind' => '$translated',
],
[
'$match' => [
'translated.con_lang' => "US-EN"
]
],
[
'$project' => [
'_id'=> 0,
'id'=> 1,
'general_name' => 1,
'translated' => 1,
]
]
]);
})->first();
I am needing to ignore the apostrophe with indexed results so that searching for "Johns potato" will show results for "John's potato"
I was able to get the analyzer accepted but now I return no search results. Does anyone see something obvious that I am missing?
$params = [
'index' => $index,
'body' => [
'settings' => [
'number_of_shards' => 5,
'number_of_replicas' => 2,
'analysis' => [
"analyzer" => [
"my_analyzer" => [
"tokenizer" => "keyword",
"char_filter" => [
"my_char_filter"
]
]
],
"char_filter" => [
"my_char_filter" => [
"type" => "mapping",
"mappings" => [
"' => "
]
]
]
]
],
'mappings' => [
$type => [
'_source' => [
'enabled' => true
],
'properties' => [
'title' => [
'type' => 'text',
'analyzer' => 'my_analyzer'
],
'content' => [
'type' => 'text',
'analyzer' => 'my_analyzer'
]
]
]
]
]
];
I did find out that removing the analyzer from my field mappings allowed results to reappear, but I get no results the second I add the analyzer.
Here's an example query that I make.
{
"body": {
"query": {
"bool": {
"must": {
"multi_match": {
"query": "apples",
"fields": [
"title",
"content"
]
}
},
"filter": {
"terms": {
"site_id": [
"1351",
"1349"
]
}
},
"must_not": [
{
"match": {
"visible": "false"
}
},
{
"match": {
"locked": "true"
}
}
]
}
}
}
}
Probably, what you really want, is to use the english analyzer that is provided. The standard analyzer which is the default will tokenize on whitespace and some punctuation, but will leave apostrophes alone. The english analyzer can stem and remove stop words since the language is known.
Here is the standard analyzer's output, where you can see "john's":
POST _analyze
{
"analyzer": "standard",
"text": "John's potato"
}
{
"tokens": [
{
"token": "john's",
"start_offset": 0,
"end_offset": 6,
"type": "<ALPHANUM>",
"position": 0
},
{
"token": "potato",
"start_offset": 7,
"end_offset": 13,
"type": "<ALPHANUM>",
"position": 1
}
]
}
And here is the english analyzer where you can see the 's is removed. The stemming will allow "John's", "Johns", and "John" to all match the document.
POST _analyze
{
"analyzer": "english",
"text": "John's potato"
}
{
"tokens": [
{
"token": "john",
"start_offset": 0,
"end_offset": 6,
"type": "<ALPHANUM>",
"position": 0
},
{
"token": "potato",
"start_offset": 7,
"end_offset": 13,
"type": "<ALPHANUM>",
"position": 1
}
]
}
How do I insert and extract the following from a MySQL database using php. I have tried 'Multiploygon, Multilinestring, GeometryCollection' but cannot get the correct output as below.
1) JSON file to insert
{
"type": "FeatureCollection",
"features": [
{ "type": "Feature", "properties": { "GID": 4728339, "PRCL_KEY": "0000T0JT005300000056000010", "PRCL_TYPE": "E", "LSTATUS": "R", "WSTATUS": "C", "GEOM_AREA": 1558.723715, "COMMENTS": "", "TAG_X": 30.920674, "TAG_Y": -25.452585, "TAG_VALUE": "1\/56", "TAG_SIZE": 0.000020, "TAG_ANGLE": 6.282645, "TAG_JUST": "MC", "ID": "T0JT00530000005600001", "DATE_STAMP": "2010\/10\/05" }, "geometry": { "type": "Polygon", "coordinates": [ [ [ 30.920838, -25.452746 ], [ 30.920876, -25.452431 ], [ 30.920876, -25.452431 ], [ 30.921498, -25.452367 ], [ 30.921498, -25.452367 ], [ 30.921492, -25.452341 ], [ 30.921492, -25.452341 ], [ 30.920476, -25.452445 ], [ 30.920476, -25.452445 ], [ 30.920441, -25.452719 ], [ 30.920441, -25.452719 ], [ 30.920838, -25.452746 ], [ 30.920838, -25.452746 ] ] ] } }
,
{ "type": "Feature", "properties": { "GID": 4822420, "PRCL_KEY": "0000T0JU004000004445000000", "PRCL_TYPE": "E", "LSTATUS": "R", "WSTATUS": "C", "GEOM_AREA": 391.799538, "COMMENTS": "", "TAG_X": 31.337096, "TAG_Y": -25.508292, "TAG_VALUE": "4445", "TAG_SIZE": 0.000020, "TAG_ANGLE": 0.002299, "TAG_JUST": "MC", "ID": "T0JU00400000444500000", "DATE_STAMP": "2013\/05\/22" }, "geometry": { "type": "Polygon", "coordinates": [ [ [ 31.336972, -25.508217 ], [ 31.337103, -25.508419 ], [ 31.337103, -25.508419 ], [ 31.337234, -25.508350 ], [ 31.337234, -25.508350 ], [ 31.337101, -25.508149 ], [ 31.337101, -25.508149 ], [ 31.336972, -25.508217 ], [ 31.336972, -25.508217 ] ] ] } }
2) Output needed
{"type":"FeatureCollection","features":[{"type":"Feature","geometry":{"properties":{"name":"T0IS00210000007100000"},"type":"Polyline","coordinates":[29.245262,-26.95622,29.245272,-26.956412,29.245272,-26.956412,29.245282,-26.956603,29.245282,-26.956603,29.245826,-26.956581,29.245826,-26.956581,29.246128,-26.956398,29.246128,-26.956398]}},{"type":"Feature","geometry":{"properties":{"name":"T0IS00210000007300000"},"type":"Polyline","coordinates":[29.245211,-26.955206,29.245231,-26.955589,29.245231,-26.955589,29.246086,-26.955554,29.246086,-26.955554,29.246066,-26.95517,29.246066,-26.95517,29.245211,-26.955206,29.245211,-26.955206]}},{"type":"Feature","geometry":{"properties":{"name":"T0IS00210000007500000"},"type":"Polyline","coordinates":[29.245171,-26.954439,29.245191,-26.954822,29.245191,-26.954822,29.246047,-26.954787,29.246047,-26.954787,29.246037,-26.954594,29.246037,-26.954594,29.245558,-26.954614,29.245558,-26.954614]}},{"type":"Feature","geometry":{"properties":{"name":"T0IS00210000010200000"},"type":"Polyline","coordinates":[29.2436,-26.954444,29.243605,-26.95454,29.243605,-26.95454,29.243623,-26.954887,29.243623,-26.954887,29.244051,-26.95487,29.244051,-26.95487,29.244028,-26.954427,29.244028,-26.954427]}},{"type":"Feature","geometry":{"properties":{"name":"T0IS00210000030400000"},"type":"Polyline","coordinates":[29.242942,-26.949925,29.242963,-26.950314,29.242963,-26.950314,29.243388,-26.950298,29.243388,-26.950298,29.243374,-26.950036,29.243374,-26.950036,29.243367,-26.949915,29.243367,-26.949915]}},{"type":"Feature","geometry":{"properties":{"name":"T0IS00210000010400001"},"type":"Polyline","coordinates":[29.243429,-26.951122,29.243439,-26.951314,29.243439,-26.951314,29.243867,-26.951296,29.243867,-26.951296,29.243857,-26.951105,29.243857,-26.951105,29.243477,-26.95112,29.243477,-26.95112]}},{"type":"Feature","geometry":{"properties":{"name":"T0IS00210000010500000"},"type":"Polyline","coordinates":[29.243288,-26.956694,29.243309,-26.957098,29.243309,-26.957098,29.243328,-26.95746,29.243328,-26.95746,29.243755,-26.957443,29.243755,-26.957443,29.243716,-26.956676,29.243716,-26.956676]}},{"type":"Feature","geometry":{"properties":{"name":"T0IS00210000010600000"},"type":"Polyline","coordinates":[29.243261,-26.956168,29.243288,-26.956694,29.243288,-26.956694,29.243716,-26.956676,29.243716,-26.956676,29.243697,-26.956312,29.243697,-26.956312,29.243688,-26.956151,29.243688,-26.956151]}},{"type":"Feature","geometry":{"properties":{"name":"T0IS00210000010800000"},"type":"Polyline","coordinates":[29.243178,-26.954558,29.243195,-26.954905,29.243195,-26.954905,29.243304,-26.954901,29.243304,-26.954901,29.243623,-26.954887,29.243623,-26.954887,29.243605,-26.95454,29.243605,-26.95454]}},{"type":"Feature","geometry":{"properties":{"name":"T0IS00210000009400000"},"type":"Polyline","coordinates":[29.244647,-26.952287,29.244653,-26.952395,29.244653,-26.952395,29.244682,-26.952394,29.244682,-26.952394,29.244697,-26.952669,29.244697,-26.952669,29.245078,-26.952654,29.245078,-26.952654]}},{"type":"Feature","geometry":{"properties":{"name":"T0IS00210000009500000"},"type":"Polyline","coordinates":[29.244599,-26.95165,29.244609,-26.951846,29.244609,-26.951846,29.244652,-26.951844,29.244652,-26.951844,29.244661,-26.952032,29.244661,-26.952032,29.244825,-26.952025,29.244825,-26.952025]}},{"type":"Feature","geometry":{"properties":{"name":"T0IS00210000009800000"},"type":"Polyline","coordinates":[29.244753,-26.950491,29.244773,-26.950875,29.244773,-26.950875,29.244987,-26.950866,29.244987,-26.950866,29.244967,-26.950483,29.244967,-26.950483,29.244753,-26.950491,29.244753,-26.950491]}}
3) Current PHP code
$geojson = array( 'type' => 'FeatureCollection', 'features' => array() );
while($row = mysql_fetch_assoc($dbquery)) {
$feature = array(
'type' => 'Feature',
'geometry' => array(
'properties' => array(
'name' => $row['LINK_ID']),
'type' => 'Polyline',
'coordinates' => array((float)$row['lon1'],(float)$row['lat1'],(float)$row['lon2'],(float)$row['lat2'],(float)$row['lon3'],(float)$row['lat3'],(float)$row['lon4'],(float)$row['lat4'],(float)$row['lon5'],(float)$row['lat5'],(float)$row['lon6'],(float)$row['lat6'],(float)$row['lon7'],(float)$row['lat7'],(float)$row['lon8'],(float)$row['lat8'],(float)$row['lon9'],(float)$row['lat9'])
)
);
// array_push($geojson, $feature);
array_push($geojson['features'], $feature);
Thank you
Unfortunately, MySQL does not have a native ST_AsGeoJSON function like PostGIS. Fortunately, you can use geoPHP in combination with a little script I wrote to achieve the same result.
Export your GeoJSON file to a MySQL spatial table using OGRor QGIS.
Download geoPHP and my MySQL to GeoJSON script.
Fill in the proper db connection settings and this script should output your MySQL spatial table in proper GeoJSON format.