Class: Milkode::SearchContents

Inherits:
Object
  • Object
show all
Defined in:
lib/milkode/cdweb/lib/search_contents.rb

Defined Under Namespace

Classes: MatchRecord

Constant Summary collapse

DISP_NUM =

1ページの表示数

20
LIMIT_NUM =

最大検索ファイル数

50
NTH =

表示範囲

3
COL_LIMIT =

1行の桁制限

200
MATH_FILE_DISP =

マッチファイルの最大表示数

3
MATH_FILE_LIMIT =

マッチファイルの検索リミット数

MATH_FILE_DISP + 1
DEFAULT_WIDE_MATCH_RANGE =

未指定時のワイド検索範囲

7
FILTER_BY_PACKAGE_NUM =
8
FILTER_BY_SUFFIX_NUM =
8
FILTER_BY_DIRECTORIES_FILES =
200

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, params, query, suburl, locale) ⇒ SearchContents

Returns a new instance of SearchContents.



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/milkode/cdweb/lib/search_contents.rb', line 33

def initialize(path, params, query, suburl, locale)
  @path             = path
  @params           = params
  @q                = query
  @page             = params[:page].to_i || 0
  @offset           = params[:offset].to_i
  @line             = params[:line].to_i
  @is_onematch      = params[:onematch]  == 'on'
  @is_sensitive     = params[:sensitive] == 'on'
  @suburl           = suburl
  @homeurl          = @suburl + "/home/"
  @locale           = locale

  @searcher_fuzzy_gotoline = nil

  # 検索1 : クエリーそのまま
  @records, @total_records, result = Database.instance.search(@q.keywords, @q.multi_match_keywords, @q.packages, path, @q.fpaths, @q.suffixs, @q.fpath_or_packages, @offset, LIMIT_NUM)
  grep_contents(@q.keywords, @q.wide_match_range)

  # 検索2 : マッチしなかった時におすすめクエリーがある場合

  # gotolineモード (test_cdstk.rb:55)
  if @match_records.empty? && recommended_fuzzy_gotoline?
    # 専用の Searcher を作成
    @searcher_fuzzy_gotoline = SearchFuzzyGotoLine.new(@path, @params, @q, @suburl)

    # 結果をコピーする
    @total_records = @searcher_fuzzy_gotoline.total_records
    @match_records = @searcher_fuzzy_gotoline.match_records
    @next_index    = @searcher_fuzzy_gotoline.next_index
    @end_index     = @searcher_fuzzy_gotoline.end_index
    @next_line     = nil
  end

  # ワイド検索範囲
  if @match_records.empty? && recommended_wide_match_range?
    grep_contents(@q.keywords, DEFAULT_WIDE_MATCH_RANGE)

    # 検索範囲0の自動マッチは混乱をまねくのでやめる
    # if @match_records.empty?
    #   grep_contents(@q.keywords, 0)
    # end
  end

  # 先頭をファイル名とみなす自動マッチは混乱をまねくのでやめる
  # if @match_records.empty? && recommended_fpath_or_packages?
  #   # おすすめクエリーに変換
  #   q2 = @q.conv_head_keyword_to_fpath_or_packages
    
  #   # 検索
  #   @records, @total_records = Database.instance.search(q2.keywords, q2.multi_match_keywords, q2.packages, path, q2.fpaths, q2.suffixs, q2.fpath_or_packages, @offset, LIMIT_NUM)
    
  #   # 再grep
  #   grep_contents(q2.keywords, q2.wide_match_range)
  # end
  
  # 検索3 : マッチするファイル
  @match_files = []
  if @offset == 0 && @line == 0
    t = 0

    if (@path != "")
      @match_files, t = Database.instance.search([], @q.multi_match_keywords, @q.packages, path, @q.fpaths + @q.keywords, @q.suffixs, @q.fpath_or_packages, @offset, MATH_FILE_LIMIT)
    else
      @match_files, t = Database.instance.search([], @q.multi_match_keywords, @q.packages, path, @q.fpaths, @q.suffixs, @q.fpath_or_packages + @q.keywords, @offset, MATH_FILE_LIMIT)
    end
  end

  # Search4 : Drilldown
  begin 
    @drilldown_packages    = DocumentTable.drilldown(result, "package", FILTER_BY_PACKAGE_NUM)
    @drilldown_directories = make_drilldown_directories(result)
    @drilldown_suffixs     = DocumentTable.drilldown(result, "suffix", FILTER_BY_SUFFIX_NUM)
  rescue Groonga::InvalidArgument
    @drilldown_packages = @drilldown_directories = @drilldown_suffixs = []
  end
