IndexTank 自动完成:修改返回的每个结果的自动完成字段文本

发布于 2024-11-26 14:59:59 字数 1115 浏览 0 评论 0原文

我在 Heroku 上部署了 Ruby/Sinatra 应用程序,并使用 IndexTank 插件来提供全文搜索功能。

我目前正在使用开箱即用的自动完成功能,按照 IndexTank 自动完成文档

目前我已编制索引我的文档中,默认 :text 字段包含文档的城市名称和国家/地区名称。即:

@index.document("1").add({:text => "London England"})

当我在默认自动完成字段中进行搜索时,它实际上可以工作并返回结果,但不是我所期望或喜欢的。

当我在字段中输入“lon”时,它会返回“london”。这确实是正确的文件,但我希望它实际上会返回“英格兰伦敦”。

有谁知道我如何修改自动完成字段下拉列表中呈现的数据,以便当我搜索“lon”时显示“伦敦英格兰”?


更新

我还按照建议尝试了 InstantLinks 功能在评论中,但这也没有完全做我需要做的事情。看起来这两种解决方案都能满足我 80% 的需求,但不幸的是我还需要一些额外的东西。

关于即时链接的两件事不能按我的需要工作:

  • 虽然我可以从索引中选择要在下拉列表中显示的字段(这是我无法使用自动完成功能执行的操作),但当我使用箭头键选择下拉列表中的选项,所选选项不会显示在文本字段中。

  • 当我从下拉列表中选择一个条目时,我会被带到另一个页面,该页面的 URL 应该是从索引中提取的。我想要发生的只是选择要填充到原始文本字段中的条目的值。

因此,不幸的是,我也不知道 InstantLinks 将如何为我提供我想要的功能。

I have a Ruby/Sinatra application deployed on Heroku and I am using the IndexTank plugin to provide Full Text Search capability.

I am currently using the out of the box autocomplete functionality as per the IndexTank Autocomplete Documentation

Currently I have indexed my documents such that the default :text field contains the city name and country name of a document. ie:

@index.document("1").add({:text => "London England"})

When I do a search in the default autocomplete field it does in fact work and return a result, however not what I would have expected, or liked.

When I type in 'lon' into the field, it returns 'london'. This is indeed the correct document but I was hoping that it would actually return me 'London England'.

Does anybody know how I go about modifying the data that is rendered in the autocomplete field dropdown so that it displays 'London England' when I search for 'lon'?


UPDATE

I have also tried the InstantLinks functionality as suggested in the comments but this also does not quite do what I need to do. It seems that both solutions do about 80% of what I need, but unfortunately I need something extra.

The two things about InstantLinks that don't work as I need is:

  • While I can select which field from the index to display in the drop down (which is what I couldn't do with the Autocomplete functionality), when I use the arrow keys to select the options in the drop down, the selected option does not display in the text field.

  • When I do select an entry from the drop down, I am taken to another page, the URL of which is supposed to have been pulled from the index. All I want to happen is the value of the entry selected to be populated into the original text field.

So, unfortunately I can't see how InstantLinks is going to give me the functionality I am after either.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

给不了的爱 2024-12-03 14:59:59

好吧,我终于找到了解决问题的方法,但是,我无法使用 IndexTank 提供的自动完成或即时链接功能。

简而言之,我所做的是使用开箱即用的 jQuery 自动完成小部件(我知道 IndexTank 自动完成在幕后使用它)来调用我创建的查询 IndexTank 索引的宁静服务。

首先,我在 Sinatra 应用程序中创建了 Restful 服务,

get '/index/' do
    term = params['term']

    #Query IndexTank index using the IndexTank::Client

    #Parse index search results and return an array of the suggestions as JSON
end

接下来,我使用 jQuery 自动完成小部件将我的 Restful 服务用作远程源。首先是我的 HTML 输入:

<form id="search_form" action="/" method="POST">
    <input id="search_field" name="search_field" type="text">
</form>

然后是用于将自动完成小部件绑定到输入的 javascript:

$(document).ready(function(){
    $("#search_field").autocomplete({
      source: function(request, response) {
        $.ajax({
          url: "/index/",
          dataType: 'json',
          data: { term: request.term },
          success: function(data) {
            response($.map(data, function(item) {
              return {label: __highlight(item, request.term),
                value: item};
            }));
          }
        });
      },
      minLength: 2
    })
    .data( "autocomplete" )._renderItem = function( ul, item ) {
      // only change here was to replace .text() with .html()
      return $( "<li></li>" )
        .data( "item.autocomplete", item )
        .append( $( "<a></a>" ).html(item.label) )
        .appendTo( ul );
    };
  });

  function __highlight(s, t) {
    var matcher = new RegExp("("+$.ui.autocomplete.escapeRegex(t)+")", "ig" );
    return s.replace(matcher, "<strong>$1</strong>");
  }

现在您就拥有了一个自动完成字段,该字段查询 IndexTank 索引并在建议下拉列表中显示所需的索引字段。

Ok, so I finally worked out a way to solve my problem however, I was not able to use either the Autocomplete or InstantLinks functionality provided by IndexTank.

In short what I did was use the out of the box jQuery autocomplete widget (which I know the IndexTank Autocomplete uses under the covers) to call a restful service I created which queries the IndexTank index.

First I created the restful service in my Sinatra Application

get '/index/' do
    term = params['term']

    #Query IndexTank index using the IndexTank::Client

    #Parse index search results and return an array of the suggestions as JSON
end

Next, I used the jQuery autocomplete widget to use my restful service as a remote source. First there is my HTML input:

<form id="search_form" action="/" method="POST">
    <input id="search_field" name="search_field" type="text">
</form>

Then the javascript to bind the autocomplete widget to the input:

$(document).ready(function(){
    $("#search_field").autocomplete({
      source: function(request, response) {
        $.ajax({
          url: "/index/",
          dataType: 'json',
          data: { term: request.term },
          success: function(data) {
            response($.map(data, function(item) {
              return {label: __highlight(item, request.term),
                value: item};
            }));
          }
        });
      },
      minLength: 2
    })
    .data( "autocomplete" )._renderItem = function( ul, item ) {
      // only change here was to replace .text() with .html()
      return $( "<li></li>" )
        .data( "item.autocomplete", item )
        .append( $( "<a></a>" ).html(item.label) )
        .appendTo( ul );
    };
  });

  function __highlight(s, t) {
    var matcher = new RegExp("("+$.ui.autocomplete.escapeRegex(t)+")", "ig" );
    return s.replace(matcher, "<strong>$1</strong>");
  }

And there you have it, an autocomplete field that queries an IndexTank index and displays the desired index field in the suggestions drop down.

ぶ宁プ宁ぶ 2024-12-03 14:59:59

您可能想使用 InstantLinks 代替。

如果您确实想调整自动完成功能,则应该更改与 Indextank 自动完成功能关联的 jQuery UI 小部件的 _renderItem 属性。

$("#query").data("autocomplete")._renderItem = function(ul, item) { .. }

有关示例,请参阅 jQuery UI 自动完成文档

You probably want to use InstantLinks instead.

If you really want to tweak autocomplete, you should change the _renderItem property of the jQuery UI widget associated with Indextank Autocomplete.

$("#query").data("autocomplete")._renderItem = function(ul, item) { .. }

See jQuery UI Autocomplete documentation for an example.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文