Elasticsearch syntax error with multi fields search












9















I watched Elasticsearch Do's, Don'ts and Pro-Tips by Itamar Syn Hershko




https://www.youtube.com/watch?v=c9O5_a50aOQ




I see multiple conditions in several fields in the following image:




https://imgur.com/a/17zAZ4w




I tried to make it in
my Laravel 5.7 app (with the elasticsearch/elasticsearch plugin) as seen in the following code:



$elasticQuery = [
"bool" => [
'must' => [
'multi_match' => [
'query' => $text,
'fields' => ['name^4', 'description']
],
],
"should" => [
'term' => [
"category_id" => 1,
]
]
]
];


but I got the error :



{"error":{"root_cause":[{"type":"parsing_exception","reason":"[bool] malformed query, expected [END_OBJECT] but found [FIELD_NAME]","line":1,"col":130}],"type":"parsing_exception","reason":"[bool] malformed query, expected [END_OBJECT] but found [FIELD_NAME]","line":1,"col":130},"status":400}


But when I use a simple condition:



        $elasticQuery = [
'multi_match' => [
'query' => $text,
'fields' => ['name^4', 'description'],
],
];


I got a valid result:



[hits] => Array
(
[total] => 1
[max_score] => 7.4126062
[hits] => Array
(
[0] => Array
(
[_index] => select_vote
[_type] => vote
[_id] => 16
[_score] => 7.4126062
[_source] => Array
(
[id] => 16
[slug] => in-the-film-babe-what-type-of-animal-was-babe
[name] => In the film Babe, what type of animal was Babe?
[description] => Babe is a 1995 A...
[created_at] => 2018-11-10 09:14:15
[category_id] => 2
[category] => Array
(
[name] => Movie&Cartoons
[slug] => movie-cartoons
[created_at] => 2018-11-10 09:14:12
)

)

)

)

)


is this a valid format for multi-request?



MODIFIED BLOCK # 2:
Making some search I found work:



$elasticQuery = [
"bool" => [
'should' => [


[
"multi_match" => [
"query" => $text,
"type" => "cross_fields",
"fields" => [
"name^4",
"description"
]
]
],

[
'match' => [
'category_id' => [
'query' => 1,
]
]
],

[
'match' => [
'category_id' => [
'query' => 3,
]
]
],

]
]
];


when I need to make a search by text fields and the array of category (1 and 3) in the example above it works but looks like it works like an "OR" condition, but I need to make a restriction like "AND" using SQL terminology...



Which way is correct in order to make a restriction as like "AND"?



Thanks!










share|improve this question

























  • Please, look at MODIFIED BLOCK # 2

    – mstdmstd
    Nov 21 '18 at 8:54


















9















I watched Elasticsearch Do's, Don'ts and Pro-Tips by Itamar Syn Hershko




https://www.youtube.com/watch?v=c9O5_a50aOQ




I see multiple conditions in several fields in the following image:




https://imgur.com/a/17zAZ4w




I tried to make it in
my Laravel 5.7 app (with the elasticsearch/elasticsearch plugin) as seen in the following code:



$elasticQuery = [
"bool" => [
'must' => [
'multi_match' => [
'query' => $text,
'fields' => ['name^4', 'description']
],
],
"should" => [
'term' => [
"category_id" => 1,
]
]
]
];


but I got the error :



{"error":{"root_cause":[{"type":"parsing_exception","reason":"[bool] malformed query, expected [END_OBJECT] but found [FIELD_NAME]","line":1,"col":130}],"type":"parsing_exception","reason":"[bool] malformed query, expected [END_OBJECT] but found [FIELD_NAME]","line":1,"col":130},"status":400}


But when I use a simple condition:



        $elasticQuery = [
'multi_match' => [
'query' => $text,
'fields' => ['name^4', 'description'],
],
];


I got a valid result:



[hits] => Array
(
[total] => 1
[max_score] => 7.4126062
[hits] => Array
(
[0] => Array
(
[_index] => select_vote
[_type] => vote
[_id] => 16
[_score] => 7.4126062
[_source] => Array
(
[id] => 16
[slug] => in-the-film-babe-what-type-of-animal-was-babe
[name] => In the film Babe, what type of animal was Babe?
[description] => Babe is a 1995 A...
[created_at] => 2018-11-10 09:14:15
[category_id] => 2
[category] => Array
(
[name] => Movie&Cartoons
[slug] => movie-cartoons
[created_at] => 2018-11-10 09:14:12
)

)

)

)

)


is this a valid format for multi-request?



MODIFIED BLOCK # 2:
Making some search I found work:



$elasticQuery = [
"bool" => [
'should' => [


[
"multi_match" => [
"query" => $text,
"type" => "cross_fields",
"fields" => [
"name^4",
"description"
]
]
],

[
'match' => [
'category_id' => [
'query' => 1,
]
]
],

[
'match' => [
'category_id' => [
'query' => 3,
]
]
],

]
]
];


