EzDevInfo.com

Jest

Elasticsearch Java Rest Client.

How to fix the URI does not specify a valid host name in ClientProtocolException:

Hi When i execute the elastic search java API using jest client i face the following error

org.apache.http.client.ClientProtocolException: URI does not specify a valid host name:localhost:9200/index/type/_search

i try various possibilities to fix this error,but it wont works is any one guide me to fix this?.Thanks in advance.


Source: (StackOverflow)

Jest dont mock my submodules

i would like insert Jest test to my project. But my every attempt ends with fail (submodules isn't mocked).

modules/action/xhr.js
modules/list.js
__tests__/list-test.js
node_modules
package.json

If i require action/xhr in modules/list.js so action/xhr isn't mocked. Does anyone know why?

I have this code on github.

  • git clone git@github.com:jonnybcz/stackoverflow-jest-dont-mock.git
  • cd stackoverflow-jest-dont-mock; make run-test

Source: (StackOverflow)

Advertisements

how to write code for search in elasticsearch using jest client in java

I try this code URL is http://localhost:9200 index is restaurant type is menu. I want pass id through the searchbuilder. Please anyone can help me. I got this output

722 [main] INFO io.searchbox.client.JestClientFactory - Node Discovery       Disabled...
hai 
io.searchbox.core.SearchResult@35f8a538

but I want id data

import io.searchbox.client.JestClient;
import io.searchbox.client.JestClientFactory;
import io.searchbox.client.JestResult;
import io.searchbox.client.config.HttpClientConfig;
import io.searchbox.core.Search;
import io.searchbox.core.SearchResult;
import io.searchbox.core.SearchResult.Hit;

import java.io.IOException;
import java.sql.Date;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import jesttask.*;

import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.builder.SearchSourceBuilder;

import com.google.gson.JsonObject;

public class Elasticsearch 
{
public static void main( String[] args ) throws Exception 
{
    JestClient client = openClient();

    JsonObject json=new JsonObject();

for search code here

   /* Search search = (Search) new Search.Builder("http://localhost:9200/")
    // multiple index or types can be added.
    .addIndex("articles")
    .addType("article")
    .build();

JestResult result = client.execute(search);*/

    SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
    searchSourceBuilder.query(QueryBuilders.queryString("AVE5MpaA7X6GJDWpFptT"));
    Search search = (Search) new Search.Builder(searchSourceBuilder.toString())
                                    // multiple index or types can be added.
                                    .addIndex("restarent")
                                    .addType("menu")
                                    .build();
    System.out.println("hai");
    JestResult result = client.execute(search);
    System.out.println(result);


    List<SearchModel> reviewList = result.getSourceAsObjectList(SearchModel.class);
    for(SearchModel review: reviewList){
        System.out.println("h");
      System.out.println("search result is " + review);
    }

    /*
    SearchHit[] results = result.getHits().getHits();
    for (SearchHit hit : results) {
      System.out.println(hit.getId());    //prints out the id of the document
      Map<String,Object> result2 = hit.getSource();   //the retrieved document
    }*/

    /*List<SearchModel> sources = result.getSourceAsObjectList(SearchModel.class);

    for (SearchModel source : sources) {
        System.out.println("h");

        System.out.println(source.getTitle() + " -> " + source.getItem());
    }*/

    client.shutdownClient();
}

private static JestClient openClient()
{ 
 HttpClientConfig clientConfig = new HttpClientConfig.Builder("http://localhost:9200/")
      .multiThreaded(true).build();
 JestClientFactory factory = new JestClientFactory();

 factory.setHttpClientConfig(clientConfig);
 JestClient jestClient = factory.getObject();

 return jestClient;
}
}

my model class is

import io.searchbox.annotations.JestId;

import com.fasterxml.jackson.annotation.JsonProperty;

public class SearchModel {
@JestId
private String id;

@JsonProperty("price")
private double price;
@JsonProperty("quantity")
private String quantity;

@JsonProperty("item")
private String item;




public String getId() {
    return id;
}
public void setId(String id) {
    this.id = id;
}
public double getPrice() {
    return price;
}
public void setPrice(Double price) {
    this.price = price;
}

public String getQuantity() {
    return quantity;
}
public void setQuantity(String quantity) {
    this.quantity = quantity;
}

public String getItem() {
    return item;
}
public void setItem(String item) {
    this.item = item;
}


}

Source: (StackOverflow)

use babel-jest for jest but still get syntax error

I am new to jest and want to test my react.js app. I am following a book, React.js Essentials for jest part.

Here is my test code, Button-test.js

jest.dontMock('../Button.react');

describe('Button component', function () {
  it('calls handler function on click', function () {
    var React = require('react');
    var TestUtils = require('react-addons-test-utils');
    var Button = require('../Button.react');
    var handleClick = jest.genMockFunction();

    var button = TestUtils.renderIntoDocument(
      <Button handleClick={handleClick}/>
    );

    var buttonInstance =
      TestUtils.findRenderedDOMComponentWithTag(button, 'button');

    TestUtils.Simulate.click(buttonInstance);
    expect(handleClick).toBeCalled();
    var numberOfCallsMadeIntoMockFunction =
      handleClick.mock.calls.length;
    expect(numberOfCallsMadeIntoMockFunction).toBe(1);
  });
});

Here is my package.json

{
  "name": "snapterest",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "jest"
  },
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "babel-jest": "^6.0.1",
    "babelify": "^6.2.0",
    "browserify": "^12.0.1",
    "gulp": "^3.9.0",
    "jest-cli": "^0.8.0",
    "vinyl-source-stream": "^1.1.0"
  },
  "dependencies": {
    "react": "^0.14.0-beta3",
    "react-dom": "^0.14.0-beta3",
    "snapkite-stream-client": "^1.0.3"
  },
  "jest": {
    "scriptPreprocessor": "<rootDir>/node_modules/babel-jest",
    "testFileExtensions": ["es6", "js"],
    "unmockedModulePathPatterns": [
      "<rootDir>/node_modules/react"
    ]
  }
}

