Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
212 views
in Technique[技术] by (71.8m points)

Is there a way to track how often an entity is getting hit in elasticsearch?

I want to keep track of how often an entity is being hit by an elasticsearch hit. For example, let's say I had an index like this:

{
    "company":"google"
},
{
    "company":"amazon"
},
{
    "company":"goodyear"
}

and I do a search query like "goo" that returns google and goodyear. Is there a way I can keep track over time of how often an entity is getting hit? Something like

{
    "company":"google",
    "tracking_num_hits": 1
},
{
    "company":"amazon",
    "tracking_num_hits": 0
},
{
    "company":"goodyear",
    "tracking_num_hits": 1
}

and if I then search "amazon" it updates to

{
    "company":"google",
    "tracking_num_hits": 1
},
{
    "company":"amazon",
    "tracking_num_hits": 1
},
{
    "company":"goodyear",
    "tracking_num_hits": 1
}

Is there a way for elasticsearch to do this, and if not what is the best way to track this on the backend?

question from:https://stackoverflow.com/questions/65836120/is-there-a-way-to-track-how-often-an-entity-is-getting-hit-in-elasticsearch

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You'll have to increment these tracking markers yourself, perhaps with an _update_by_query call as soon as you've obtained the hits:

POST myindex/_update_by_query
{
  "query": {
    "terms": {
      "company.keyword": [
        "google",
        "goodyear"
      ]
    }
  },
  "script": {
    "source": "if (ctx._source.tracking_num_hits != null) { ctx._source.tracking_num_hits++ } else { ctx._source.tracking_num_hits = 1 }"
  }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...