簡體   English   中英

如何在彈性搜索jdbc導入器中使用sql Like運算符

[英]How to use sql Like Operator in elastic search jdbc importer

我的項目中使用的技術(Spring mvc,elasticsearch,rest api),問題是,我的彈性搜索jdbc導入器無法像運算符一樣運行sql,iam會獲取空數據,因此,任何人都可以幫我這個忙。

控制者

package com.technoshinelabs.ulearn.rest.controller;

import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.MediaType; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController;

import com.technoshinelabs.ulearn.api.elasticsearch.repository.AutocompleteRepository; import com.technoshinelabs.ulearn.controller.BaseController; import com.technoshinelabs.ulearn.rest.entity.ResponseEntity; import com.technoshinelabs.ulearn.rest.util.RestStatus; import com.technoshinelabs.ulearn.util.ControllerUriConstant;

@RestController @RequestMapping(value = ControllerUriConstant.rest_autocomplete) public class RestAutocompleteController extends BaseController {

    @Autowired  AutocompleteRepository autoCompleteRepository;

    /**      *       * @param autoComplete   * @return   */     @RequestMapping(value = ControllerUriConstant.rest_autocompletesearch, method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)    public ResponseEntity getOverLayWords(@RequestParam("q") String name) {         System.out.println("check search value" + " " + name);      return getResponseEntity(autoCompleteRepository.findByName(name), RestStatus.OK.getCode(), RestStatus.OK.getText());    } }

知識庫

package com.technoshinelabs.ulearn.api.elasticsearch.repository;

import java.util.List;

import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;

import com.technoshinelabs.ulearn.api.elasticsearch.entities.Autocomplete;

public interface AutocompleteRepository extends ElasticsearchRepository<Autocomplete, String>{

    public List<Autocomplete> findByName(String name); }

自動完成

 @echo off

set DIR=%~dp0 set LIB=%DIR%..\lib\* set BIN=%DIR%..\bin

REM ??? echo {^
    "type" : "jdbc",^
    "jdbc" : {^
        "url" : "jdbc:mysql://localhost:3306/ulearn_app",^
        "user" : "root",^
        "password" : "root",^
        "sql" :  "Select id as _id, id, name from course where name like '%name%'",^
        "treat_binary_as_string" : true,^
        "elasticsearch" : {^    
             "cluster" : "elasticsearch",^
             "host" : "localhost",^
             "port" : 9300^
        },^
        "index" : "ind_autocomplete"^
      }^ }^ | "%JAVA_HOME%\bin\java" -cp "%LIB%" -Dlog4j.configurationFile="%BIN%\log4j2.xml" "org.xbib.tools.Runner" "org.xbib.tools.JDBCImporter"

至少有兩種方法可以執行此操作。 我使用第二個是因為我不必在jdbc中使用'to \\'更改並將其更改為''以測試sql工具中的查詢。

  1. \\' for each '使用\\' for each '
  2. 或。將查詢作為視圖存儲在數據庫中。 然后使用\\'調用視圖而不是sql

    • 在您的數據庫中:

      CREATE VIEW your_jdbc_view AS Select id as _id, id, name from course where name like '%name%';

    • 將您的elasticsearch文件中的行更改為:

      “ sql”: "Select * from your_jdbc_view",

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM