Comments (11)
Hi @dijadev
The query is correct. But in your case it should be formed as 3 bool queries in the must operator.
{
"query": {
"bool":
{
"must":[
{
"term":{"tag":"wow"}
},
{
"term":{"tag":"elasticsearch"}
},
{
"term":{"tag":"dsl"}
}
]
}
}
}
from elasticsearchdsl.
Hi @saimaz thank you for replying!
I tested by adding 3 bool queries but it does not work
Could you give the right query to get the array you showed ?
from elasticsearchdsl.
You query is right. The output should be like I posted.
$search = new Search();
$termQueryForTag1 = new TermQuery("tag", "wow");
$termQueryForTag2 = new TermQuery("tag", "elasticsearch");
$termQueryForTag3 = new TermQuery("tag", "dsl");
$search->addQuery($termQueryForTag1);
$search->addQuery($termQueryForTag2);
$search->addQuery($termQueryForTag3);
$queryArray = $search->toArray();
echo "<pre>";json_encode($queryArray);die();
Generates:
{
"query": {
"bool":
{
"must":[
{
"term":{"tag":"wow"}
},
{
"term":{"tag":"elasticsearch"}
},
{
"term":{"tag":"dsl"}
}
]
}
}
}
If not, sent me more info about your env. The exact ElasticsearchDSL version 2.2.x (what is x number), your PHP version.
from elasticsearchdsl.
I checked the code in the bundle and i found a key optional arg so i added for each query a different key as bellow and it works !
$search = new Search();
$bool = new BoolQuery();
$termQueryForTag1 = new TermQuery("tag", "wow");
$termQueryForTag2 = new TermQuery("tag", "elasticsearch");
$termQueryForTag3 = new TermQuery("tag", "dsl");
$bool->add($termQueryForTag1, BoolQuery::MUST,1);
$bool->add($termQueryForTag2, BoolQuery::MUST,2);
$bool->add($termQueryForTag3, BoolQuery::MUST,3);
$search->addFilter($bool)
$queryArray = $search->toArray();
echo "<pre>";json_encode($queryArray);die();
This must be fixed or mentioned in the doc.
Thanks,
from elasticsearchdsl.
It's still very interesting why key is not generated and when you add the second query it drops first. I still cannot reproduce your issue :(.
The key is for case when you want to add some identifier where you later can grab that specific query from BoolQuery
container. It should not be necessary for adding multiple TermQuery
.
from elasticsearchdsl.
Ok I'll debug this as soon as i can and will keep you in touch :)
from elasticsearchdsl.
Probably the problem is uniqid() in BoolQuery where he is assigning the key if not exists. I have the same problem on my local machine(xampp, php 7, windows 7) where microtime is just not refreshing quick enough. I solve it with hardcoded random generator
public function add(BuilderInterface $query, $type = self::MUST, $key = null)
{
if (!in_array($type, [self::MUST, self::MUST_NOT, self::SHOULD, self::FILTER])) {
throw new \UnexpectedValueException(sprintf('The bool operator %s is not supported', $type));
}
if (!$key) {
$key = str_random(60); //uniqid();
}
$this->container[$type][$key] = $query;
return $key;
}
public static function random($length = 16)
{
$string = '';
while (($len = strlen($string)) < $length) {
$size = $length - $len;
$bytes = static::randomBytes($size);
$string .= substr(str_replace(['/', '+', '='], '', base64_encode($bytes)), 0, $size);
}
return $string;
}
from elasticsearchdsl.
Thank you @puskic. The uniqid()
is definitely a problem. Your provided solution would work, but it seems a bit overhead to calculate just a unique string. I believe there should be more optimized way to do this.
from elasticsearchdsl.
I agree with you. I already have it, way not to use ;).
Probably
bin2hex(random_bytes(30))
will do the work.
Strange result will came out if $key overlaps, it should be unique enough...
from elasticsearchdsl.
Hi !
I confirm what @puskic said; this problem occurs just in windows env, and disappears when I deploy to a linux machine..
My debug on windows gives:
public function add(BuilderInterface $query, $type = self::MUST, $key = null)
{
if (!in_array($type, [self::MUST, self::MUST_NOT, self::SHOULD, self::FILTER])) {
throw new \UnexpectedValueException(sprintf('The bool operator %s is not supported', $type));
}
if (!$key) {
$key = uniqid();
echo $type.'=>'.$key.'<br>';
}
$this->container[$type][$key] = $query;
return $key;
}
It generates the same $key for the TermQuerys I added to BoolQuery
must=>5860f2aa7f977
must=>5860f2aa9fd1f
must=>5860f2aabc62e
must=>5860f2aabc62e
must=>5860f2aabcdfe
from elasticsearchdsl.
Fixed in v2.2.1 release
from elasticsearchdsl.
Related Issues (20)
- doc page link 404 HOT 1
- Implement GeoDistanceSort
- How do you do this sort HOT 2
- Is this project alive? HOT 8
- HTTP 500 Internal Server Error Class \Document\PostDoc does not exist
- Support Symfony 4.4 HOT 1
- How to build this query ?
- It is not possible to set field at function_score random_score
- DateHistogramAggregation does not support TimeZone HOT 2
- PHP 8 support HOT 7
- DateHistogramAggregation does not support 'timezone' ?
- Missing weight parameter in function score query for certain functions
- Same configuration returned different hits HOT 1
- php 8 support HOT 1
- Add new tag v6.2.1 with renaming of MaxAggregation.php class
- Elasticsearch 8.0 removed support for `interval` in `date_histogram` aggregation types HOT 2
- Lack of symfony/serializer 6 support HOT 1
- php 8.1 support HOT 3
- Elasticsearch 8 support HOT 1
- Compatibility issue with Laravel 10 and symfony/mime in ongr/elasticsearch-dsl v7.2.2 HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from elasticsearchdsl.