when I need to make a search by text fields and the array of category (1 and 3) in the example above it works but looks like it works like an "OR" condition, but I need to make a restriction like "AND" using SQL terminology...



Which way is correct in order to make a restriction as like "AND"?



Thanks!










share|improve this question

























  • Please, look at MODIFIED BLOCK # 2

    – mstdmstd
    Nov 21 '18 at 8:54
















9












9








9








I watched Elasticsearch Do's, Don'ts and Pro-Tips by Itamar Syn Hershko




https://www.youtube.com/watch?v=c9O5_a50aOQ




I see multiple conditions in several fields in the following image:




https://imgur.com/a/17zAZ4w




I tried to make it in
my Laravel 5.7 app (with the elasticsearch/elasticsearch plugin) as seen in the following code:



$elasticQuery = [
"bool" => [
'must' => [
'multi_match' => [
'query' => $text,
'fields' => ['name^4', 'description']
],
],
"should" => [
'term' => [
"category_id" => 1,
]
]
]
];


but I got the error :



{"error":{"root_cause":[{"type":"parsing_exception","reason":"[bool] malformed query, expected [END_OBJECT] but found [FIELD_NAME]","line":1,"col":130}],"type":"parsing_exception","reason":"[bool] malformed query, expected [END_OBJECT] but found [FIELD_NAME]","line":1,"col":130},"status":400}


But when I use a simple condition:



        $elasticQuery = [
'multi_match' => [
'query' => $text,
'fields' => ['name^4', 'description'],
],
];


I got a valid result:



[hits] => Array
(
[total] => 1
[max_score] => 7.4126062
[hits] => Array
(
[0] => Array
(
[_index] => select_vote
[_type] => vote
[_id] => 16
[_score] => 7.4126062
[_source] => Array
(
[id] => 16
[slug] => in-the-film-babe-what-type-of-animal-was-babe
[name] => In the film Babe, what type of animal was Babe?
[description] => Babe is a 1995 A...
[created_at] => 2018-11-10 09:14:15
[category_id] => 2
[category] => Array
(
[name] => Movie&Cartoons
[slug] => movie-cartoons
[created_at] => 2018-11-10 09:14:12
)

)

)

)

)


is this a valid format for multi-request?



MODIFIED BLOCK # 2:
Making some search I found work:



$elasticQuery = [
"bool" => [
'should' => [


[
"multi_match" => [
"query" => $text,
"type" => "cross_fields",
"fields" => [
"name^4",
"description"
]
]
],

[
'match' => [
'category_id' => [
'query' => 1,
]
]
],

[
'match' => [
'category_id' => [
'query' => 3,
]
]
],

]
]
];


when I need to make a search by text fields and the array of category (1 and 3) in the example above it works but looks like it works like an "OR" condition, but I need to make a restriction like "AND" using SQL terminology...



Which way is correct in order to make a restriction as like "AND"?



Thanks!










share|improve this question
















I watched Elasticsearch Do's, Don'ts and Pro-Tips by Itamar Syn Hershko




https://www.youtube.com/watch?v=c9O5_a50aOQ




I see multiple conditions in several fields in the following image:




https://imgur.com/a/17zAZ4w




I tried to make it in
my Laravel 5.7 app (with the elasticsearch/elasticsearch plugin) as seen in the following code:



$elasticQuery = [
"bool" => [
'must' => [
'multi_match' => [
'query' => $text,
'fields' => ['name^4', 'description']
],
],
"should" => [
'term' => [
"category_id" => 1,
]
]
]
];


but I got the error :



{"error":{"root_cause":[{"type":"parsing_exception","reason":"[bool] malformed query, expected [END_OBJECT] but found [FIELD_NAME]","line":1,"col":130}],"type":"parsing_exception","reason":"[bool] malformed query, expected [END_OBJECT] but found [FIELD_NAME]","line":1,"col":130},"status":400}


But when I use a simple condition:



        $elasticQuery = [
'multi_match' => [
'query' => $text,
'fields' => ['name^4', 'description'],
],
];


I got a valid result:



[hits] => Array
(
[total] => 1
[max_score] => 7.4126062
[hits] => Array
(
[0] => Array
(
[_index] => select_vote
[_type] => vote
[_id] => 16
[_score] => 7.4126062
[_source] => Array
(
[id] => 16
[slug] => in-the-film-babe-what-type-of-animal-was-babe
[name] => In the film Babe, what type of animal was Babe?
[description] => Babe is a 1995 A...
[created_at] => 2018-11-10 09:14:15
[category_id] => 2
[category] => Array
(
[name] => Movie&Cartoons
[slug] => movie-cartoons
[created_at] => 2018-11-10 09:14:12
)

)

)

)

)


