简体   繁体   English

在elasticsearch上使用python在所有字段上进行搜索

[英]make a search on all field with python on elasticsearch

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 ? 如何在所有字段上搜索字符串aavril?

Regards 问候

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. 或者可能是首选方法,在建立索引时,创建一个字段,该字段将要搜索的所有字段连接起来,然后对该字段进行简单匹配。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM