last day (19 days later) » 

9:00 AM
1
A: How to combie bool must and sort for elasticsearch

ValYou're almost there. You've correctly placed the empty sort array at the same level as your query, which is correct. The issue comes later when you try to feed it as a bool/must constraint instead of in the empty sort array. // sorting is not working with bool and must if ($request->request->...

 
thanks a lot for your answer, but it is not sorting as it should, can you kindly check it once more, thanks a lot again for your answer :)
 
Val
My bad, I've edited my answer
 
@thanks a lot for your answer val :), although it is not working and not sorting the document :)
 
Val
Are you sure your Salary field name is correct? Can you print out the generated query perhaps using json_encode($params) or similar?
 
i have update my question val, can u kindly have a look :)
So when i sort by name it is not sorting according to the name
 
Val
9:00 AM
You've shared the response, can you also share the query as JSON please?
 
I HAVE UPDATE MY QUESTION, ACN U KINDLY HAVE A LOKK PLEASE :)
 
Val
You're not sorting on the correct field: name should be Name with uppercase
 
if i put "Name" it returns me an error, cause the actual field name is "name"
 
Val
Can you show the error?
 
i have update the error
Hi val
 
Val
9:01 AM
Hi
The error is talking about the Title field not the Name field.
 
actually the field name is title
i just change the name on stack Name
it is actually title
 
Val
Not easy to follow if what you input on StackOverflow is different than what your ES is configured with :)
 
{"index":"myIndex","type":"myType","size":30,"body":{"query":{"match_all":[]},"s‌​ort":[{"title":"asc"}]}}
this is the real one actually
sorry for being wrong
but the field name is title
 
Val
I suggest you update your question with the correct field names everywhere, otherwise I won't be able to track the error down.
 
ok, i will do that now
i have update my question val :)
can u kindly have a look please
 
Val
9:08 AM
jas
so what is the output of that query now?
still the one that's posted in the question and which looks unsorted?
 
yes that is the right one actually
 
Val
What's weird is that all scores are null and all documents are sorted on "0"
 
yes
i also see that
but that is what i get when i print the result
 
Val
Do you have the /head/ plugin installed?
 
yes i have
 
Val
9:13 AM
can you try to run the query in there directly?
 
yes, just a second
if i put get and put the query it return me this
{
"status": 200,
"name": "Korrek",
"cluster_name": "christofer",
"version": {
"number": "1.7.1",
"build_hash": "b88f43fc40b0bcd7f173a1f9ee2e97816de80b19",
"build_timestamp": "2015-07-29T09:54:16Z",
"build_snapshot": false,
"lucene_version": "4.10.4"
},
"tagline": "You Know, for Search"
}
and the query i put is
{
"index": "myIndex",
"type": "myType",
"size": 30,
"body": {
"query": {
"match_all": []
},
"sort": [
{
"title": "asc"
}
]
}
}
 
Val
Run your query against /myIndex/myType/_search and not the root /
side question: why did you accept the answer since it's not working yet?
any luck?
 
