2017-01-28 2 views
0

マイ状況:ElasticSearchが適用されるべきであるとの範囲

私はElasticSearchデータベースで働いていると私は、「論理和」プラス「論理積」のカップルのカップルを適用するカント。私は私が望むものを表示するためにSQLクエリを書いています。私はconfirmedPlayerspendingPlayersを配列として使っていますが、もちろん私はそれをSQLで行うことはできませんが、例を取りたいと思います。

あなたが私のマッピングを追加したいなら、私はそれを広げたいと思うだけです。

これはSQLで私のクエリです:

SELECT * 
    FROM match 
WHERE (
     "AVnJOMvXOX1s7Ny2Wu9O" in confirmedPlayers OR 
     "AVnJOMvXOX1s7Ny2Wu9O" in pendingPlayers OR 
     "AVnJOMvXOX1s7Ny2Wu9O" = creator 
     ) 
    AND date >= "20/01/2016" 
    /* AND other filter will be added */ 

これは私のマッチタイプの情報です:

{ 
    "took": 79, 
    "timed_out": false, 
    "_shards": { 
     "total": 5, 
     "successful": 5, 
     "failed": 0 
    }, 
    "hits": { 
     "total": 8, 
     "max_score": 1, 
     "hits": [ 
     { 
      "_index": "yojuego", 
      "_type": "match", 
      "_id": "AVmak0bWIjogo0aNpbGs", 
      "_score": 1, 
      "_source": { 
       "title": "Mi primer match", 
       "date": "2016-01-13T20:31:20.000Z", 
       "fromTime": "19:00", 
       "toTime": "20:00", 
       "location": "casa de pablo", 
       "creator": "AVmabq-5Ijogo0aNpbGn", 
       "matchType": "5", 
       "confirmedPlayers": [], 
       "pendingPlayers": [], 
       "comments": [] 
      } 
     }, 
     { 
      "_index": "yojuego", 
      "_type": "match", 
      "_id": "AVm0ETbT0Y26YggShbFa", 
      "_score": 1, 
      "_source": { 
       "title": "Mi primer match", 
       "date": "2016-01-13T20:31:20.000Z", 
       "fromTime": "19:00", 
       "toTime": "20:00", 
       "location": "casa de pablo", 
       "creator": "AVmabVjUIjogo0aNpbGm", 
       "matchType": "5", 
       "confirmedPlayers": [], 
       "pendingPlayers": [ 
        "AVmBKi21XRKVuACJGZZZ", 
        "AVmabq-5Ijogo0aNpbGn" 
       ], 
       "comments": [] 
      } 
     }, 
     { 
      "_index": "yojuego", 
      "_type": "match", 
      "_id": "AVmab1G5Ijogo0aNpbGo", 
      "_score": 1, 
      "_source": { 
       "title": "Mi primer match", 
       "date": "2016-01-13T20:31:20.000Z", 
       "fromTime": "19:00", 
       "toTime": "20:00", 
       "location": "casa de pablo", 
       "creator": "AVmabVjUIjogo0aNpbGm", 
       "matchType": "5", 
       "confirmedPlayers": [ 
        "AVmabVjUIjogo0aNpbGm", 
        "AVmBKi21XRKVuACJGZZZ" 
       ], 
       "pendingPlayers": [ 
        "AVmBKi21XRKVuACJGZZZ" 
       ], 
       "comments": [] 
      } 
     }, 
     { 
      "_index": "yojuego", 
      "_type": "match", 
      "_id": "AVm0EPX20Y26YggShbFZ", 
      "_score": 1, 
      "_source": { 
       "title": "Mi primer match", 
       "date": "2016-01-13T20:31:20.000Z", 
       "fromTime": "19:00", 
       "toTime": "20:00", 
       "location": "casa de pablo", 
       "creator": "AVmabVjUIjogo0aNpbGm", 
       "matchType": "5", 
       "confirmedPlayers": [ 
        "AVmabVjUIjogo0aNpbGm", 
        "AVmabq-5Ijogo0aNpbGn" 
       ], 
       "pendingPlayers": [ 
        "AVmBKi21XRKVuACJGZZZ" 
       ], 
       "comments": [] 
      } 
     }, 
     { 
      "_index": "yojuego", 
      "_type": "match", 
      "_id": "match", 
      "_score": 1, 
      "_source": { 
       "title": "otro match 3", 
       "date": "2017-12-28T00:00:00.000Z", 
       "fromTime": "21:00", 
       "toTime": "22:00", 
       "location": "somewhere", 
       "creator": "AVnJOMvXOX1s7Ny2Wu9O", 
       "matchType": "5", 
       "confirmedPlayers": [], 
       "pendingPlayers": [], 
       "comments": [] 
      } 
     }, 
     { 
      "_index": "yojuego", 
      "_type": "match", 
      "_id": "AVnm-9fOJxj9yxI50RS3", 
      "_score": 1, 
      "_source": { 
       "title": "otro match 3", 
       "date": "2017-12-28T00:00:00.000Z", 
       "fromTime": "21:00", 
       "toTime": "22:00", 
       "location": "somewhere", 
       "creator": "AVmabVjUIjogo0aNpbGm", 
       "matchType": "5", 
       "confirmedPlayers": [], 
       "pendingPlayers": [ 
        "AVnJOMvXOX1s7Ny2Wu9O" 
       ], 
       "comments": [] 
      } 
     }, 
     { 
      "_index": "yojuego", 
      "_type": "match", 
      "_id": "AVnm-ykMJxj9yxI50RS1", 
      "_score": 1, 
      "_source": { 
       "title": "otro match 3", 
       "date": "2017-12-28T00:00:00.000Z", 
       "fromTime": "21:00", 
       "toTime": "22:00", 
       "location": "somewhere", 
       "creator": "AVnJOMvXOX1s7Ny2Wu9O", 
       "matchType": "5", 
       "confirmedPlayers": [], 
       "pendingPlayers": [], 
       "comments": [] 
      } 
     }, 
     { 
      "_index": "yojuego", 
      "_type": "match", 
      "_id": "AVnm-73OJxj9yxI50RS2", 
      "_score": 1, 
      "_source": { 
       "title": "otro match 3", 
       "date": "2017-12-28T00:00:00.000Z", 
       "fromTime": "21:00", 
       "toTime": "22:00", 
       "location": "somewhere", 
       "creator": "AVmabVjUIjogo0aNpbGm", 
       "matchType": "5", 
       "confirmedPlayers": [ 
        "AVnJOMvXOX1s7Ny2Wu9O" 
       ], 
       "pendingPlayers": [], 
       "comments": [] 
      } 
     } 
     ] 
    } 
} 

