Ernest Poldrige
Ernest Poldrige

Reputation: 429

How to search in a JSON element?

I am using elasticsearch 7 and I have stored following index:

PUT myindex

{
    "id": 2,
    "price": {"amount":10.0, "currency": "EUR"},
    "folders": ["Book", "Newspaper"]
}

and I want to create two queries:

I tried to map a new index by using the following:

{
    "settings" : {
        "index" : {
            "sort.field" : ["price.amount", "price.currency"], 
            "sort.order" : ["asc", "asc"] 
        }
    },
    "mappings": {
        "properties": {
            "price": {
                "type": "keyword"

            },
            "currency": {
                "type": "keyword"
            }
        }
    }
}

but I got following error:

{
  "error": {
    "root_cause": [
      {
        "type": "resource_already_exists_exception",
        "reason": "index [myindex/1dsbGhj3RSCwm8Yad6oPOA] already exists",
        "index_uuid": "1dsbGhj3RSCwm8Yad6oPOA",
        "index": "myindex"
      }
    ],
    "type": "resource_already_exists_exception",
    "reason": "index [myindex/1dsbGhj3RSCwm8Yad6oPOA] already exists",
    "index_uuid": "1dsbGhj3RSCwm8Yad6oPOA",
    "index": "myindex"
  },
  "status": 400
}

thanks for any help

UPDATE

@LeBigCat here a sample of a list of documents for which I would like to create the queries:

PUT myindex2

{
    "id": 2,
    "price": {"amount":10.0, "currency": "EUR"},
    "folders": ["Book", "Newspaper"]
},
{
    "id": 3,
    "price": {"amount":15.0, "currency": "EUR"},
    "folders": ["Book"]
},
{
    "id": 4,
    "price": {"amount":3.0, "currency": "EUR"},
    "folders": ["Plant"]
},
{
    "id": 5,
    "price": {"amount":3.0, "currency": "USD"},
    "folders": []
}

Upvotes: 1

Views: 84

Answers (1)

ashutosh sharma
ashutosh sharma

Reputation: 302

Try to use this query:

{
    "sort": [{
        "price.amount": {
            "order": "asc"
        }
    }],
    "query": {
        "bool": {
            "must": [{
                "match": {
                    "folders": "Book"
                }
            }]
        }
    }
}

Upvotes: 1

Related Questions