Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Elasticsearch Dsl Laravel Package

ongr/elasticsearch-dsl

Object-oriented Elasticsearch query builder for PHP. Build searches, filters, aggregations and more with a DSL, then export to arrays for elasticsearch-php or ONGR ElasticsearchBundle. Supports Elasticsearch 5/6/7 via versioned releases.

View on GitHub
Deep Wiki
Context7

Function Score Query

More info about function score query is in the official elasticsearch docs

The function score query allows you to modify the score of documents that are retrieved by a query. This can be useful if, for example, a score function is computationally expensive and it is sufficient to compute the score on a filtered set of documents.

Example with linear decay function

{
  "query": {
    "function_score": {
      "query": {
        "range": {
          "price": {
            "gte": 10,
            "lte": 100
          }
        }
      },
      "functions": [
        {
          "linear": {
            "price": {
              "origin": 10,
              "scale": 50,
              "offset": 0,
              "decay" : 0.5
            }
          }
        }
      ]
    }
  }
}

In DSL

$rangeQuery = new RangeQuery('price');
$rangeQuery->addParameter(RangeQuery::GTE, 10);
$rangeQuery->addParameter(RangeQuery::LTE, 100);
$functionScoreQuery = new FunctionScoreQuery($rangeQuery);
$functionScoreQuery->addDecayFunction(
    'linear',
    'price',
    [
        'origin' => 10,
        'scale' => 50,
        'offset' => 0,
        'decay' => 0.5
    ]
);

$search = new Search();
$search->addQuery($functionScoreQuery);

$queryArray = $search->toArray();

Example weight function to multiply score of subset

{
  "query": {
    "function_score": {
      "query": {
        "match_all": {}
      },
      "functions": [
        {
          "weight": 2,
          "filter": {
            "range": {
              "price": {
                "gte": 10,
                "lte": 40
              }
            }
          }
        }
      ]
    }
  }
}

In DSL:

$functionScoreQuery = new FunctionScoreQuery(new MatchAllQuery());
$rangeFilter = new RangeQuery('price', ['gte' => 10, 'lte' => 100]);
$functionScoreQuery->addWeightFunction(2, $rangeFilter);

$search = new Search();
$search->addQuery($functionScoreQuery);

$queryArray = $search->toArray();

Example Field Value Factor Function

$functionScoreQuery = new FunctionScoreQuery(new MatchAllQuery());
$existsQuery = new ExistsQuery('price');
$functionScoreQuery->addFieldValueFactorFunction('price', 0.5, 'ln', $existsQuery, 0);

$search = new Search();
$search->addQuery($functionScoreQuery);

$queryArray = $search->toArray();

Will result in

{
  "query": {
    "function_score": {
      "query": {
        "match_all": {}
      },
      "functions": [
        {
          "field_value_factor": {
            "field": "price",
            "factor": 0.5,
            "modifier": "ln"
          },
          "filter": {
            "exists": {
              "field": "price"
            }
          }
        }
      ]
    }
  }
}

Random function example

$functionScoreQuery = new FunctionScoreQuery(new MatchAllQuery());
$functionScoreQuery->addRandomFunction();

$search = new Search();
$search->addQuery($functionScoreQuery);

$queryArray = $search->toArray();

Will result in

{
  "query": {
    "function_score": {
      "query": {
        "match_all": {}
      },
      "functions": [
        {
          "random_score": {}
        }
      ]
    }
  }
}
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
emuniq/filament-browser-notifications
syriable/filament-translator
hungnm28/livewire-form
wenprise/eloquent
crudly/encrypted
fadion/bouncy
cuci/prototurk-sdk
gos/pubsub-router-bundle
cuci/prototurk-sdk-symfony
clementtalleu/easyadmin-markdown-bundle
codeflextech/permission-manager
karnoweb/livewire-datepicker
sayedenam/sayed-dashboard
milito/query-filter
apiboxsym/user-bundle
apiboxsym/health-check-bundle
jayeshmepani/jpl-moshier-ephemeris-php
elnasnato/laraliveui
labrodev/rest-sdk
sampaui/sampaui