2017-01-28 4 views
0

Моя ситуация:ElasticSearch применять следует и диапазон

Я работаю с базой данных ElasticSearch, и я не могу применить пару «ОШ» плюс пару «Анд». Я пишу SQL-запрос, чтобы показать, что хочу, в моем SQL-запросе я использовал confirmedPlayers и pendingPlayers, поскольку они были массивами, конечно, я знаю, что мы не можем сделать это в 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 матча, и это нормально.

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" } 
       } 
      } 
     } 
    } 
} 

Проблема АМСВА когда я использую должен и все должны совмещен. Если я использую, должен и должен отдельно работать нормально.

+0

Какой тип анализатора используется для 'подтвержденныхPlayers',' pendingPlayers' и 'creator'? – GrantD71

+0

Если эти строковые поля не были установлены в 'index:" not_analyzed "', то предложения if никогда не будут совпадать, потому что у вас есть заглавные буквы в поисковых терминах. – GrantD71

+0

Я собираюсь расширить свой вопрос позже. Но оба фильтра работают отлично. Моя проблема возникает, когда я использую их все togheter. Я также добавлю свои сопоставления. Благодарю. –

ответ

2

Основываясь на результатах вашего второго примера запроса (там, где вы утверждаете, что должны быть возвращены 0 результатов), похоже, у вас есть путаница в том, как should работает в elasticsearch.

Я процитирую из the documentation

должен

Предложение (запрос) должен появиться в согласующего документ. В boolean query без обязательных или фильтрующих статей, один или несколько должно быть статьи должны соответствовать документу. Минимальное количество обязательных положений до может быть установлено с использованием параметра minimum_should_match.

Если вы используете запрос с should и must, это не на самом деле необходимо, чтобы положение should хиты, только пункт must. Если пункты should действительно попадут, они будут оценены в результатах выше.

У вас есть варианты. Один из вариантов: вы можете написать простой запрос should и установить параметр minimum_should_match, а затем обернуть этот запрос в предложение filtered для фильтрации на основе даты. Второй вариант: создать вложенный запрос с предложением must внутри предложения should.

+0

Ключ был в 'minimum_should_match: 1', просто добавив, что этого достаточно. И да, я не понимал, как «должен» работать, когда он сочетается с 'must'. Разумеется, принято и одобрено. Спасибо. –

Смежные вопросы