The problem is, when I run npm test, it reports the following syntax error. I think babel-jest is installed, and I have no idea why the syntax error is still there. Are there anything else I need to do except installing babel-jest?

source/components/__tests__/Header-test.js 
● Runtime Error
SyntaxError:        
/snapterest/source/components/__tests__/Header-test.js: Unexpected token (11:6)
   9 |    
  10 |     var button = TestUtils.renderIntoDocument(
> 11 |       <Button handleClick={handleClick}/>
     |       ^
  12 |     );

Source: (StackOverflow)

ElasticSearch Jest client use: MalformedJsonException on any queries

I am trying to use a JEST Client to search the remotely located ElasticSearch index.

However I've ran into a problem - every single query, be it constructed using various builders or just default ES queries, everything returns com.google.gson.stream.MalformedJsonException

Code:

String URL = "http://api.exiletools.com:80";
String API_KEY = "DEVELOPMENT-Indexer";

 JestClientFactory factory = new JestClientFactory();
        factory.setHttpClientConfig(new HttpClientConfig.Builder(URL)
                .defaultCredentials("apikey", API_KEY)
                .build());
 JestClient client = factory.getObject();

  qb = QueryBuilders
                    .boolQuery()
                    .must(QueryBuilders.termQuery("attributes.league", "Standard"))
                    .must(new TermQueryBuilder("attributes.equipType", "Ring"))
                    .must(new TermQueryBuilder("shop.verified", "yes"));
  searchSourceBuilder = new SearchSourceBuilder();
  searchSourceBuilder.query(qb);

  query = searchSourceBuilder.toString();

  search = new Search.Builder(query).build();

  client.execute(search); // Here I get the error

As a final test I just copied the smallest query I could find from Jest integration test examples and just replaced the search terms there, to look like:

        query = "{\n"
                + "    \"query\" : {\n"
                + "        \"term\" : { \"shop.chaosEquiv\" : \"167\" }\n"
                + "    }\n"
                + "}";

This query when copied from the output stream looks like this:

{
    "query" : {
        "term" : { "shop.chaosEquiv" : "167" }
    }
}

No trailing whitespaces or anything, looks valid to me.

Still getting the same error. Can anyone tell what is going on?


Source: (StackOverflow)

Signing AWS HTTP requests with Apache HttpComponents Client

I'm trying to make HTTP requests to an AWS Elasticsearch domain protected by an IAM access policy. I need to sign these requests for them to be authorized by AWS. I'm using Jest, which in turn use Apache HttpComponents Client.

