Coder Social home page Coder Social logo

Comments (11)

saimaz avatar saimaz commented on June 21, 2024

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.

dijadev avatar dijadev commented on June 21, 2024

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.

saimaz avatar saimaz commented on June 21, 2024

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.

dijadev avatar dijadev commented on June 21, 2024

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.

saimaz avatar saimaz commented on June 21, 2024

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.

dijadev avatar dijadev commented on June 21, 2024

Ok I'll debug this as soon as i can and will keep you in touch :)

from elasticsearchdsl.

puskic avatar puskic commented on June 21, 2024

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.

saimaz avatar saimaz commented on June 21, 2024

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.

puskic avatar puskic commented on June 21, 2024

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.

dijadev avatar dijadev commented on June 21, 2024

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.

saimaz avatar saimaz commented on June 21, 2024

Fixed in v2.2.1 release

from elasticsearchdsl.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.