9:38 AM
{
"took": 10,
"timed_out": false,
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"hits": {
"total": 1066,
"max_score": 1,
"hits": [
{
"_index": "myIndex",
"_type": "myType",
"_id": "pa9cdceb5fe00daa943e4892f551339918d38f0bd",
"_score": 1,
"_source": {
"title": "Gustaf Dahlbergs videoaudition till Idol Unknown TV Play-biljetten",
"content": "Se 26-åriga Gustaf Dahlbergs videoaudition till Idol Unknown TV Play-biljetten. Gillar du Gustafs audition? Dela klippet med dina vänner.",
"guid": "pa9cdceb5fe00daa943e4892f551339918d38f0bd",
sorry, internet was down
i also accept the answer because of your effort of solving problem
i really appriciate that
 
Val
No worries. It doesn't seem like your sort was taken into account since there is no sort field in the response
oh wait
you only need to use the body, and not the whole thing
so only this
{
"query": {
"match_all": []
},
"sort": [
{
"title": "asc"
}
]
}
also use POST instead of GET
 
9:53 AM
{
"took": 67,
"timed_out": false,
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"hits": {
"total": 1066,
"max_score": null,
"hits": [
{
"_index": "myIndex",
"_type": "myType",
"_id": "pe065319de73937aa6ef46413afd7aac26a58a611",
"_score": null,
"_source": { "title": "Smarason trycker in 2-0 till HIF efter retur","myType": "HIF gör 2-0 mot Halmstad."},
"sort": [
"0"
]
}
,
{
"_index": "myIndex",
"_type": "myType",
"_id": "pebc44a70008f53f74f23ab23f8a1f79b2b729448",
"_score": null,
"_source": { "title": "Anders Svenssons tips gav 1-0","myType": "Anders Svenssons tips"},
it still sort [0]
it should not be sort[0] always
first 5 documents its coming sort [0] than it's coming sort[00]
it's really strange
 
Val
no it should not... hard to tell without having my hands on it, though. I'm out of options... Everything looks correct
try one more thing, please copy/paste the following
 
what's that
and thanks a lot for your help
 
Val
{
"query": {
"match_all": {}
},
"sort": {
"title": "asc"
}
}
 
i really really appreciate it
 
Val
no worries
 
9:59 AM
it is same result
i mean it's the same result
can i ask u one last question
if u don't mind
 
Val
sure go ahead
 
$typefilter = array('male', 'female');

if ($request->request->get('male') && $request->request->get('female') ) {
echo "both";
$params['body']['query']['bool']['must'][] = array(
'term' => array(
"type" => implode(" ", $typefilter)
)
);
}
i am trying to querying when the both is selcted
like male and female
but it's not working for me
i have also tried like so
"type" => array('male', 'female')
 
Val
10:54 AM
sorry I was at lunch
Is there any chance for you to create a gist where I can recreate it all on my local ES? The gist should contain the index creation, sample documents and a query to test
I'm going to answer your other question on SO
 
no problem
 
Val
you can create a gist here gist.github.com
 
thanks i wil create it now
 
Val
that's the only way I see to move forward efficiently
 
thanks a lot
one last thing
this line is showing error
 
Val
11:04 AM
my bad
should be "minimum_should_match" => count($typefilter)
i.e. with => not :
if you really need both terms to be present, then you need that line, otherwise if only one term (male or female) needs to be present, you can leave it out
 
thanks a lot Val :)
thanks a lot
 
Val
Cool
none of my business but are you working at Qlik (Sweden)?
 
{"index":"myIndex","type":"myType","size":30,"body":{"query":{"bool":{"must":[{"‌​query_string":{"default_field":"source","query":"99mac.se"}},{"term":{"type":"svi‌​deo"}},{"term":{"type":"article"}},{"terms":{"type":["svideo","article"],"minimum‌​_should_match":2}}]}},"sort":[]}}
yes, i work at qlik in sweden
i am a junior developer
just started to work recently
but may i ask how do u know !
 
Val
Google is my friend ;)
 
haaaa...haaaa.....haaaa.....
u r a smart person :0
cool
{"index":"myIndex","type":"myType","size":30,"body":{"query":{"bool":{"must":[{"‌​query_string":{"default_field":"source","query":"99mac.se"}},{"term":{"type":"svi‌​deo"}},{"term":{"type":"article"}},{"terms":{"type":["svideo","article"],"minimum‌​_should_match":2}}]}},"sort":[]}}
this is the query when i run it
 
Val
11:12 AM
it doesn't seem right
your have term:svideo + term:article and then again terms:svideo+article
you only need the last one
 
yah
but when i select the both of the button than it is working on the 3 query
one is svideo
than article
than both
 
Val
something's off in the way you build the query
 
yah, i think somewhere i am making the mistake
if ($request->request->get('svideo')) {
$params['body']['query']['bool']['must'][] = array(
'term' => array(
"type" => 'svideo'
)
);
}

if ($request->request->get('article')) {
echo "article is working";
$params['body']['query']['bool']['must'][] = array(
'term' => array(
"type" => 'article'
)
);
}


if ($request->request->get('svideo') && $request->request->get('article') ) {
$params['body']['query']['bool']['must'][] = array(
'terms' => array(
"type" => $typefilter,
"minimum_should_match" => count($typefilter)
so this is the whole one
 
Val
you need some else cases
if ($request->request->get('svideo') && $request->request->get('article') ) {
}
else if ($request->request->get('svideo')) {
}
else if ($request->request->get('article')) {
}
 
great val:)
thanks a lot
 
Val
11:16 AM
sure
 
you are really cool :)
 
Val
just helping
ok, I have to jump on something else now, let me know when the gist is ready
 
but you think really fast
yah, thanks again
see u later
 
Val
I guess 20 years of programming do help a bit :)
 
oooooo, than u r talking to a kid actually
where i am just couple of months old
thanks again for your help :)
 
Val
11:19 AM
"old" generation transmitting to the "new" one
 
You are not old, you are gold
{"index":"myIndex","type":"myType","size":30,"body":{"query":{"bool":{"must":[{"‌​query_string":{"default_field":"source","query":"99mac.se"}},{"terms":{"type":["s‌​video","article"],"minimum_should_match":2}}]}},"sort":[]}}
now this query is executing although it not coming the expected result
in terms it should select both terms
 
Val
is type an array of several values or always a single one?
 
no there is only 2 values on the type
svideo or article
 
Val
ok that wasn't clear, remove the second line (minimum_should_match) then
 
so match_all
ok
thanks val
that;s work perfectly
thanks a lot Val
thanks a lot
 
Val
11:28 AM
Awesome!
That was related to your latest question on SO, right?
 
you are really cool
yah
 
Val
cool
now if you get me some gist, we can beat the previous one
 
yup
working on it
 
Val
1. create index
2. add data
3. create query
 
should i give u an dump file as well
great
understand
 
Val
11:30 AM
put just a few documents in the gist, no need for everything
 
yah sure
 
Val
it's only about sorting
 
yah
thanks Val
just 5 min plz
 
Val
no rush, I'm on something else anyway
I'll check back later
 
thanks Val
 
Val
yes, although I also need the mapping you used to create your index
 
just 1 min plz
sorry did not include that
I HAVE UPDATE MY MAPPING
Can u have a look please
 
Val
cool. would you mind also adding the output of this command ?

curl -XGET localhost:9200/myIndex
 
sure
just a second
i have update the result
in the localhost section
 
Val
12:36 PM
sorry, can you replace with this instead

curl -XGET localhost:9200/myIndex?pretty
just so the output is formatted
thanks
 
sure
just 1 min
i have updated
 
Val
ok, thanks, that should do it
I'll let you know in here when I have something
 
thanks a lot Val
thanks a lot :)
 

  last day (19 days later) »