简体   繁体   English

使用jQuery / PHP / AJAX / JSON的Lucene / Solr即时开发

[英]Lucene/Solr Instant Development using jQuery/PHP/AJAX/JSON

I am new to UI technologies like JQuery, AJAX, PHP etc. 我是JQuery,AJAX,PHP等UI技术的新手。

I am building a Google Instant like feature. 我正在建立类似Google Instant的功能。 My search engine infrastructure at backend is pretty fast and returns results even for Tera Bytes of data in super quick time. 我在后端的搜索引擎基础结构非常快,即使在超级快的时间内,即使是太字节数据也能返回结果。

I have a PHP function in a class that returns me an array: 我在类中有一个PHP函数,该函数返回一个数组:

solrsearch.php

    class SolrSearch{
        ...
        ...
        public function find( $q ){
           ...
           ...
           return $found;
        }
    }

$found is a key value pair something like documentID=>Data

When I dry run it ie on a console (not on a browser) with a test string and not using $q input variable everything seems to be fine and I print $found and it prints the values. 当我空运行它时,即在带有测试字符串的控制台(而不是在浏览器上)上并且不使用$ q输入变量,一切似乎都很好,我打印了$ found并打印了值。

Now my html file with embedded javascript looks like this, so basically here I am capturing each key press on the textbox and doing a get request 现在我的带有嵌入式javascript的html文件看起来像这样,所以基本上在这里,我捕获了文本框上的每个按键并进行了get请求

testjson.html

    <html>
    ...
    <body>

    <div align="center"><p><font size = 7 face="Verdana"> Search: <input type="text" id="term" size = 60 style="font-size:22pt"/>
    <table id="results">
    </table>
    <script>
    $(document).ready( function() {
    $('#term').keyup( function() {
      $.get('search_json.php?q='+escape($('#term').val()), function(data) {
        html = '<table id="results">';
        $.each( data, function( index, val ) {
            html += '<tr><td class="document"><b>'+index.key+'</b>&nbsp;';
            html += 'Dated '+val.value+'</td></tr>';
        } );
        html += '</html>';
        $('#results').replaceWith( html );
      } );
    } );
    } );
    </script>
    ...
    ...
    </html>

And then finally here is my 最后是我的

search_json.php

$s = new SolrSearch();
print json_encode( $s->find( $_REQUEST['q'] ) );

Even the json encoded string is working perfectly fine and prints the json though on a console (not on browser) 甚至json编码的字符串也可以正常工作,并且尽管在控制台上(而不是在浏览器上)打印json

Here is the problem I am facing: On the browser when I run testjson.html which just has a textBox and I input some strings so I dont get any results displayed back on the browser. 这是我面临的问题:在浏览器上,当我运行只有一个文本框的testjson.html时,我输入了一些字符串,所以我没有在浏览器上显示任何结果。 When I debug it using Fiddler (HTTP Debugging Proxy) I can see AJAX get requests in that. 当我使用Fiddler(HTTP调试代理)调试它时,我可以看到AJAX获取请求。

Any help would be appreciated. 任何帮助,将不胜感激。 If there is anything else I can add to the question which is required I will be more than happy to do that. 如果还有其他需要补充的问题,我将很乐意这样做。

EDIT: 编辑:

I further debugged and printed $q in the find function in solrsearch.php and it did not print the value that means the value is not getting passed from search_json.php however I can see the GET requests on Fiddler. 我在solrsearch.php的find函数中进一步调试并打印了$ q,它没有打印该值,这意味着该值未从search_json.php传递过来,但是我可以在Fiddler上看到GET请求。

I guess that this line: 我猜这行:

  • html += '</html>';

should be 应该

  • html += '</table>';

By the way, instead of using $('#results').replaceWith( html ); 顺便说一句,而不是使用$('#results').replaceWith( html ); try with $('#results').html( html ); 尝试$('#results').html( html );

Edited: Instead of $.get(), try with $.ajax(): 编辑:代替$ .get(),尝试使用$ .ajax():

$.ajax({
   url: 'search_json.php',
   type: 'POST',
   data: { q: escape($('#term').val() },
}).done(function(resp) {
   //Your html code with response options
});

If your front end uses HTML you definitely have to take a look on AJAX-SOLR 如果您的前端使用HTML,则一定要看一看AJAX-SOLR

AJAX Solr is a JavaScript library for creating user interfaces to Apache Solr AJAX Solr是一个JavaScript库,用于创建指向Apache Solr的用户界面

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM