eablex
eablex

Reputation: 59

How do i write a search query in elastic when the field name is unknown

enter image description here

I want to use the elastic search to query documents who has the matching values of "dc883c6f24776ad6ce1f86c41b5cf87cfb784e85".

Please find the structure of the source document attached in the image. Is it possible to query this with something like below using *[wild char] when the field name is unknown for us.

 "query" : {
    "constant_score" : {
        "filter" : {
            "terms" : { 
                "commitId.persistence-statics-service.*":["dc883c6f24776ad6ce1f86c41b5cf87cfb784e85"]
            }
        }
    }
}

Upvotes: 2

Views: 2127

Answers (2)

Vijay
Vijay

Reputation: 5010

Try to Use query_string . Its very powerful in partial search in ES ,check my answer link :-

{
  "query": {
    "query_string": {
       "fields" : ["commitId.persistence-statics-service.*"] ,
      "query": "*dc883c6f24776ad6ce1f86c41b5cf87cfb784e85*"
    }
  }
}

Query_string is more powerful than multi_match link2

Upvotes: 2

Andrei Stefan
Andrei Stefan

Reputation: 52368

You can use query_string or multi_match to specify a wildcard in the field name part. I think the multi_match is simpler though:

{
  "query": {
    "constant_score": {
      "filter": {
        "multi_match": {
          "query": "dc883c6f24776ad6ce1f86c41b5cf87cfb784e85",
          "fields": [
            "commitId.persistence-statics-service.*"
          ],
          "analyzer": "keyword"
        }
      }
    }
  }
}

Upvotes: 2

Related Questions