2015-02-17 6 views
7

नीचे दी गई क्वेरी वह है जिसे मैं elasticsearch-dsl-py का उपयोग करके बनाना चाहता हूं, लेकिन मुझे नहीं पता कि यह कैसे करें।मैं elasticsearch-dsl-py का उपयोग करके "OR" फ़िल्टर कैसे बना सकता हूं?

GET /my_index/_search 
{ 
    "query": { 
    "filtered": { 
     "filter": { 
     "bool": { 
      "must": [ 
      { 
       "term": { 
       "status": "published" 
       } 
      }, 
      { 
       "or": { 
       "filters": [ 
        { 
        "range": { 
         "start_publication": { 
         "lte": "2015-02-17T03:45:00.245012+00:00" 
         } 
        } 
        }, 
        { 
        "missing": { 
         "field": "start_publication" 
        } 
        } 
       ] 
       } 
      }, 
      { 
       "or":{ 
       "filters": [ 
        { 
        "range": { 
         "end_publication": { 
         "gte": "2015-02-17T03:45:00.245012+00:00" 
         } 
        } 
        }, 
        { 
        "missing": { 
         "field": "end_publication" 
        } 
        } 
       ] 
       } 
      } 
      ] 
     } 
     } 
    } 
    } 
} 

elasticsearch-DSL-py का उपयोग करना, इस के रूप में मैं प्राप्त कर सकते हैं के रूप में करीब है, लेकिन यह एक ही नहीं है। '|' ऑपरेटर 'OR' के बजाय 'चाहिए' खंड में बदल जाता है।

client = Elasticsearch() 
    now = timezone.now() 

    s = Search(using=client, 
       index="my_index" 
     ).filter(
      "term", status=PUBLISHED 
     ).filter(
      F("range", start_publication={"lte": now},) | 
      F("missing", field="start_publication") 
     ).filter(
      F("range", end_publication={"gte": now},) | 
      F("missing", field="end_publication") 
     ) 
    response = s.execute() 

उत्तर

9

समाधान:

s = Search(using=client, 
      index="my_index" 
    ).filter(
     "term", status=PUBLISHED 
    ).filter(
     "or", [F("range", start_publication={"lte": now},), 
       F("missing", field="start_publication")] 
    ).filter(
     "or", [F("range", end_publication={"gte": now},), 
       F("missing", field="end_publication")] 
    ) 

कौन सा में बदल जाता है:

{ 
    "query":{ 
     "filtered":{ 
     "filter":{ 
      "bool":{ 
       "must":[ 
        { 
        "term":{ 
         "status":"published" 
        } 
        }, 
        { 
        "or":{ 
         "filters":[ 
          { 
           "range":{ 
           "start_publication":{ 
            "lte":"2015-02-17T03:45:00.245012+00:00" 
           } 
           } 
          }, 
          { 
           "missing":{ 
           "field":"start_publication" 
           } 
          } 
         ] 
        } 
        }, 
        { 
        "or":{ 
         "filters":[ 
          { 
           "range":{ 
           "end_publication":{ 
            "gte":"2015-02-17T03:45:00.245012+00:00" 
           } 
           } 
          }, 
          { 
           "missing":{ 
           "field":"end_publication" 
           } 
          } 
         ] 
        } 
        } 
       ] 
      } 
     }, 
     "query":{ 
      "match_all":{ 

      } 
     } 
     } 
    } 
} 

उम्मीद है कि यह भविष्य में elasticsearch-DSL-py दस्तावेज में शामिल किया जा सकता।

+1

यह समाधान पुराना है। एफ अब मौजूद नहीं है और फ़िल्टरिंग के लिए वाक्यविन्यास बदल गया है। – Michael

2

Elasticsearch 2.x (और elasticsearch-dsl> 2.x) के साथ आप अब @ theslow1 की टिप्पणी के रूप में फ़िल्टर लागू नहीं कर सकते हैं। इसके बजाय आप Q रों के संयोजन के द्वारा अपने फ़िल्टर बनाने के लिए है:

search = Search(using=esclient, index="myIndex") 
firstFilter = Q("match", color='blue') & Q("match", status='published') 
secondFilter = Q("match", color='yellow') & Q("match", author='John Doe') 
combinedFilter = firstFilter | secondFilter 
search.query('bool', filter=[combinedFilter]) 

search.query('bool', filter=[combinedQ])elasticsearch-dsl documentation में वर्णित के रूप फिल्टर के रूप में लागू होता है क्यू मापदंड।

+0

संयुक्त एसएफ, स्कोर फ़ंक्शन का उपयोग कैसे करें। – devanathan

+0

मेरा एसएफ दिया गया इनपुट के आकार के आधार पर गतिशील होने जा रहा है। मैं कार्यों को उत्पन्न करने के लिए लूप के लिए उपयोग करने जा रहा हूं। – devanathan

संबंधित मुद्दे

 संबंधित मुद्दे