end

Instance Attribute Details

#pageObject (readonly)

Returns the value of attribute page.



17
18
19
# File 'lib/milkode/cdweb/lib/search_contents.rb', line 17

def page
  @page
end

#total_recordsObject (readonly)

Returns the value of attribute total_records.



16
17
18
# File 'lib/milkode/cdweb/lib/search_contents.rb', line 16

def total_records
  @total_records
end

Instance Method Details

#data_rangeObject



140
141
142
# File 'lib/milkode/cdweb/lib/search_contents.rb', line 140

def data_range
  @offset..(@offset + @end_index)
end

#directjump?Boolean

Returns:

  • (Boolean)


281
282
283
# File 'lib/milkode/cdweb/lib/search_contents.rb', line 281

def directjump?
  @searcher_fuzzy_gotoline && @searcher_fuzzy_gotoline.directjump?
end

#directjump_urlObject



285
286
287
# File 'lib/milkode/cdweb/lib/search_contents.rb', line 285

def directjump_url
  @searcher_fuzzy_gotoline.directjump_url
end

#html_contentsObject



144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/milkode/cdweb/lib/search_contents.rb', line 144

def html_contents
  match_groups = @match_records.reduce([]) do |g, m|
    if (g.empty?)
      g << [m]
    else
      prev = g[-1][-1]

      if (m.match_line.index - prev.match_line.index <= NTH * 2 &&
          m.record.shortpath == prev.record.shortpath)
        g[-1] << m          # グループの末尾に追加
        g
      else
        g << [m]            # 新規グループ
      end
    end

    # 近接マッチ無効
    # g << [m]
  end

  @prev = nil
  
  <<EOF
#{recommended_contents}
#{match_groups.map{|g|result_match_record(g)}.join}
EOF
end

#html_paginationObject



266
267
268
269
270
271
272
273
274
275
# File 'lib/milkode/cdweb/lib/search_contents.rb', line 266

def html_pagination
  return "" if @q.empty?
  return "" if next_offset >= @total_records

  return <<EOF
<div class='pagination pagination-centered'>
#{pagination_link(next_offset, @next_line, "next >>")}
</div>
EOF
end

#make_drilldown_directories(result) ⇒ Object



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/milkode/cdweb/lib/search_contents.rb', line 111

def make_drilldown_directories(result)
  # Return empty if root path
  return [] if @path == ""

  # Drilldown
  files = DocumentTable.drilldown(result, "restpath")
  return [] if files.size > FILTER_BY_DIRECTORIES_FILES
  
  files.map {|v|
    Util.relative_path(v[1], @path.split("/")[1..-1].join("/")).to_s               # 'path/to/file' ->  'to/file' (@path == 'path')
  }.find_all {|v|
    v.include?("/")                                                                 # Extract directory
  }.map {|v|
    v.split("/")[0]                                                                 # 'to/file' -> 'to'
  }.inject(Hash.new(0)) {|hash, v| 
    hash[v] += 1; hash                                                              # Collect hash
  }.map {|key, value|
    [value, key]                                                                    # To Array
  }.to_a
end

#match_files_contentsObject



248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
# File 'lib/milkode/cdweb/lib/search_contents.rb', line 248

def match_files_contents
  unless @match_files.empty?
    is_and_more = @match_files.size >= MATH_FILE_LIMIT
    @match_files = @match_files[0..MATH_FILE_DISP-1]
    conv_query = (@path != "") ? @q.conv_keywords_to_fpath : @q.conv_keywords_to_fpath_or_packages
    tmpp = @params.clone
    tmpp[:query] = conv_query.query_string
    url = Mkurl.new(@path, tmpp).inherit_query_shead
    <<EOF