This seems to be a common use case and I was wondering if there is out there some kind of library which I can use on top of Apache HttpComponents Client to sign all the requests.


Source: (StackOverflow)

How can I make Jest handle an ElasticSearch server being unavailable?

I currently configure Jest by giving it a list of server URIs. Like this:

 public JestClient jestClient() {
    final JestClientFactory factory = new JestClientFactory();
    factory.setHttpClientConfig(new HttpClientConfig
            .Builder(esServerUris)
            .build());
    final JestClient jestClient = factory.getObject();
    return jestClient;
}

If one of my ElasticSearch servers go offline (e.g. failure or maintenance), then a percentage of my Jest queries fail. Jest doesn't seem to do any kind of intelligent connection management by default.It must do something like round-robin through the servers or pick a server at random.

Is there a better way to handle this?


Source: (StackOverflow)

ElasticSearch Jest client, how to return document id from hit?

I'm using Jest as ElasticSearch client to search documents:

JestClient client = ...;
Search search = ...;
SearchResult searchResult = client.execute(search);
List<Hit<T, Void>> hits = searchResult.getHits(klass);

Each Hit object looks like:

{"_index":"some_index","_type":"some_type","_id":"some_id","_score":2.609438,"_source":{"foo1":"bar1","foo2":"bar2"}}

While I can only find hit.source method, it seems no hit.id method.

Parse it as JSON object and retrieve key _id's value is a way, but is there any API that can get the document id?


Source: (StackOverflow)

Making index/type mapping raises internal error with JEST

I'm using JEST to access Elasticsearch and so far its working fine. Now I want to manage index/type mappings from my application so I followed an example from JEST web site but I'm getting an error as bellow.

RootObjectMapper.Builder rootObjectMapperBuilder = new RootObjectMapper.Builder("person_mapping").add(
                    new StringFieldMapper.Builder("lastname").store(true));
Builder builder = new DocumentMapper.Builder("indexName", null, rootObjectMapperBuilder);

The error is raised on the last line staring with new DocumentMapper.Builder ... . Its rather something internal but not sure how to fix this.

java.lang.NullPointerException: null
    at org.elasticsearch.Version.indexCreated(Version.java:481) ~[elasticsearch-1.7.2.jar:na]
    at org.elasticsearch.index.mapper.core.NumberFieldMapper.<init>(NumberFieldMapper.java:206) ~[elasticsearch-1.7.2.jar:na]
    at org.elasticsearch.index.mapper.core.IntegerFieldMapper.<init>(IntegerFieldMapper.java:132) ~[elasticsearch-1.7.2.jar:na]
    at org.elasticsearch.index.mapper.internal.SizeFieldMapper.<init>(SizeFieldMapper.java:104) ~[elasticsearch-1.7.2.jar:na]
    at org.elasticsearch.index.mapper.internal.SizeFieldMapper.<init>(SizeFieldMapper.java:99) ~[elasticsearch-1.7.2.jar:na]
    at org.elasticsearch.index.mapper.DocumentMapper$Builder.<init>(DocumentMapper.java:182) ~[elasticsearch-1.7.2.jar:na]

Does anyone have some working example maintaining mappings for Elasticsearch with JEST?

EDIT #1: Integration tests are not helping me :-(

I have looked at JEST integration test focused on mapping here https://github.com/searchbox-io/Jest/blob/master/jest/src/test/java/io/searchbox/indices/PutMappingIntegrationTest.java#L46 and it doesnt help. I dont know where comes client() ... based on others searches it seems its something from native JAVA API and not REST ? Any idea how to use it or where client() comes from?

GetSettingsResponse getSettingsResponse =
                client().admin().indices().getSettings(new GetSettingsRequest().indices(INDEX_NAME)).actionGet();
DocumentMapper documentMapper = new DocumentMapper
                    .Builder(INDEX_NAME, getSettingsResponse.getIndexToSettings().get(INDEX_NAME), rootObjectMapperBuilder).build(null);

SOLVED!

DocumentMapper.Builder requires Settings parameter. Null doesnt work here. Settings can be created manualy like this

Settings indexSettings = ImmutableSettings.settingsBuilder()
                .put("number_of_shards", 1)
                .put("number_of_replicas", 1)
                .put("index.version.created",99999)
                .build();       
Builder builder = new DocumentMapper.Builder("indexName",indexSettings, rootObjectMapperBuilder);

