Coder Social home page Coder Social logo

elasticsearchdsl's Introduction

ElasticsearchDSL

Introducing Elasticsearch DSL library to provide objective query builder for Elasticsearch bundle and elasticsearch-php client. You can easily build any Elasticsearch query and transform it to an array.

If you need any help, stack overflow is the preferred and recommended way to ask ONGR support questions.

Build Status codecov Latest Stable Version Total Downloads Scrutinizer Code Quality

If you like this library, help me to develop it by buying a cup of coffee

Buy Me A Coffee

Version matrix

Elasticsearch version ElasticsearchDSL version
>= 7.0 >= 7.0
>= 6.0, < 7.0 >= 6.0
>= 5.0, < 6.0 >= 5.0
>= 2.0, < 5.0 >= 2.0 (not supported)
>= 1.0, < 2.0 1.x (not supported)
<= 0.90.x not supported

Documentation

The online documentation of the bundle is here

Try it!

Installation

Install library with composer:

$ composer require ongr/elasticsearch-dsl

elasticsearch-php client is defined in the composer requirements, no need to install it.

Search

Elasticsearch DSL was extracted from Elasticsearch Bundle to provide standalone query dsl for elasticsearch-php. Examples how to use it together with Elasticsearch Bundle can be found in the Elasticsearch Bundle docs.

If you dont want to use Symfony or Elasticsearch bundle, no worries, you can use it in any project together with elasticsearch-php. Here's the example:

If you are using Symfony there is also the ElasticsearchBundle which provides full integration with Elasticsearch DSL.

The library is standalone and is not coupled with any framework. You can use it in any PHP project, the only requirement is composer. Here's the example:

Create search:

 <?php
  require 'vendor/autoload.php'; //Composer autoload

  $client = ClientBuilder::create()->build(); //elasticsearch-php client
  
  $matchAll = new ONGR\ElasticsearchDSL\Query\MatchAllQuery();
  
  $search = new ONGR\ElasticsearchDSL\Search();
  $search->addQuery($matchAll);
  
  $params = [
    'index' => 'your_index',
    'body' => $search->toArray(),
  ];
  
  $results = $client->search($params);

Elasticsearch DSL covers every elasticsearch query, all examples can be found in the documentation

elasticsearchdsl's People

Contributors

ansata2001 avatar asev avatar bcremer avatar chyzas avatar dvondrak avatar einorler avatar emgiezet avatar grandltu avatar htaghizadeh avatar ivannis avatar j13k avatar juliensantos87 avatar knysakpatryk avatar linasmo avatar ltrocky avatar luciansabo avatar mvar avatar niels-nijens avatar pascalthesing avatar pmishev avatar ronrademaker avatar saimaz avatar sbofirov avatar soebbing avatar tchiotludo avatar tomaspocevicius avatar trandangtri avatar tw99 avatar vadersv avatar zylius avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

elasticsearchdsl's Issues

Group aggregation by it's kind

There are too many aggs and names are starting to duplicate. There is introduced pipeline namespace, in addition, we could move bucket and metric also to it namespace group.

Don't forget to keep BC and add deprecations.

function_score has problem

with this function

private function applyQuery(array &$function, BuilderInterface $query = null)
    {
        if ($query) {
            $function['query'] = $query->toArray();
        }
    }

When i execute these codes below:

$functionScoreQuery = new FunctionScoreQuery(new MatchAllQuery());
$stateTermQuery = new TermQuery('state', 'FINISH');
$functionScoreQuery->addWeightFunction(100, $stateTermQuery);
$stateTermQuery = new TermQuery('state', 'PENDING');
$functionScoreQuery->addWeightFunction(90, $stateTermQuery);

I cannot generate "filter" for attribute state

for example

.....

"function_score": {
                        "query": {
                            "match_all": []
                        },
                        "functions": [
                            {
                                "weight": 100,
                                "filter": {
                                    "term": {
                                        "state": "FINISH"
                                    }
                                }
                            },
                            {
                                "weight": 90,
                                "filter": {
                                    "term": {
                                        "state": "PENDING"
                                    }
                                }
                            }
                        ]
                    }

.....
Please let me know how can i create filter, it's always generate query

ReversedNestedAggregation cannot be used without inner aggregations

Currently aggregations have to be added to the ReverseNestedAggregation, otherwise an exception will be thrown:

throw new \LogicException("Reverse Nested aggregation `{$this->getName()}` has no aggregations added");

Here is a use case: I want to aggregate on nested objects, but return the doc_count of the top level documents (because several nested objects can match). This can be achieved by:

"aggs": {
    "color_nested": {
      "nested": {
        "path": "variants"
      },
      "aggs": {
        "color": {
          "terms": {
            "field": "variants.color"
          },
          "aggs": {
            "color_reverse": {
              "reverse_nested": {}
            }
          }
        }
      }
    }
  }

As illustrated by this query, in this case scenario there is no point in having additional aggregations and it is supported by Elasticsearch. I think we should remove the exception here, what do others think?

addDecayFunction without options

addDecayFunction does not work if no options are passed. There is no function with "0" name.

{
  "functions": [
    {
      "linear": {
        "myField": {
          "origin": 0,
          "scale": 100
        }
      },
      "0": []
    }
  ]
}

IMHO options should be siblings of "myField" and not "linear". Otherwise you will not be able to add decay on multi value fields.

Ability to use Nested Child Query with parent child inner hits

Hi,

I'm currently using the HasChildQuery class to include additional documents within my result set. I need to include the inner_hits part of the query in order to get the right results but it looks like this ability isn't part of the HasChildQuery implementation when dealing with parent/child documents (https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-inner-hits.html#parent-child-inner-hits).

Do you know if this is planned to be included in later versions? I saw that inner_hits support was added for other queries but not for parent child ones.

Some queries should support multiple fields

I have tested range query and found out that it works with multiple fields (tested with ES 2.1.1). This library does not support that.

Example:

{
    "range": {
        "price": {
            "gt": 50
        },
        "title": {
            "gt": "ccc"
        }
    }
}

Currently if you want to achieve something like that using our library you need to create two range queries and put them into bool.

We should check how many queries support multiple fields and think how we could add support for it.

Edit: prefix and regexp queries also supports multiple fields (tested on ES 2.1.1).

How to use filtered

Is there a way to set filtered node in query node?

Example Query:
{ "query":{ "filtered":{ "query":{ "query_string":{ "query":"test", "fields": ["title", "content","abstract","page_title_s"], "minimum_should_match":"30%" } }, "filter":{ "bool":{ "should":[ { "term":{ "CType":"bullets" } }, { "term":{ "CType":"text" } }, { "term":{ "CType":"textpic" } }, { "term":{ "CType":"header" } }, { "term":{ "CType":"table" } } ] } } } } }

Rename `Suggest` to the `TermSuggest`

It's actually a term suggest, so the name should be correct.

toArray function should return the name as a key, and SuggestEndoint has to handle suggest key return. e.g. the structure should be like this:

...
"suggest" : {
  "my-suggest-1" : {
    "text" : "the amsterdma meetpu",
    "term" : {
      "field" : "body"
    }
  },
  "my-suggest-2" : {
    "text" : "the rottredam meetpu",
    "term" : {
      "field" : "title"
    }
  }
}

So here's what we have to do:

  • Refactor a SuggestEndpoint to handle more than one suggestion and provide a suggest key.
  • Rename Suggest to the TermSuggest
  • Add $name as a key to return the structure in toArray function in TermSuggest.

MatchAllQuery() not working with Elasticsearch version 5 if no parameters are provided

If i instantiate an new MatchAllQuery Object with for example a boost of 1 MatchAllQuery(['boost' => 1]) everything works as intended:
"match_all":{"boost":1}

However if i provide no parameters to MatchAllQuery(), the getParameters() returns an empty array which remains an array when encoded to JSON:
"match_all":[]
That raises an exception inside Elasticsearch:

[match_all] query malformed, no start_object after query name

Elasticsearch expects an object even if it is empty.

Expected JSON:
"match_all":{}

Multiple Nested Arguments

I'm looking to reproduce the following:

"aggs": {
    "stock_type": {
      "terms": {
        "field": "stock_type"
      },
      "aggs": {
        "year": {
          "terms": {
            "field": "year",
            "size": 500
          },
          "aggs": {
            "make": {
              "terms": {
                "field": "make",
                "size": 500
              },
              "aggs": {
                "model": {
                  "terms": {
                    "field": "model",
                    "size": 500
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}
       

Basically, I'm looking for:

  • Stock > Year > Make > Model

I have 3 days of experience with ES but I am stepping into a working established project.

The example code implies some background knowledge in ES.

Any help or suggestion would be greatly appreciated.

FiltersAggregation generates invalid array

The FiltersAggregation adds filters inside an array with the type as key. Though, this generates the following array for a TermQuery with the 'term'-key twice in the stack:

array(
    'filtername' => array(
        'term' => array(
            'term' => array(
                'fieldname' => 'fieldvalue'
            )
        )
    )
)

This filter will throw an exception when sending it with a query to an Elasticsearch server.

I can supply a PR to fix this issue if you want.

No alive nodes found in your cluster on after 2.2.0 update

Hi,

After upgrading from the elasticsearch-dsl 2.1.2 to 2.2.0, we are receiving the No alive nodes found in your cluster. We reverted back and it is working fine.

Is something changed on the version release? we are also using shield along with our ES node.

Regards,

Cannot set "size" parameter for TermsAggregation

According to the ES documentation, when performing a "terms" aggregation, ElasticSearch will by default return the top 10 terms. If you want more, you have to pass an additional "size" parameter.

However, there is no way to set that parameter on ONGR\ElasticsearchDSL\Aggregation\Bucketing\TermsAggregation .

It seems to me that this class (and perhaps other aggregation classes?) should either:

  1. Have a getter/setter for the "size" parameter, or
  2. Support an optional "parameters" array in the constructor allowing the passing of any additional parameters. This matches the classes in ONGR\ElasticsearchDSL\Query.

Of course option 2 is more flexible, but perhaps too much so?

I'd be happy to implement either one and make a PR, but I'd like to know that a solution is not already in the pipeline, and also get your input on which of the two options you'd prefer.

Raw DSL element needed

When building a Query, sometimes we need a feature that is not supported by ESB yet. Some RawXXX is needed of form:

$search->addAggregation(new RawAggregation([
    'name' => 'foo',
    'field' => 'field,
    'aggregations' => [/*...*/],
]));

Which would inject custom aggregation introduced to Elastisearch, for example, 2 days ago.

Other types of elements might be added too, like RawFilter, etc.

origin: ongr-io/ElasticsearchBundle#322

Add options support to SpanNotQuery

Removes matches which overlap with another span query. The span not query maps to Lucene SpanNotQuery. Here is an example:

{
    "span_not" : {
        "include" : {
            "span_term" : { "field1" : "hoya" }
        },
        "exclude" : {
            "span_near" : {
                "clauses" : [
                    { "span_term" : { "field1" : "la" } },
                    { "span_term" : { "field1" : "hoya" } }
                ],
                "slop" : 0,
                "in_order" : true
            }
        }
    }
}

The include and exclude clauses can be any span type query. The include clause is the span query whose matches are filtered, and the exclude clause is the span query whose matches must not overlap those returned.
Read More...

BoolQuery::getQueries with ie. BoolQuery::MUST triggers 'Undefined index: must'

For my particular use case I'm re-using the query (instances) in aggregations. When using the BoolQuery::getQueries method with a $boolType argument. It will trigger a notice that an array index is not defined when no query instances have been added to the boolType.

From the docblock you'd assume that it would always return an (empty) array. Also because there's currently no other way to read the added query instances from the BoolQuery instance with the specified boolType.

I can provide a PR for this if you'd like.

Nested query with bool doesn't work when there is only one MUST condition.

$bool = new BoolQuery();
$bool->add(new MatchQuery('some.field', 'someValue'), BoolQuery::MUST);

$nested = new NestedQuery('urls', $bool);

Will produce:

{
  "nested": {
    "path": "urls",
    "query": {
      "bool": {
        "match": {
          "some.field": {
            "query": "someValue"
          }
        }
      }
    }
  }
}

Missing must condition inside bool...

Add "type" support to IdsQuery

Filters documents that only have the provided ids. Note, this query uses the _uid field.

{
    "ids" : {
        "type" : "my_type",
        "values" : ["1", "4", "100"]
    }
}

The type is optional and can be omitted, and can also accept an array of values. If no type is specified, all types defined in the index mapping are tried.

multiple termQuery not working

Hi !
I am trying to execute this query but it seems that it's not supported to add multiple termQuery.

<?php
       $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, BoolQuery::MUST);

        $queryArray = $search->toArray();
        echo "<pre>";var_dump($queryArray);die(); 

Just the laste query was token

array(1) {
  ["query"]=>
  array(1) {
    ["term"]=>
    array(1) {
      ["tag"]=>
      string(3) "dsl"
    }
  }
} 

Is it a bug or I am doing it wrongly ?

PS: I am using ElasticSearch 2.3.3 and elasticsearch-dsl 2.2.

Thanks in advance,

Provide more information in README

At least it would be nice to point users to documentation.

At best [IMHO] we could provide all the main information directly in README. Nothing could be simpler than to open repository and see what is it, when to use it and how to use it.

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.