このクエリは、4試合を返し、それはOKです。

http://localhost:9200/my_index/match 
POST _search 
{ 
    "query": { 
     "bool": { 
     "should": [ 
      { "term": { "confirmedPlayers": { "value": "AVnJOMvXOX1s7Ny2Wu9O" } } }, 
      { "term": { "pendingPlayers": { "value": "AVnJOMvXOX1s7Ny2Wu9O" } } }, 
      { "term": { "creator": { "value": "AVnJOMvXOX1s7Ny2Wu9O" } } } 
     ], 
     "must": [ 
      { "range": { "date": { "gte": "20/01/2016", "format": "dd/MM/yyyy" } } } 
     ] 
     } 
    } 
} 
//RESULT  
{ 
    "took": 7, 
    "timed_out": false, 
    "_shards": { 
     "total": 5, 
     "successful": 5, 
     "failed": 0 
    }, 
    "hits": { 
     "total": 4, 
     "max_score": 1.6931472, 
     "hits": [ 
     { 
      "_index": "yojuego", 
      "_type": "match", 
      "_id": "match", 
      "_score": 1.6931472, 
      "_source": { 
       "title": "otro match 3", 
       "date": "2017-12-28T00:00:00.000Z", 
       "fromTime": "21:00", 
       "toTime": "22:00", 
       "location": "somewhere", 
       "creator": "AVnJOMvXOX1s7Ny2Wu9O", 
       "matchType": "5", 
       "confirmedPlayers": [], 
       "pendingPlayers": [], 
       "comments": [] 
      } 
     }, 
     { 
      "_index": "yojuego", 
      "_type": "match", 
      "_id": "AVnm-73OJxj9yxI50RS2", 
      "_score": 1.6931472, 
      "_source": { 
       "title": "otro match 3", 
       "date": "2017-12-28T00:00:00.000Z", 
       "fromTime": "21:00", 
       "toTime": "22:00", 
       "location": "somewhere", 
       "creator": "AVmabVjUIjogo0aNpbGm", 
       "matchType": "5", 
       "confirmedPlayers": [ 
        "AVnJOMvXOX1s7Ny2Wu9O" 
       ], 
       "pendingPlayers": [], 
       "comments": [] 
      } 
     }, 
     { 
      "_index": "yojuego", 
      "_type": "match", 
      "_id": "AVnm-9fOJxj9yxI50RS3", 
      "_score": 1.287682, 
      "_source": { 
       "title": "otro match 3", 
       "date": "2017-12-28T00:00:00.000Z", 
       "fromTime": "21:00", 
       "toTime": "22:00", 
       "location": "somewhere", 
       "creator": "AVmabVjUIjogo0aNpbGm", 
       "matchType": "5", 
       "confirmedPlayers": [], 
       "pendingPlayers": [ 
        "AVnJOMvXOX1s7Ny2Wu9O" 
       ], 
       "comments": [] 
      } 
     }, 
     { 
      "_index": "yojuego", 
      "_type": "match", 
      "_id": "AVnm-ykMJxj9yxI50RS1", 
      "_score": 1.287682, 
      "_source": { 
       "title": "otro match 3", 
       "date": "2017-12-28T00:00:00.000Z", 
       "fromTime": "21:00", 
       "toTime": "22:00", 
       "location": "somewhere", 
       "creator": "AVnJOMvXOX1s7Ny2Wu9O", 
       "matchType": "5", 
       "confirmedPlayers": [], 
       "pendingPlayers": [], 
       "comments": [] 
      } 
     } 
     ] 
    } 
} 