is this a valid format for multi-request?



MODIFIED BLOCK # 2:
Making some search I found work:



$elasticQuery = [
"bool" => [
'should' => [


[
"multi_match" => [
"query" => $text,
"type" => "cross_fields",
"fields" => [
"name^4",
"description"
]
]
],

[
'match' => [
'category_id' => [
'query' => 1,
]
]
],

[
'match' => [
'category_id' => [
'query' => 3,
]
]
],

]
]
];


when I need to make a search by text fields and the array of category (1 and 3) in the example above it works but looks like it works like an "OR" condition, but I need to make a restriction like "AND" using SQL terminology...



Which way is correct in order to make a restriction as like "AND"?



Thanks!







elasticsearch laravel-5






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 29 '18 at 6:50









W.Ambrozic

901212




901212










asked Nov 17 '18 at 8:42









mstdmstdmstdmstd

126615




126615













  • Please, look at MODIFIED BLOCK # 2

    – mstdmstd
    Nov 21 '18 at 8:54





















  • Please, look at MODIFIED BLOCK # 2

    – mstdmstd
    Nov 21 '18 at 8:54



















Please, look at MODIFIED BLOCK # 2

– mstdmstd
Nov 21 '18 at 8:54







Please, look at MODIFIED BLOCK # 2

– mstdmstd
Nov 21 '18 at 8:54














1 Answer
1






active

oldest

votes


















5





+100









If you just change should to must it is not going to work because category_id cannot have two values at the same time (unless it's an array, but it's not).



You need to use the following query instead:



$elasticQuery = [
"bool" => [
'must' => [
[
"multi_match" => [
"query" => $text,
"type" => "cross_fields",
"fields" => [
"name^4",
"description"
]
]
],
],
'filter' => [
[
'terms' => [
'category_id' => [ 1, 3 ]
]
]
]
]
];





share|improve this answer
























  • Great ! glad it helped!

    – Val
    Nov 30 '18 at 6:28











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53349629%2felasticsearch-syntax-error-with-multi-fields-search%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









5





+100









If you just change should to must it is not going to work because category_id cannot have two values at the same time (unless it's an array, but it's not).



You need to use the following query instead:



$elasticQuery = [
"bool" => [
'must' => [
[
"multi_match" => [
"query" => $text,
"type" => "cross_fields",
"fields" => [
"name^4",
"description"
]
]
],
],
'filter' => [
[
'terms' => [
'category_id' => [ 1, 3 ]
]
]
]
]
];





share|improve this answer
























  • Great ! glad it helped!

    – Val
    Nov 30 '18 at 6:28
















5





+100









If you just change should to must it is not going to work because category_id cannot have two values at the same time (unless it's an array, but it's not).



You need to use the following query instead:



$elasticQuery = [
"bool" => [
'must' => [
[
"multi_match" => [
"query" => $text,
"type" => "cross_fields",
"fields" => [
"name^4",
"description"
]
]
],
],
'filter' => [
[
'terms' => [
'category_id' => [ 1, 3 ]
]
]
]
]
];





share|improve this answer
























  • Great ! glad it helped!

    – Val
    Nov 30 '18 at 6:28














5





+100







5





+100



5




+100





If you just change should to must it is not going to work because category_id cannot have two values at the same time (unless it's an array, but it's not).



You need to use the following query instead:



$elasticQuery = [
"bool" => [
'must' => [
[
"multi_match" => [
"query" => $text,
"type" => "cross_fields",
"fields" => [
"name^4",
"description"
]
]
],
],
'filter' => [
[
'terms' => [
'category_id' => [ 1, 3 ]
]
]
]
]
];





share|improve this answer













If you just change should to must it is not going to work because category_id cannot have two values at the same time (unless it's an array, but it's not).



You need to use the following query instead:



$elasticQuery = [
"bool" => [
'must' => [
[
"multi_match" => [
"query" => $text,
"type" => "cross_fields",
"fields" => [
"name^4",
"description"
]
]
],
],
'filter' => [
[
'terms' => [
'category_id' => [ 1, 3 ]
]
]
]
]
];






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 22 '18 at 6:55









ValVal

107k6146180




107k6146180













  • Great ! glad it helped!

    – Val
    Nov 30 '18 at 6:28



















  • Great ! glad it helped!

    – Val
    Nov 30 '18 at 6:28

















Great ! glad it helped!

– Val
Nov 30 '18 at 6:28





Great ! glad it helped!

– Val
Nov 30 '18 at 6:28




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53349629%2felasticsearch-syntax-error-with-multi-fields-search%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

How to change which sound is reproduced for terminal bell?

Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents

Can I use Tabulator js library in my java Spring + Thymeleaf project?