Reputation: 1154
i'am trying to search a string on all field of an index :
Here is my request :
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'}}})
how to search the string aavril on all field ?
Regards
Upvotes: 0
Views: 406
Reputation: 29345
a few different ways:
using multimatch:
{
"query": {
"multi_match" : {
"query": "aavril",
"fields": [ "PRODUCT_NAME", "BRAND_NAME" ]
}
}
}
using bool:
{
"query": {
"bool" : {
"should" : [
{ "match" : { "PRODUCT_NAME" : "aavril" } },
{ "match" : { "BRAND_NAME" : "aavril" } }
]
}
}
}
or may preferred method, at index time, create a field that concatenates all the fields you want to search against and then a simple match against that field.
Upvotes: 1