2

I am using elastic search in my Android application. I am passing filters in query string like below:

URL/IndexName/TypeName/_search?sort=registry_date:desc&from=0&size=10&q=firstname:رشید

That works fine. But i want to apply a prefix filter on firstname field for achieving below thing through query string:

{
  "query": {
    "bool": {
      "must": [
        {
          "prefix": {
            "firstname.keyword": "رشید"
          }
        }
      ],
      "must_not": [],
      "should": []
    }
  },
  "from": 0,
  "size": 10,
  "sort": [
    {
      "registry_date": {
        "order": "desc"
      }
    }
  ],
  "aggs": {}
}

Any help?

4
  • By prefix filter do you mean, for example, all names starting with "Fre". Would match "Fred" and "Freddy"? Commented Mar 9, 2018 at 9:51
  • @RishiDiwan yes . And I am referring to the prefix word in structured query in my question. Commented Mar 9, 2018 at 9:55
  • What is your ES version? If sufficiently updated elastic.co/guide/en/elasticsearch/reference/current/… Commented Mar 9, 2018 at 9:59
  • @RishiDiwan I can't append that in query string. Commented Mar 9, 2018 at 10:07

1 Answer 1

1

I cannot find now where I found this but for me (on Elasticsearch version 5.4) works something like this:

URL/IndexName/TypeName/_search?source=PLACE_JSON_HERE

where PLACE_JSON_HERE could be entire JSON which you are passing as a POST body.

So for example:

URL/IndexName/TypeName/_search?source={"query":{"match_all":{}}}

will return all the documents.

In your situation you can even replace the PLACE_JSON_HERE with:

{
  "query": {
    "bool": {
      "must": [
        {
          "prefix": {
            "firstname.keyword": "رشید"
          }
        }
      ],
      "must_not": [],
      "should": []
    }
  },
  "from": 0,
  "size": 10,
  "sort": [
    {
      "registry_date": {
        "order": "desc"
      }
    }
  ],
  "aggs": {}
}

But as I said I cannot find this in ES documentation now so I do not know if it works on all versions.


UPDATE

Tested on 6.2.2 version:

http://localhost:9200/_doc/_search?source={"query":{"match_all":{}}}&source_content_type=application/json

Works as expected. source_content_type is required with source since ES 6.

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

1 Comment

@Naila please take a look at my update. It works for me on the newest ES (6.2.2) version.

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.