0

我正在尝试按弹性搜索数据库中不存在的字段进行过滤,这是我的字段:

{
        "_index" : "catalogue",
        "_type" : "_doc",
        "_id" : "0lzS2HEB-X5mOkOYpdV9",
        "_score" : 1.0,
        "_source" : {
          "nom" : "Junk Resin Necklace",
          "categorie" : "Jewellery",
          "image" : "http://img6a.flixcart.com/image/necklace-chain/f/7/g/junk-n-055-junk-necklace-original-imae4cgmqcz4hzzx.jpeg",
          "marque" : "Junk"
        }

这是我基于 NLU 输出的 json 文件:

{
  "intent": {
    "name": "Bonbons",
    "confidence": 0.9993242025375366
  },
  "entities": [
    {
      "entity": "gout",
      "start": 26,
      "end": 39,
      "extractor": "DIETClassifier",
      "value": "Eucalyptus"
    },
    {
      "entity": "marque",
      "start": 40,
      "end": 47,
      "extractor": "DIETClassifier",
      "value": "RICOLA",
      "processors": [
        "EntitySynonymMapper"
      ]
    }
  ],
  "intent_ranking": [
    {
      "name": "Bonbons",
      "confidence": 0.9993242025375366
    },
    {
      "name": "greet",
      "confidence": 0.0006757942610420287
    }
  ],
  "response_selector": {
    "default": {
      "response": {
        "name": null,
        "confidence": 0.0
      },
      "ranking": [],
      "full_retrieval_intent": null
    }
  },
  "text": "je veux acheter un bonbon cerise & cola TIC TAC"
}

我试过这个:

es=Elasticsearch([{'host':'localhost','port':'9200'}])
variable=json_files['entities']
text=json_files['text']
entities=[]
for i in variable:
    query_search={'query':{'match':{i['entity']:i['value']}}}
    entities.append(i['entity'])
    if(("marque" in entities) and ("categorie" in entities) and ("nom" in entities) and ("image" in entities)):
        if(i['entity']=="marque"):
            res = es.search(index="catalogue", body=query_search)
            print(res)
        elif(i["entity"]=="nom"):
            res = es.search(index="catalogue", body=query_search)
            print(res)
        elif(i["entity"]=="categorie"):
            res = es.search(index="catalogue", body=query_search)
            print(res)
        elif(i["entity"]=="image"):
            res = es.search(index="catalogue", body=query_search)
            print(res)
        else:
            print("*******")
    elif(("marque" in entities) or ("categorie" in entities) or ("nom" in entities)):
        if(i['entity']=="marque" and i['entity']!="nom" and i['entity']!="categorie" and i['entity']!="image"):
            res = es.search(index="catalogue", body=query_search)
            print(res)
        elif(i["entity"]=="nom" and i['entity']!="marque" and i['entity']!="categorie" and i['entity']!="image"):
            res = es.search(index="catalogue", body=query_search)
            print(res)
        elif(i["entity"]=="categorie" and i['entity']!="nom" and i['entity']!="marque" and i['entity']!="image"):
            res = es.search(index="catalogue", body=query_search)
            print(res)
        elif(i["entity"]=="image" and i['entity']!="nom" and i['entity']!="categorie" and i['entity']!="marque"):
            res = es.search(index="catalogue", body=query_search)
            print(res)
        else:
            res = es.search(index="catalogue", body=query_search)
            print(res)
    else:
        res = es.search(index="catalogue", body=query_search)
        print(res)

结果是:

{
   "took":13,
   "timed_out":False,
   "_shards":{
      "total":1,
      "successful":1,
      "skipped":0,
      "failed":0
   },
   "hits":{
      "total":{
         "value":0,
         "relation":"eq"
      },
      "max_score":"None",
      "hits":[

      ]
   }
}{
   "took":460,
   "timed_out":False,
   "_shards":{
      "total":1,
      "successful":1,
      "skipped":0,
      "failed":0
   },
   "hits":{
      "total":{
         "value":8,
         "relation":"eq"
      },
      "max_score":8.775399,
      "hits":[
         {
            "_index":"catalogue",
            "_type":"_doc",
            "_id":"pV3S2HEB-X5mOkOYwgRP",
            "_score":8.775399,
            "_source":{
               "nom":"Sony HT-GT1 2.1 Home Theatre System",
               "categorie":"Home Entertainment",
               "image":"http://img6a.flixcart.com/image/home-theatre-system/h/x/z/ht-gt1-sony-original-imaefyb3m3smdcau.jpeg",
               "marque":"Sony"
            }
         },
         {
            "_index":"catalogue",
            "_type":"_doc",
            "_id":"fFzS2HEB-X5mOkOYjryo",
            "_score":8.775399,
            "_source":{
               "nom":"Haut-parleur sans fil Sony SRSHG1/BLK Hi-Res - Noir anthracite",
               "categorie":"haut-parleurs Bluetooth et  sans fil",
               "image":"No image",
               "marque":"Sony"
            }
         },
         {
            "_index":"catalogue",
            "_type":"_doc",
            "_id":"flzS2HEB-X5mOkOYjryo",
            "_score":8.775399,
            "_source":{
               "nom":"Mini-cassettes vidéo numériques Sony - DVC - 1 heure",
               "categorie":"Accessoires appareil photo",
               "image":"No image",
               "marque":"Sony"
            }
         },
         {
            "_index":"catalogue",
            "_type":"_doc",
            "_id":"f1zS2HEB-X5mOkOYjryo",
            "_score":8.775399,
            "_source":{
               "nom":"Haut-parleur sans fil SRS-ZR7",
               "categorie":"haut-parleurs Bluetooth et  sans fil",
               "image":"No image",
               "marque":"Sony"
            }
         },
         {
            "_index":"catalogue",
            "_type":"_doc",
            "_id":"i1zS2HEB-X5mOkOYjryo",
            "_score":8.775399,
            "_source":{
               "nom":"Facade autoradio CD marin Sony MEXM100BT 160W RMS avec Bluetooth (noir) et SiriusXM Ready",
               "categorie":"Accessoires électroniques pour voiture",
               "image":"No image",
               "marque":"Sony"
            }
         },
         {
            "_index":"catalogue",
            "_type":"_doc",
            "_id":"jFzS2HEB-X5mOkOYjryo",
            "_score":8.775399,
            "_source":{
               "nom":"Haut-parleur portable sans fil Sony SRSXB30/BLK XB30 avec Bluetooth",
               "categorie":"haut-parleurs Bluetooth et  sans fil",
               "image":"No image",
               "marque":"Sony"
            }
         },
         {
            "_index":"catalogue",
            "_type":"_doc",
            "_id":"jVzS2HEB-X5mOkOYjryo",
            "_score":8.775399,
            "_source":{
               "nom":"Mini-système LBT-GPX555 de Sony avec Bluetooth et NFC",
               "categorie":"Accessoires audio et vidéo",
               "image":"No image",
               "marque":"Sony"
            }
         },
         {
            "_index":"catalogue",
            "_type":"_doc",
            "_id":"LVzS2HEB-X5mOkOYsOgL",
            "_score":8.775399,
            "_source":{
               "nom":"Sony Nh-Aa-B4gn Rechargeable Ni-MH Battery",
               "categorie":"Cameras & Accessories",
               "image":"http://img6a.flixcart.com/image/rechargeable-battery/rechargeable-ni-mh-battery/y/3/j/sony-nh-aa-b4gn-1100x1100-imaeebadghrwfaa8.jpeg",
               "marque":"Sony"
            }
         }
      ]
   }
}{
   "took":0,
   "timed_out":False,
   "_shards":{
      "total":1,
      "successful":1,
      "skipped":0,
      "failed":0
   },
   "hits":{
      "total":{
         "value":0,
         "relation":"eq"
      },
      "max_score":"None",
      "hits":[

      ]
   }
}{
   "took":0,
   "timed_out":False,
   "_shards":{
      "total":1,
      "successful":1,
      "skipped":0,
      "failed":0
   },
   "hits":{
      "total":{
         "value":0,
         "relation":"eq"
      },
      "max_score":"None",
      "hits":[

      ]
   }
}

如何使用我的 elasticsearch 数据库中不存在的字段(例如:gout)过滤我的查询并得到准确的“bonbon cerise & cola TIC TAC”作为结果?

4

0 回答 0