Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Search in Elasticsearch using query_string on specific ids

If I index this three items with ids, 10, 11 and 12:

curl -X PUT 'localhost:9200/myindex/item/10?pretty=true' -d '{
  "title" : "Microsiervos - Discos duros de 10TB",
  "body" : "Empiezan a sacar DD de 30GB en el mercado",
  "source_id" : "27",
  "time_order" : "1379548800:0000004153553"
}'

curl -X PUT 'localhost:9200/myindex/item/11?pretty=true' -d '{
  "title" : "Microsiervos - En el 69 llegamos a la luna",
  "body" : "Se cumplen 3123 anos de la llegada a la luna y mercado",
  "source_id" : "27",
  "time_order" : "1433109600:556e006ff335c"
}'

curl -X PUT 'localhost:9200/myindex/item/12?pretty=true' -d '{
  "title" : "Cadena Ser - Cumplidos 288 programas",
  "body" : "Hoy hemos cumplido los 288 programas que prometimos prometer",
  "source_id" : "69",
  "time_order" : "1390344242:0000002764401"
}'

I would like to know the way to search on two (as a example) specific id, let's say 10 and 11.

I know there ids to search by index, but this returns the entire document with the given id. I would like to combine ids and query_string to make something like this:

curl -XGET 'localhost:9200/myindex/item/_search?pretty=true' -d '{
    "query": {
            "id" : {
                "type" : "item",
                "values" : ["10", "11"]
             },
            "query_string" : {
               "default_field" : "body",
               "query" : "mercado"
}}}'

This means I want to search for the word mercado of the field body only in the indexes 10 and 11 (but not 12). That query of course doesn't work. Can someone tell me how can archive that goal?

like image 471
Avión Avatar asked Feb 18 '26 13:02

Avión


1 Answers

You can use _id field to search on particular ids. Try something like this

{
  "query": {
    "bool": {
      "must": [
        {
          "terms": {
            "_id": [
              "10",
              "11"
            ]
          }
        },
        {
          "query_string": {
            "default_field": "body",
            "query": "mercado"
          }
        }
      ]
    }
  }
}
like image 111
ChintanShah25 Avatar answered Feb 21 '26 15:02

ChintanShah25



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!