Cherri Smack
Cherri Smack

Reputation: 105

Exact match Query in Elastic Search issue

I have a index in ElasticSearch with 4 datas

Here's the Data:

{
  "took": 1,
  "timed_out": false,
  "_shards": {
    "total": 1,
    "successful": 1,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": {
      "value": 4,
      "relation": "eq"
    },
    "max_score": 1,
    "hits": [
      {
        "_index": "sample4",
        "_type": "logs",
        "_id": "UQBMOHABHstawU4w4_z3",
        "_score": 1,
        "_source": {
          "date": "2020-02-12T07:28:48",
          "target": {
            "http://localhost/wordpress/index.php/2020/01/13/hello-world/": {
              "clicks": {
                "868 278": 12
              }
            }
          }
        }
      },
      {
        "_index": "sample4",
        "_type": "logs",
        "_id": "UgBNOHABHstawU4wT_wn",
        "_score": 1,
        "_source": {
          "date": "2020-02-12T07:29:15",
          "target": {
            "http://localhost/wordpress/": {
              "clicks": {
                "958 250": 5
              }
            }
          }
        }
      },
      {
        "_index": "sample4",
        "_type": "logs",
        "_id": "UABMOHABHstawU4wC_y9",
        "_score": 1,
        "_source": {
          "date": "2020-02-12T07:27:52",
          "target": {
            "http://localhost/wordpress/": {
              "clicks": {
                "880 257": 6
              }
            }
          }
        }
      },
      {
        "_index": "sample4",
        "_type": "logs",
        "_id": "UwBOOHABHstawU4wFvxV",
        "_score": 1,
        "_source": {
          "date": "2020-02-12T07:30:06",
          "target": {
            "http://localhost/wordpress/index.php/2020/01/13/hello-world/": {
              "clicks": {
                "389 60": 33
              }
            },
            "http://localhost/wordpress/": {
              "clicks": {
                "657 235": 8
              }
            }
          }
        }
      }
    ]
  }
}

I want to match the target key in the index with the value http://localhost/wordpress/. If the given value exactly matches the value in target key in ES index, I would get 3 data. Inside the target key, it was like an object. So i don't know how make a match query.

Here's the query i tried:

{
    "query": {
        "wildcard": {
            "target.http://localhost/wordpress/": {
                "value": "*"
            }
        }
    }
}

But it returns 0 results.
Output I got:

  {
      "took": 1,
      "timed_out": false,
      "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
      },
      "hits": {
        "total": {
          "value": 0,
          "relation": "eq"
        },
        "max_score": null,
        "hits": []
      }
    }

Required Output:

{
  "took": 1,
  "timed_out": false,
  "_shards": {
    "total": 1,
    "successful": 1,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": {
      "value": 3,
      "relation": "eq"
    },
    "max_score": 1,
    "hits": [
      {
        "_index": "sample4",
        "_type": "logs",
        "_id": "UgBNOHABHstawU4wT_wn",
        "_score": 1,
        "_source": {
          "date": "2020-02-12T07:29:15",
          "target": {
            "http://localhost/wordpress/": {
              "clicks": {
                "958 250": 5
              }
            }
          }
        }
      },
      {
        "_index": "sample4",
        "_type": "logs",
        "_id": "UABMOHABHstawU4wC_y9",
        "_score": 1,
        "_source": {
          "date": "2020-02-12T07:27:52",
          "target": {
            "http://localhost/wordpress/": {
              "clicks": {
                "880 257": 6
              }
            }
          }
        }
      },
      {
        "_index": "sample4",
        "_type": "logs",
        "_id": "UwBOOHABHstawU4wFvxV",
        "_score": 1,
        "_source": {
          "date": "2020-02-12T07:30:06",
          "target": {
            "http://localhost/wordpress/index.php/2020/01/13/hello-world/": {
              "clicks": {
                "389 60": 33
              }
            },
            "http://localhost/wordpress/": {
              "clicks": {
                "657 235": 8
              }
            }
          }
        }
      }
    ]
  }
}

Help me to solve this problem.....

Upvotes: 0

Views: 60

Answers (1)

Val
Val

Reputation: 217254

Since you're checking on a field name and not a value, you should try this query instead

{
    "query": {
        "exists": {
            "field": "target.http://localhost/wordpress/"
        }
    }
}

Upvotes: 3

Related Questions