I am starting out using the search kit and am trying to connect to a local instance of Elasticsearch version 5.2. There is already an index, mapping and data in the Elasticsearch instance and I could query for the data using Kibana.
When the page loads, the result is always 0 results found. I am not even sure if it manages to connect to the local instance successfully. The code is:
import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import registerServiceWorker from './registerServiceWorker';
import{SearchkitManager,SearchkitProvider,SearchBox,Hits,HitsStats} from "searchkit";
const searchkit = new SearchkitManager("http://localhost:9200");
ReactDOM.render((
<SearchkitProvider searchkit={searchkit}>
<div>
<SearchBox searchOnChange={true} queryFields={["productName"]} queryOptions={{analyzer:"standard"}}/>
<HitsStats translations={{
"hitstats.results_found":"{hitCount} results found"
}}/>
<Hits hitsPerPage={5}/>
</div>
</SearchkitProvider>), document.getElementById('root'));
registerServiceWorker();
I am probably doing something wrong or missing something. Could anyone help with this?
All browsers have a default security policy enabled:
From Wipikedia:
I imagine that you're serving your webpage using a local web server exposed on the
3000port. So, when your ajax request to elasticsearch has an origin ofhttp://localhost:3000while your ES instance is exposed athttp://localhost:9200.You have many options to solve this.
Disable same origin policy in Chrome (only for development!)
You can follow this SO question to disabling same origin policy in Chrome.
Use a reverse proxy
You can configure a local reverse proxy (e.g. nginx) to serve your pages at
localhostwhile exposing your ES instance atlocalhost/es. This is a simple nginx example to expose ES at localhost.Proxy requests to ES from you app
If you're using a node based app to serve your pages, you can use the node-http-proxy to proxy request from your local path (e.g.
localhost:3000/es) to your ES instance.Enable CORS in your ES instance
Add these lines to your
elasticsearch.ymlconfiguration file:If you want to allow all Origins just replace the last configuration value with
"*". See the documentation for more information.