Giter Site home page Giter Site logo

Comments (7)

barseghyanartur avatar barseghyanartur commented on June 8, 2024

It should be possible to expose _score (however, it's not always available). Let me know if you have trouble implementing this. Eventually, PRs are welcome.

from graphene-elastic.

coreyculler avatar coreyculler commented on June 8, 2024

How would this be done? Would I have to create a custom resolver to retrieve the _score attribute? Is that attribute going to be available on the parent object within an ElasticsearchObjectType resolver?

from graphene-elastic.

barseghyanartur avatar barseghyanartur commented on June 8, 2024

Yep. Most likely, through a filter backend. It should be done in a declarative, non-breaking manner. Somewhat, like in highlight, but much simpler.

from graphene-elastic.

barseghyanartur avatar barseghyanartur commented on June 8, 2024

Check this (not yet released, just pushed into master):

from graphene import Node
from graphene_elastic import ElasticsearchObjectType
from graphene_elastic.filter_backends import (
    # ...
    ScoreFilterBackend,
    OrderingFilterBackend,
    # ...
)
from search_index.documents import Post as PostDocument

class Post(ElasticsearchObjectType):

    class Meta:

        document = PostDocument
        interfaces = (Node,)
        filter_backends = [
            # ...
            ScoreFilterBackend,
            OrderingFilterBackend,
            # ...
        ]


        # For `OrderingFilterBackend` backend
        ordering_fields = {
            # The dictionary key (in this case `tags`) is the name of
            # the corresponding GraphQL query argument. The dictionary
            # value (in this case `tags.raw`) is the field name in the
            # Elasticsearch document (`PostDocument`).
            'title': 'title.raw',

            # The dictionary key (in this case `created_at`) is the name of
            # the corresponding GraphQL query argument. The dictionary
            # value (in this case `created_at`) is the field name in the
            # Elasticsearch document (`PostDocument`).
            'created_at': 'created_at',

            # The dictionary key (in this case `num_views`) is the name of
            # the corresponding GraphQL query argument. The dictionary
            # value (in this case `num_views`) is the field name in the
            # Elasticsearch document (`PostDocument`).
            'num_views': 'num_views',

            # Score
            'score': '_score',
        }

Do make sure you have added ordering results by score (as shown in ordering_fields above).

Query like this:

query PostsQuery {
      allPostDocuments(
            search:{query:"Budget"},
            filter:{category:{value:"Elastic"}},
            ordering:{score:DESC}
      ) {
        edges {
          node {
            id
            title
            category
            content
            createdAt
            score
          }
        }
      }
    }

Will produce results similar to these:

{
  "data": {
    "allPostDocuments": {
      "edges": [
        {
          "node": {
            "id": "UG9zdDpMNnBiV1hNQjhYRzdJclZ2X20waA==",
            "title": "Budget.",
            "category": "Elastic",
            "content": "Bed television public teacher behind human up.\nMind anyone politics ball cost wife try adult. College work for.\nPlay five ten not sort energy.\nCommon word behind spring. All behind voice policy.",
            "createdAt": "1973-03-12T00:00:00",
            "score": 20.420774
          }
        },
       ]
    }
  }
}

from graphene-elastic.

coreyculler avatar coreyculler commented on June 8, 2024

Oh wow, thanks for the quick code update! I'll try it out tomorrow.

from graphene-elastic.

barseghyanartur avatar barseghyanartur commented on June 8, 2024

@coreyculler:

Some more work is done on this. It's now possible (in master) to allow sorting on score (if added as option to the ordering_fields). See updated example.

from graphene-elastic.

barseghyanartur avatar barseghyanartur commented on June 8, 2024

Released in 0.6.3.

from graphene-elastic.

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.