使用elasticsearch上的python搜索所有字段

2024-04-27 00:19:27 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试搜索索引的所有字段上的字符串:

这是我的要求:

from elasticsearch import Elasticsearch
from pprint import pprint
# Connect to the elastic cluster
auth = ('Toto', 'Titi')

es=Elasticsearch("https://192.168.1.92:9200",verify_certs=False,http_auth=auth)

e1={'BARCODE': '366221700417', 'BRAND_MEDALS': ['Transparency'], 'BRAND_NAME': 'Avril', 'COMPOSITION': [{'INGREDIENT_GRADE': 'good', 'INGREDIENT_NAME': 'Aqua'}, {'INGREDIENT_GRADE': 'good', 'INGREDIENT_NAME': 'Citrus Aurantium Amara Water'}, {'INGREDIENT_GRADE': 'neutral', 'INGREDIENT_NAME': 'Sodium Cocoyl Glutamate'}, {'INGREDIENT_GRADE': 'allergene', 'INGREDIENT_NAME': 'Cocamidopropyl Betaine'}, {'INGREDIENT_GRADE': 'good', 'INGREDIENT_NAME': 'Glycerin'}, {'INGREDIENT_GRADE': 'neutral', 'INGREDIENT_NAME': 'Polyglyceryl-4 Caprate'}, {'INGREDIENT_GRADE': 'neutral', 'INGREDIENT_NAME': 'Sodium Chloride'}, {'INGREDIENT_GRADE': 'neutral', 'INGREDIENT_NAME': 'Xanthan Gum'}, {'INGREDIENT_GRADE': 'good', 'INGREDIENT_NAME': 'Aloe Barbadensis Leaf Juice Powder'}, {'INGREDIENT_GRADE': 'neutral', 'INGREDIENT_NAME': 'Sodium Benzoate'}, {'INGREDIENT_GRADE': 'allergene', 'INGREDIENT_NAME': 'Levulinic Acid'}, {'INGREDIENT_GRADE': 'neutral', 'INGREDIENT_NAME': 'Citric Acid'}, {'INGREDIENT_GRADE': 'neutral', 'INGREDIENT_NAME': 'Sodium Levulinate'}], 'DATASOURCE': 'avril 2019_02', 'FRONT_PICTURE': 'https://koalabourre.s3-eu-west-1.amazonaws.com/default_pic_76d3ec7a-f9f4-4c20-ba32-213e6ed02894.jpg', 'OVERVIEW': {'Allergene': 2, 'Avoid': 0, 'Danger': 0, 'Good': 4, 'Neutral': 7, 'Unknown': 0}, 'PRODUCT_CATEGORY': 'Soin Du Corps', 'PRODUCT_NAME': 'Base Lavante Neutre 2040 Ml - Certifiée Bio', 'PRODUCT_USAGE': ''}

res = es.index(index='product_json',doc_type='product',id=e1["BARCODE"],body=e1)

res= es.search(index='product_json',body={'query':{'match':{'BRAND_NAME':'avril'}}})

print(res)

res= es.search(index='product_json',body={'query':{'match':{"PRODUCT_NAME":'aavril'}}})

print(res)

res= es.search(index='product_json',body={'query':{'match':{"all":'aavril'}}})

print(res)

res= es.search(index='product_json',body={'query':{'multi_match': {'query': 'aavril'}}})

如何在所有字段上搜索字符串aavril?你知道吗

敬礼


Tags: namejsonsearchindexesbodyresproduct
1条回答
网友
1楼 · 发布于 2024-04-27 00:19:27

有几种不同的方法:

使用多重匹配:

{
  "query": {
    "multi_match" : {
      "query":    "aavril", 
      "fields": [ "PRODUCT_NAME", "BRAND_NAME" ] 
    }
  }
}

使用bool:

{
  "query": {
    "bool" : {
      "should" : [
        { "match" : { "PRODUCT_NAME" : "aavril" } },
        { "match" : { "BRAND_NAME" : "aavril" } }
      ]
    }
  }
}

或者首选方法,在索引时,创建一个字段,将要搜索的所有字段串联起来,然后对该字段进行简单匹配。你知道吗

相关问题 更多 >