No I can see no null pointer error.


Source: (StackOverflow)

How to fix java.lang.NoClassDefFoundError:: io.searchbox.client.config.idle.ReapableConnectionManager

Hi When i try to configure jest client I got

java.lang.NoClassDefFoundError:: io.searchbox.client.config.idle.ReapableConnectionManager.

I use the mvn dependency of searchbox is . <dependency> <groupId>io.searchbox</groupId> <artifactId>jest</artifactId> <version>0.1.7</version> </dependency> . After seeing the error i found the class

Reapableconnectionmanager,which is already exist there. Is any one guide me to fix this error .

Thanks in advance.


Source: (StackOverflow)

Cannot call method 'toUpperCase' of undefined in simple React/Jest test

Stumped by this..

I'm trying to test that a component is rendered successfully. The test looks like:

jest.dontMock("../opportunities/OpportunityList");

var OpportunityList = require("../opportunities/OpportunityList");
var opportunitiesArray = [];

describe("OpportunityList", function() {

  it("should render", function() {
    var opportunityList = TestUtils.renderIntoDocument(
      <OpportunityList opportunities={opportunitiesArray}
                       bucketValue="fooo"/>

    );
    expect(TestUtils.isCompositeComponent(opportunityList)).toBeTruthy();
  });

});

And the component:

var PipeableOpportunity = require("./PipeableOpportunity");

var OpportunityList = React.createClass({
  getInitialState: function() {
    return {
      componentWidth: null
    };
  },

  componentDidMount: function() {
    this.setState( { componentWidth: React.findDOMNode(this).offsetWidth } );
  },

  render: function() {
    var bucketValue = this.props.bucketValue,
        opportunities = this.props.opportunities,
        componentWidth = this.state.componentWidth,
        pipelineSettings = this.props.pipelineSettings;

    var opportunityList = opportunities.map( function(opportunity) {
      return (
        <PipeableOpportunity key={opportunity.id}
                             opportunity={opportunity}
                             bucketValue={bucketValue}
                             componentWidth={componentWidth}
                             pipelineSettings={pipelineSettings}/>
      );
    });

    return (
      <ol className={bucketValue}>
        {opportunityList}
      </ol>
    );
  }

});

module.exports = OpportunityList;

Running the test produces the following output:

 FAIL  __tests__/OpportunityList-test.js (1.645s)