しかし、このクエリでも4つの一致が返されます。この場合、何も返されません。

POST _search 
{ 
    "query": { 
     "bool": { 
     "should": [ 
      { "term": { "confirmedPlayers": { "value": "inexistant" } } }, 
      { "term": { "pendingPlayers": { "value": "inexistant" } } }, 
      { "term": { "creator": { "value": "inexistant" } } } 
     ], 
     "must": [ 
      { "range": { "date": { "gte": "20/01/2016", "format": "dd/MM/yyyy" } } } 
     ] 
     } 
    } 
} 
//RESULT 
{ 
    "took": 7, 
    "timed_out": false, 
    "_shards": { 
     "total": 5, 
     "successful": 5, 
     "failed": 0 
    }, 
    "hits": { 
     "total": 4, 
     "max_score": 1, 
     "hits": [ 
     { 
      "_index": "yojuego", 
      "_type": "match", 
      "_id": "match", 
      "_score": 1, 
      "_source": { 
       "title": "otro match 3", 
       "date": "2017-12-28T00:00:00.000Z", 
       "fromTime": "21:00", 
       "toTime": "22:00", 
       "location": "somewhere", 
       "creator": "AVnJOMvXOX1s7Ny2Wu9O", 
       "matchType": "5", 
       "confirmedPlayers": [], 
       "pendingPlayers": [], 
       "comments": [] 
      } 
     }, 
     { 
      "_index": "yojuego", 
      "_type": "match", 
      "_id": "AVnm-9fOJxj9yxI50RS3", 
      "_score": 1, 
      "_source": { 
       "title": "otro match 3", 
       "date": "2017-12-28T00:00:00.000Z", 
       "fromTime": "21:00", 
       "toTime": "22:00", 
       "location": "somewhere", 
       "creator": "AVmabVjUIjogo0aNpbGm", 
       "matchType": "5", 
       "confirmedPlayers": [], 
       "pendingPlayers": [ 
        "AVnJOMvXOX1s7Ny2Wu9O" 
       ], 
       "comments": [] 
      } 
     }, 
     { 
      "_index": "yojuego", 
      "_type": "match", 
      "_id": "AVnm-ykMJxj9yxI50RS1", 
      "_score": 1, 
      "_source": { 
       "title": "otro match 3", 
       "date": "2017-12-28T00:00:00.000Z", 
       "fromTime": "21:00", 
       "toTime": "22:00", 
       "location": "somewhere", 
       "creator": "AVnJOMvXOX1s7Ny2Wu9O", 
       "matchType": "5", 
       "confirmedPlayers": [], 
       "pendingPlayers": [], 
       "comments": [] 
      } 
     }, 
     { 
      "_index": "yojuego", 
      "_type": "match", 
      "_id": "AVnm-73OJxj9yxI50RS2", 
      "_score": 1, 
      "_source": { 
       "title": "otro match 3", 
       "date": "2017-12-28T00:00:00.000Z", 
       "fromTime": "21:00", 
       "toTime": "22:00", 
       "location": "somewhere", 
       "creator": "AVmabVjUIjogo0aNpbGm", 
       "matchType": "5", 
       "confirmedPlayers": [ 
        "AVnJOMvXOX1s7Ny2Wu9O" 
       ], 
       "pendingPlayers": [], 
       "comments": [] 
      } 
     } 
     ] 
    } 
} 

