亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

python+elasticsearch實(shí)現(xiàn)標(biāo)簽匹配計(jì)數(shù)操作

 更新時(shí)間:2024年04月23日 11:33:00   作者:P-ShineBeam  
這篇文章主要介紹了python+elasticsearch實(shí)現(xiàn)標(biāo)簽匹配計(jì)數(shù)操作,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下

給定一組標(biāo)簽 [{“tag_id”: “1”, “value”: “西瓜”}, {“tag_id”: “1”, “value”: “蘋果”}],我想精準(zhǔn)匹配到現(xiàn)有的標(biāo)簽庫中存在的標(biāo)簽并記錄匹配成功的數(shù)量。

標(biāo)簽id(tag_id)標(biāo)簽名(tag_name)標(biāo)簽值(tag_name )
1水果西瓜
1水果蘋果
1水果橙子
2動(dòng)物老虎

這個(gè)步驟需要sql中的and操作,即:

es中的must條件

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

要同時(shí)滿足條件1,條件2這個(gè)查詢才會(huì)有結(jié)果。里面的term表示精準(zhǔn)查詢。

這個(gè)步驟需要sql中的or操作,即:

es中的should條件

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

滿足條件1,條件2任意一個(gè)查詢都會(huì)有結(jié)果。里面的match表示模糊查詢。

查詢

我需要查詢給定這組標(biāo)簽 [{“tag_id”: “1”, “value”: “西瓜”}, {“tag_id”: “1”, “value”: “蘋果”}],在現(xiàn)有的標(biāo)簽庫出現(xiàn)的次數(shù),這既需要tag_id和value的and關(guān)系,又需要外層的or關(guān)系,查詢的語句如下

# 執(zhí)行查詢
query_terms = [{"tag_id": "1", "value": "西瓜"}, {"tag_id": "1", "value": "蘋果"}]
query = {
    "query": {
        "bool": {
            "should": [
                        {"bool": {
                                "must": [
                                            {
                                                "term": {
                                                "value":  term['value']
                                                }
                                            },
                                            {
                                                "term": {
                                                "tag_id":  term['tag_id']
                                                }
                                            }
                                            ]
                            }} for term in query_terms
            ]
        }
    }
}

查庫結(jié)果

# 執(zhí)行查詢并輸出結(jié)果
search_result = es.search(index=index_name, body=query)
num_matches = search_result["hits"]["total"]["value"]  
print(num_matches)
if search_result["hits"]["total"]["value"] == 0:
    print("沒有匹配的結(jié)果。查詢條件:", query_terms)
else:
    print("查詢結(jié)果:")
    for hit in search_result["hits"]["hits"]:
        print("ID:", hit["_id"], "Score:", hit["_score"], "Data:", hit["_source"])

到此這篇關(guān)于python+elasticsearch實(shí)現(xiàn)標(biāo)簽匹配計(jì)數(shù)操作的文章就介紹到這了,更多相關(guān)python elasticsearch計(jì)數(shù)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論