● OpportunityList › it should render
  - TypeError: Cannot call method 'toUpperCase' of undefined
        at autoGenerateWrapperClass (/home/tmillwardwright/mwri_git/piper/node_modules/react/lib/ReactDefaultInjection.js:53:19)
        at Object.getComponentClassForElement (/home/tmillwardwright/mwri_git/piper/node_modules/react/lib/ReactNativeComponent.js:59:49)
        at ReactCompositeComponentMixin._processProps (/home/tmillwardwright/mwri_git/piper/node_modules/react/lib/ReactCompositeComponent.js:429:44)
        at ReactCompositeComponentMixin.mountComponent (/home/tmillwardwright/mwri_git/piper/node_modules/react/lib/ReactCompositeComponent.js:127:28)
        at wrapper [as mountComponent] (/home/tmillwardwright/mwri_git/piper/node_modules/react/lib/ReactPerf.js:70:21)
        at Object.ReactReconciler.mountComponent (/home/tmillwardwright/mwri_git/piper/node_modules/react/lib/ReactReconciler.js:38:35)
        at ReactDOMComponent.ReactMultiChild.Mixin.mountChildren (/home/tmillwardwright/mwri_git/piper/node_modules/react/lib/ReactMultiChild.js:192:44)
        at ReactDOMComponent.Mixin._createContentMarkup (/home/tmillwardwright/mwri_git/piper/node_modules/react/lib/ReactDOMComponent.js:289:32)
        at ReactDOMComponent.Mixin.mountComponent (/home/tmillwardwright/mwri_git/piper/node_modules/react/lib/ReactDOMComponent.js:199:12)
        at Object.ReactReconciler.mountComponent (/home/tmillwardwright/mwri_git/piper/node_modules/react/lib/ReactReconciler.js:38:35)
        at ReactCompositeComponentMixin.mountComponent (/home/tmillwardwright/mwri_git/piper/node_modules/react/lib/ReactCompositeComponent.js:247:34)
        at wrapper [as mountComponent] (/home/tmillwardwright/mwri_git/piper/node_modules/react/lib/ReactPerf.js:70:21)
        at Object.ReactReconciler.mountComponent (/home/tmillwardwright/mwri_git/piper/node_modules/react/lib/ReactReconciler.js:38:35)
        at ReactCompositeComponentMixin.mountComponent (/home/tmillwardwright/mwri_git/piper/node_modules/react/lib/ReactCompositeComponent.js:247:34)
        at wrapper [as mountComponent] (/home/tmillwardwright/mwri_git/piper/node_modules/react/lib/ReactPerf.js:70:21)
        at Object.ReactReconciler.mountComponent (/home/tmillwardwright/mwri_git/piper/node_modules/react/lib/ReactReconciler.js:38:35)
        at mountComponentIntoNode (/home/tmillwardwright/mwri_git/piper/node_modules/react/lib/ReactMount.js:248:32)
        at ReactReconcileTransaction.Mixin.perform (/home/tmillwardwright/mwri_git/piper/node_modules/react/lib/Transaction.js:134:20)
        at batchedMountComponentIntoNode (/home/tmillwardwright/mwri_git/piper/node_modules/react/lib/ReactMount.js:269:15)
        at ReactDefaultBatchingStrategyTransaction.Mixin.perform (/home/tmillwardwright/mwri_git/piper/node_modules/react/lib/Transaction.js:134:20)
        at Object.ReactDefaultBatchingStrategy.batchedUpdates (/home/tmillwardwright/mwri_git/piper/node_modules/react/lib/ReactDefaultBatchingStrategy.js:66:19)
        at Object.batchedUpdates (/home/tmillwardwright/mwri_git/piper/node_modules/react/lib/ReactUpdates.js:110:20)
        at Object.ReactMount._renderNewRootComponent (/home/tmillwardwright/mwri_git/piper/node_modules/react/lib/ReactMount.js:404:18)
        at Object.wrapper [as _renderNewRootComponent] (/home/tmillwardwright/mwri_git/piper/node_modules/react/lib/ReactPerf.js:70:21)
        at Object.ReactMount.render (/home/tmillwardwright/mwri_git/piper/node_modules/react/lib/ReactMount.js:493:32)
        at Object.wrapper [as render] (/home/tmillwardwright/mwri_git/piper/node_modules/react/lib/ReactPerf.js:70:21)
        at Object.ReactTestUtils.renderIntoDocument (/home/tmillwardwright/mwri_git/piper/node_modules/react/lib/ReactTestUtils.js:52:18)
        at Spec.<anonymous> (/home/tmillwardwright/mwri_git/piper/app/assets/javascripts/components/__tests__/OpportunityList-test.js:14:37)
        at jasmine.Block.execute (/home/tmillwardwright/mwri_git/piper/node_modules/jest-cli/vendor/jasmine/jasmine-1.3.0.js:1065:17)
        at jasmine.Queue.next_ (/home/tmillwardwright/mwri_git/piper/node_modules/jest-cli/vendor/jasmine/jasmine-1.3.0.js:2098:31)
        at null._onTimeout (/home/tmillwardwright/mwri_git/piper/node_modules/jest-cli/vendor/jasmine/jasmine-1.3.0.js:2088:18)
        at Timer.listOnTimeout [as ontimeout] (timers.js:112:15)

I'm running a script before the tests to require React, and it is in the unmockedModulePathPatterns list in my package.json. See below:

React = require("react/addons");
Radium = require("radium");
GlobalStyles = require("../../../GlobalStyles");
TestUtils = React.addons.TestUtils;
routerStub = require("./routerStub");

The component renders fine in a real app.

Using node debug I've ascertained that the test gets as far as running render, but not componentDidMount but beyond that I haven't got any further information, or ideas as to what to try!


Source: (StackOverflow)

Elasticsearch and JEST - how to get NamedAnalyzer?

I'm using a JEST for accessing Elasticsearch in my project and I cant figure out, how to specify analyzer when creating a mapping. I have analyzer defined for my index but how to get NamedAnalyzer object to be used in StringFieldMapper.Builder.indexAnalyzer method?

new StringFieldMapper.Builder("doc_authors").store(true).index(true).tokenized(false).indexAnalyzer(NamedAnalyzer ...)

Source: (StackOverflow)