Elasticsearch one record for one matching query

Mohd Shahid

I have one elasticsearch index in which I have so many records. There is a field username, I want to get latest 1 post of each username by passing comma separated values, example ::

john,shahid,mike,jolie

and I want latest 1 post of each usernames. How can I do this? I can do it by passing one username at a time but it will hit so many http requests. I want to do it in one request.

Val

You could use a filtered terms aggregation coupled with a top_hits one in order to achieve what you need:

{
  "size": 0,
  "query": {
    "bool": {
      "filter": {
        "terms": {
          "username": [ "john", "shahid", "mike", "jolie" ]
        }
      }
    }
  },
  "aggs": {
    "usernames": {
      "filter": {
        "terms": {
          "username": [ "john", "shahid", "mike", "jolie" ]
        }
      },
      "aggs": {
        "by_username": {
          "terms": {
            "field": "username"
          },
          "aggs": {
            "top1": {
              "top_hits": {
                "size": 1,
                "sort" : {"created_date" : "desc"}
              }
            }
          }
        }
      }
    }
  }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

PHP Query select one record

From Dev

Make a SELECT query from two tables with one record from matching rows in mysql

From Dev

How to query rows with one matching and one non-matching field

From Dev

SQL query with two conditions, update the one matching

From Dev

Merge records in one record with a SQL query

From Dev

Use Linq to query in order except the one record

From Dev

Delete Query for one record going super slow

From Dev

mysql two table with different record in one query

From Dev

Use Linq to query in order except the one record

From Dev

SQL Query - Join multiple fields to one record

From Dev

Active Record multiple selects in one query

From Dev

ElasticSearch: Using output of one query as input to another

From Dev

SQL: How to find if there is at least one record matching a condition

From Dev

msaccess join most recent matching record from one table to another

From Dev

SQL: How to find if there is at least one record matching a condition

From Dev

Use MySQL to select the next one record after a matching value

From Dev

msaccess join most recent matching record from one table to another

From Dev

How to get the number of hits of several matching fields in one record?

From Dev

report if any one record is not of matching size using sed

From Dev

SQL Query - select all from one table with matching records in another

From Dev

CloudKit: fetch record and references in one query (like in Parse)

From Dev

Efficient JPA query to find if at least one record exists with given condition

From Dev

SQL Query Writing Help - Multiple Lookups in One Record

From Dev

Access query to pull records that are max of one record but unique of another

From Dev

Capturing the record with the highest value for one column in a single query?

From Dev

Access query to pull records that are max of one record but unique of another

From Dev

MySQL selecting all records and for each select separate record - in one query

From Dev

SQL Query for Getting a Duplicate Record and Show it in One Row in Gridview

From Dev

Active Record - How to get all Direct and Indirect children in one query

Related Related

  1. 1

    PHP Query select one record

  2. 2

    Make a SELECT query from two tables with one record from matching rows in mysql

  3. 3

    How to query rows with one matching and one non-matching field

  4. 4

    SQL query with two conditions, update the one matching

  5. 5

    Merge records in one record with a SQL query

  6. 6

    Use Linq to query in order except the one record

  7. 7

    Delete Query for one record going super slow

  8. 8

    mysql two table with different record in one query

  9. 9

    Use Linq to query in order except the one record

  10. 10

    SQL Query - Join multiple fields to one record

  11. 11

    Active Record multiple selects in one query

  12. 12

    ElasticSearch: Using output of one query as input to another

  13. 13

    SQL: How to find if there is at least one record matching a condition

  14. 14

    msaccess join most recent matching record from one table to another

  15. 15

    SQL: How to find if there is at least one record matching a condition

  16. 16

    Use MySQL to select the next one record after a matching value

  17. 17

    msaccess join most recent matching record from one table to another

  18. 18

    How to get the number of hits of several matching fields in one record?

  19. 19

    report if any one record is not of matching size using sed

  20. 20

    SQL Query - select all from one table with matching records in another

  21. 21

    CloudKit: fetch record and references in one query (like in Parse)

  22. 22

    Efficient JPA query to find if at least one record exists with given condition

  23. 23

    SQL Query Writing Help - Multiple Lookups in One Record

  24. 24

    Access query to pull records that are max of one record but unique of another

  25. 25

    Capturing the record with the highest value for one column in a single query?

  26. 26

    Access query to pull records that are max of one record but unique of another

  27. 27

    MySQL selecting all records and for each select separate record - in one query

  28. 28

    SQL Query for Getting a Duplicate Record and Show it in One Row in Gridview

  29. 29

    Active Record - How to get all Direct and Indirect children in one query

HotTag

Archive