在elasticsearch中使用逗号分隔值进行搜索

时间:2016-03-31 13:29:26

标签: elasticsearch

我在弹性搜索方面相当新,所以如果有人可以帮助我

假设我选择了

1) Category - Hollywood
2) Sub-Category - Bond Special
3) Genre - Action & Drama & Comedy ( as multiple selection will be there )
4) Language - English, Russian and Hindi ( as multiple selection will be there)
5) Release Year - 1990,1999,2000 ( as multiple selection will be there)
6) 3D Movie - True OR False (any one will be selected)
7) SortBy - “A-Z”OR “Z-A” OR “Date”

任何人都可以帮助我进行弹性搜索查询。我将使用“match_phrase”进行AND条件,但问题是匹配参数或搜索参数将是多个和逗号分隔(你可以说)。 我的索引数组如下: -

[_source] => Array (
                                [id] => 43
                                [value] => GREENBERG
                                [imageName] => Done
                                [date] => (1905) USA (Bengali)
                                [language] => (Bengali) | 1905 |  1.47hrs
                                [directorName] => Alejandro González Iñárritu, Ang Lee
                                [castForSearch] => Ben Stiller, John Turturro
                                [viewDetailsUrl] => /movie/greenberg
                                [movieType] => Animation
                                [rating] => 0
                                [cast] => Ben Stiller, John Turturro, Olivier Martinez
                                [synopsis] => A man from Los Angeles, who moved to New York years ago, returns to L.A. to figure out his life while he house-sits for his brother. He soon sparks with his brother's assistant.
                                [code] => HLR06
                                [type] => Non-3D
                                [trailer] => https://www.youtube.com/watch?v=cwdliqOGTLw
                                [imdb_code] => 1234654
                                [tags] => Array
                                    (
                                        [0] => Animation
                                    )

                                [genre] => Adventure
                                [languages] => Bengali
                                [categories_filter] => Array
                                    (
                                        [0] => Category 2,Hollywood
                                    )

                                [sub_categories_filter] => Array
                                    (
                                        [0] => Sub-Category 1,Sub-Category 4,Sub-Category 5,Sub-Category 6,Sub-Category 7
                                    )

                                    )
Weekly Sunday 12 AM
everyday 12 AM 
every day 12:15 AM
daily 12:01 AM
daily 12:01 AM
joinScreenCancellationScheduler - Weekly Sunday 12 AM 
0 0 * * 7 curl <url>
goLiveDate - everyday 12 AM 
0 0 * * * curl <url>
nearestDateDisable - every day 12:15 AM
15 0 * * * curl <url>
reminderOfEvent - daily 12:01 AM
01 0 * * * curl <url>
thresholdNotMet - daily 12:01 AM
daily 12:01 AM

1 个答案:

答案 0 :(得分:1)

  • 要匹配多个可能值中的一个,请使用terms query。您不需要match_phrase查询,因为您没有进行任何类型的自由文本匹配。

  • 在将数据索引到Elasticsearch之前(或使用以逗号分隔的tokenizer)之前,您需要将逗号分隔值拆分为数组。

  • 您的使用案例表明您并不关心评分,只关注过滤,在这种情况下,您的查询应该只有一个过滤器。

  • 排序与过滤不同;对于您的A-Z / Z-A /日期排序,您需要在查询之外使用sort子句。

最后的事情可能会是这样的:

GET /my_index/my_type/_search
{
  "query": {
    "bool": {
      "filter": [
        "terms": { "genre": ["Action", "Drama", "Comedy"] },
        "terms": { "language": ["English", "Russian", "Hindi"] },
        // more terms filters
      ]
    }
  },
  "sort": { "title": "asc" }
}