Elasticsearch CURL Query

Hi all !

I'm trying to merge multiple search query in ES but the result had no enough informations to match searched objects with results.

How can I do that ?

Thanks :slight_smile:

query sample:

curl -X GET "localhost:9200/_search?pretty" -H 'Content-Type: application/json' -d'
{
  "query": {
    "terms": {
      "labels": [ "Rebic", "Toto" ],
      "boost": 1.0
    }
  }
}

curl -X GET "localhost:9200/_search?pretty" -H 'Content-Type: application/json' -d'
{
    "_source": [ "labels", "id" ],
    "query" : {
        "bool": {
            "should": [
                {"match" : { "labels" : "Rebic" }},
                {"match" : { "labels" : "Toto" }}
            ]
        }
    }
}' 

response sample:

{
  "took" : 6,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 15,
      "relation" : "eq"
    },
    "max_score" : 24.525375,
    "hits" : [
      {
        "_index" : "enwiki_latest",
        "_type" : "_doc",
        "_id" : "r-1jU3QBzwBIdiQJJlKB",
        "_score" : 24.525375,
        "_source" : {
          "id" : "\"Q87486800\"",
          "labels" : "{\"en\": {\"language\": \"en\", \"value\": \"Jelena Rebic\"}, \"nl\": {\"language\": \"nl\", \"value\": \"Jelena Rebic\"}}"
        }
      },
      {
        "_index" : "enwiki_latest",
        "_type" : "_doc",
        "_id" : "ktgeU3QBzwBIdiQJ2ssN",
        "_score" : 17.667746,
        "_source" : {
          "id" : "\"Q2384109\"",
          "labels" : "{\"fr\": {\"language\": \"fr\", \"value\": \"La T\\u00eate \\u00e0 Toto\"}}"
        }
      }
    ]
  }
}

Can you describe in more detail what you're trying to do? What is this system and what's the query for?

The relationship between result and query is that the first result contains the word "Rebic" and the second result contains the word "Toto", both in the field "labels". The queries worked. What other information do you need Elasticsearch to return?

Hello @Emanuil , thanks for your help

from my previous answer i would like to generate a response like that or something similar :slight_smile: :

desired response sample:

{
  "took" : 6,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 15,
      "relation" : "eq"
    },
    "max_score" : 24.525375,
    "hits" : [
      {
        "_index" : "enwiki_latest",
        "_type" : "_doc",
        "_id" : "r-1jU3QBzwBIdiQJJlKB",
        "_score" : 24.525375,
        **"_labels" : Rebic**
        "_source" : {
          "id" : "\"Q87486800\"",
          "labels" : "{\"en\": {\"language\": \"en\", \"value\": \"Jelena Rebic\"}, \"nl\": {\"language\": \"nl\", \"value\": \"Jelena Rebic\"}}"
        }
      },
      {
        "_index" : "enwiki_latest",
        "_type" : "_doc",
        "_id" : "ktgeU3QBzwBIdiQJ2ssN",
        "_score" : 17.667746,
        **"_labels" : Toto**
        "_source" : {
          "id" : "\"Q2384109\"",
          "labels" : "{\"fr\": {\"language\": \"fr\", \"value\": \"La T\\u00eate \\u00e0 Toto\"}}"
        }
      }
    ]
  }
}

I think you want the highlight functionality. Maybe start with something simple to start, like the plain highlighter in this example.

EDIT: Let us know how it goes and share your query! It'll help other users (and is personally interesting..)

Hello !

in your case you will return the retrieve label , and I would like to remind wich match I searched :slight_smile:

query:

 curl -X GET "localhost:9200/_search?pretty" -H 'Content-Type: application/json' -d'
{
    "_source": [ "labels", "id" ],
    "query" : {
        "bool": {
            "should": [
                {"match" : { "labels" : "Rebic" }},
                {"match" : { "labels" : "toto" }}
            ]
        }
    }
}' 

desired response:

{
"took" : 9,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 7,
"relation" : "eq"
},
"max_score" : 20.856234,
"hits" : [
{
"_index" : "enwiki_latest",
"_type" : "_doc",
"_id" : "X38jaXQBb3qqttZ12sOf",
"_score" : 20.856234,
"_from" : "Rebic",
"_source" : {
"id" : ""Q12897826""
}
},
{
"_index" : "enwiki_latest",
"_type" : "_doc",
"_id" : "a4AkaXQBb3qqttZ1uSNW",
"_score" : 20.326504,
"_from" : "Rebic",
"_source" : {
"id" : ""Q22322231""
}
},
{
"_index" : "enwiki_latest",
"_type" : "_doc",
"_id" : "MYItaXQBb3qqttZ1hybj",
"_score" : 15.031863,
"_from" : "Rebic",
"_source" : {
"id" : ""Q68961742""
}
},
{
"_index" : "enwiki_latest",
"_type" : "_doc",
"_id" : "yH8iaXQBb3qqttZ1-muk",
"_score" : 14.735747,
"_from" : "toto",
"_source" : {
"id" : ""Q4118198""
}
},
{
"_index" : "enwiki_latest",
"_type" : "_doc",
"_id" : "YX8iaXQBb3qqttZ1Z0DN",
"_score" : 2.8115559,
"_from" : "toto",
"_source" : {
"id" : ""Q192891""
}
}
]
}
}

Have a look at https://www.elastic.co/guide/en/elasticsearch/reference/7.9/query-dsl-bool-query.html#named-queries

2 Likes

perfect !

QUERY

curl -X GET "localhost:9200/_search?pretty" -H 'Content-Type: application/json' -d'
{
    "_source": [ "labels", "id" ],
    "query" : {
        "bool": {
            "should": [
		{ "match": { "labels": { "query": "titi", "_name": "titi_name" } } },
        	{ "match": { "labels": { "query": "toto", "_name": "toto_name" } } }
            ]
        }
    }
}' 

RESPONSE

  {
    "_index" : "enwiki_latest",
    "_type" : "_doc",
    "_id" : "xXwWaXQBb3qqttZ1ekxZ",
    "_score" : 21.006222,
    "_source" : {
      "id" : "\"Q31557634\"",
      "labels" : "{\"ceb\": {\"language\": \"ceb\", \"value\": \"Titi Swamp\"}, \"en\": {\"language\": \"en\", \"value\": \"Titi Swamp\"}}"
    },
    "matched_queries" : [
      "titi_name"
    ]
  },
  {
    "_index" : "enwiki_latest",
    "_type" : "_doc",
    "_id" : "a4AkaXQBb3qqttZ1uSNW",
    "_score" : 20.85733,
    "_source" : {
      "id" : "\"Q22322231\"",
      "labels" : "{\"fr\": {\"language\": \"fr\", \"value\": \"Toto\"}, \"en\": {\"language\": \"en\", \"value\": \"Toto\"}, \"es\": {\"language\": \"es\", \"value\": \"Toto\"}}"
    },
    "matched_queries" : [
      "toto_name"
    ]
  },
1 Like