SoFunction
Updated on 2024-10-29

python+elasticsearch implementation of tag match counting operations

Given a set of tags [{"tag_id": "1", "value": "watermelon"} , {"tag_id": "1", "value": "apple"}], I want to precisely match to the tags present in the existing tag library and record the number of successful matches.

Tag id(tag_id) Tag name (tag_name) Tag value (tag_name )
1 fruit cantaloupe
1 fruit pomegranate
1 fruit orange
2 wildlife tigers

This step requires the and operation in sql, ie:

The must condition in es

{
  "query": {
    "bool": {
      "must": [
          {
            "term": {
              "Condition 1":  "ok"
            }
          },
          {
            "term": {
              "Condition 2":  123
            }
          }
        ]
    }
  }
}

To satisfy both condition 1, condition 2 this query will have results. The term inside means precise query.

This step requires the OR operation in sql, ie:

The should condition in es

{
  "query": {
    "bool": {
      "should": [
        {
          "match": {
            "Condition 1": "ok"
          }
        },
        {
          "match": {
            "Condition 2": "666"
          }
        }
      ]
    }
  }
}

Satisfy condition 1, condition 2 any one of the query will have results. The match in it indicates a fuzzy query.

consult (a document etc)

I need to query given this set of tags [{"tag_id": "1", "value": "watermelon "}, {"tag_id": "1", "value": "apple "}], the number of occurrences in the existing tag library, which requires both the AND relationship of tag_id and value and the OR relationship of the outer layer, the query is as follows

# Execute queries
query_terms = [{"tag_id": "1", "value": "Watermelon."}, {"tag_id": "1", "value": "Apples."}]
query = {
    "query": {
        "bool": {
            "should": [
                        {"bool": {
                                "must": [
                                            {
                                                "term": {
                                                "value":  term['value']
                                                }
                                            },
                                            {
                                                "term": {
                                                "tag_id":  term['tag_id']
                                                }
                                            }
                                            ]
                            }} for term in query_terms
            ]
        }
    }
}

Results of library searches

# Execute queries and output results
search_result = (index=index_name, body=query)
num_matches = search_result["hits"]["total"]["value"]  
print(num_matches)
if search_result["hits"]["total"]["value"] == 0:
    print("No matching results. Query Condition:", query_terms)
else:
    print("Query Result:")
    for hit in search_result["hits"]["hits"]:
        print("ID:", hit["_id"], "Score:", hit["_score"], "Data:", hit["_source"])

to this article on python + elasticsearch tag matching counting operation is introduced to this article, more related python elasticsearch counting content please search for my previous posts or continue to browse the following related articles I hope you will support me in the future more!