[英]make a search on all field with python on elasticsearch
我正在嘗試在索引的所有字段上搜索字符串:
這是我的要求:
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?
問候
幾種不同的方式:
使用多重匹配:
{
"query": {
"multi_match" : {
"query": "aavril",
"fields": [ "PRODUCT_NAME", "BRAND_NAME" ]
}
}
}
使用布爾:
{
"query": {
"bool" : {
"should" : [
{ "match" : { "PRODUCT_NAME" : "aavril" } },
{ "match" : { "BRAND_NAME" : "aavril" } }
]
}
}
}
或者可能是首選方法,在建立索引時,創建一個字段,該字段將要搜索的所有字段連接起來,然后對該字段進行簡單匹配。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.