マッピング:

{ 
    "match": { 
     "properties": { 
      "title": { "type": "string" }, 
      "date": { "type": "date" }, 
      "fromTime": { "type": "string" }, 
      "toTime": { "type": "string" }, 
      "location": { "type": "string" }, 
      "matchType": { "type": "integer" }, 
      "creator": { 
       "type": "string", 
       "index": "not_analyzed" 
      }, 
      "confirmedPlayers" : { 
       "type": "string", 
       "index": "not_analyzed" 
      }, 
      "pendingPlayers" : { 
       "type": "string", 
       "index": "not_analyzed" 
      }, 
      "comments" : { 
       "properties" : { 
        "id" : { "type" : "integer" }, 
        "owner" : { "type" : "string" }, 
        "text" : { "type" : "string" }, 
        "writtenOn": { "type": "date" } 
       } 
      } 
     } 
    } 
} 

私がすべきとしなければならないすべてのtogheter使用する場合に問題がアップcames。私が使用する必要があり、別々に彼らは正常に動作する必要があります。

+0

'confirmedPlayers'、' pendingPlayers'、 'creator'にはどのようなタイプのアナライザーが使われますか? – GrantD71

+0

これらの文字列フィールドが 'index:" not_analyzed "'に設定されていないと、検索用語に大文字があるので、should節は決して一致しません。 – GrantD71

+0

私は後で私の質問を拡大するつもりです。しかし、両方のフィルタは別々に正常に動作します。私の問題は、それらをすべてtogheterで使うときに現れます。私は私のマッピングも追加します。ありがとう。 –

答えて

2

2番目の例のクエリの結果(0の結果が返されると主張するところ)によると、shouldがelasticsearchで動作する方法について混乱があるようです。 the documentation

句(クエリ)が一致した文書に表示されますはずですから

は私が引用します。 必須またはフィルタ句を含まないブールクエリでは、1つまたは複数の句が文書と一致する必要があります。 に一致するshould節の最小数は、minimum_should_matchパラメータを使用して設定できます。

あなたはshouldmustでクエリを使用している場合、should句が当たることを実際にのみmust句必要はありません。 should句がヒットした場合、結果の上位ランクになります。

あなたはオプションがあります。 1つのオプション:単純なshouldクエリを記述し、minimum_should_matchパラメータを設定してから、そのクエリをfiltered句にラップして日付に基づいてフィルタすることができます。 2番目のオプション:should句の中にmust句を入れてネストされたクエリを作成します。

+0

鍵は 'minimum_should_match:1'にありました。追加するだけで十分でした。そして、そうです、私は「必要」と組み合わされたときに「すべき」ということを誤解していました。受け入れられupvoted、もちろん。おかげさまで –

関連する問題