#{@match_files.map {|record| result_record(DocumentRecord.new(record))}.join}
#{"<a href='#{url}'>...and more</a></a>" if is_and_more}
<hr>
EOF
  else
    ""
  end
end

#match_numObject



277
278
279
# File 'lib/milkode/cdweb/lib/search_contents.rb', line 277

def match_num
  @match_records.size
end

#next_offsetObject



136
137
138
# File 'lib/milkode/cdweb/lib/search_contents.rb', line 136

def next_offset
  @offset + @next_index
end

#queryObject



132
133
134
# File 'lib/milkode/cdweb/lib/search_contents.rb', line 132

def query
  @q.query_string
end


172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'lib/milkode/cdweb/lib/search_contents.rb', line 172

def recommended_contents
  contents = []

  str = drilldown_contents
  contents << str unless str.empty?

  # str = recommended_query_contents
  # contents << str unless str.empty?

  str = match_files_contents
  contents << str unless str.empty?

  unless contents.empty?
    contents.join
  else
    ""
  end
end

Returns:

  • (Boolean)


199
200
201
# File 'lib/milkode/cdweb/lib/search_contents.rb', line 199

def recommended_fpath_or_packages?
  @q.keywords.size >= 2 && @q.only_keywords
end

Returns:

  • (Boolean)


191
192
193
# File 'lib/milkode/cdweb/lib/search_contents.rb', line 191

def recommended_fuzzy_gotoline?
  @q.keywords.size == 1 && @q.only_keywords && Util.fuzzy_gotoline_keyword?(@q.keywords[0])
end


203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
# File 'lib/milkode/cdweb/lib/search_contents.rb', line 203

def recommended_query_contents
  result = []
  
  if recommended_fuzzy_gotoline?
    conv_query   = @q.conv_fuzzy_gotoline
    tmpp         = @params.clone
    tmpp[:query] = conv_query.query_string
    url          = Mkurl.new(@path, tmpp).inherit_query_shead
    result << "<dt class='result-file'>#{img_icon('document-new-4.png', @suburl)}<a href='#{url}'>#{conv_query.query_string}</a></dt>"
  end

  if recommended_wide_match_range?
    conv_query   = @q.conv_wide_match_range(0)
    tmpp         = @params.clone
    tmpp[:query] = conv_query.query_string
    w0_url        = Mkurl.new(@path, tmpp).inherit_query_shead

    conv_query   = @q.conv_wide_match_range(1)
    tmpp         = @params.clone
    tmpp[:query] = conv_query.query_string
    w1_url        = Mkurl.new(@path, tmpp).inherit_query_shead

    conv_query   = @q.conv_wide_match_range(DEFAULT_WIDE_MATCH_RANGE)
    tmpp         = @params.clone
    tmpp[:query] = conv_query.query_string
    url          = Mkurl.new(@path, tmpp).inherit_query_shead

    result << "<dt class='result-file'>#{img_icon('document-new-4.png', @suburl)}<a href='#{url}'>#{conv_query.query_string}</a> (<a href='#{w0_url}'>w:0</a>, <a href='#{w1_url}'>w:1</a>)</dt>"
  end
  
  if recommended_fpath_or_packages?
    conv_query   = @q.conv_head_keyword_to_fpath_or_packages
    tmpp         = @params.clone
    tmpp[:query] = conv_query.query_string
    url          = Mkurl.new(@path, tmpp).inherit_query_shead
    result << "<dt class='result-file'>#{img_icon('document-new-4.png', @suburl)}<a href='#{url}'>#{conv_query.query_string}</a></dt>"
  end

  unless result.empty?
    result.join("\n") + "<hr>\n"
  else
    ""
  end
end

Returns:

  • (Boolean)


195
196
197
# File 'lib/milkode/cdweb/lib/search_contents.rb', line 195

def recommended_wide_match_range?
  @q.keywords.size >= 2 && @q.wide_match_range_empty?
end