0

I use elasticsearch like :

{
  "query": {
    "match_phrase": {
      "title": "my title"
    }
  },
  "aggs": {
    "unique_title": {
      "cardinality": {
        "field": "title"
      }
    }
  }
}

i just want to sql

select distinct title from table where title like '%my title%'

the result give me multiple same results, "cardinality" dont worked whit "query"

if you dont understand me, Please forgive my poor English ^_^

2 Answers 2

1

Cardinality aggregation calculates the count of distinct values for a field.

Hence the equivalent sql query for the elasticsearch query you wrote would look like:

select count(distinct title) from table where title like '%my title%'

What you need to use is the Terms aggregation for getting the distinct titles.

{
  "query": {
    "match_phrase": {
      "title": "my title"
    }
  },
  "aggs": {
    "unique_title": {
      "terms": {
        "field": "title"
      }
    }
  }
}

And you need to look into the "aggregations" section of the search response to get the distinct values in the "buckets" array.

Sign up to request clarification or add additional context in comments.

Comments

0

You can use below query to get expected result:

GET my_index/my_type/_search
{
"from": 0,
"size": 200,
"query": {
    "filtered": {
        "filter": {
            "bool": {
                "must": {
                    "query": {
                        "wildcard": {
                            "title": "*my title*"
                        }
                    }
                }
            }
        }
    }
},
"_source": {
    "includes": [
        "title"
    ],
    "excludes": []
}
}

1 Comment

thank you for your answer but the results still has same value

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.