Skip to content

Commit 63dfb11

Browse files
rendered according to new values
1 parent de83710 commit 63dfb11

File tree

3 files changed

+49
-34
lines changed

3 files changed

+49
-34
lines changed

src/AutoSuggest.js

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import Autosuggest from 'react-autosuggest';
33
import './AutoSuggest.css'
44
import ListIndex from './ListIndex'
55
import axios from 'axios'
6-
import {serverUrl, localUrl} from './url'
6+
import {suggestionsUrl, searchUrl} from './url'
77
import {suggest} from './suggest'
88

99
// Imagine you have a list of languages that you'd like to autosuggest.
@@ -51,6 +51,7 @@ var suggestionJson = {
5151
const getSuggestionOnSearch = value => {
5252
const suggestion = {...suggestionJson}
5353
suggestion.query.match["tags.edgengram"] = value
54+
console.log(suggestion)
5455
return suggestion
5556
}
5657

@@ -66,11 +67,11 @@ const renderData = data => {
6667

6768
const renderDataNew = data => {
6869
var listOfSuggestions = [],
69-
firstResponseValue = ''
70+
firstResponseValue = []
7071
const maxScore = data.hits.max_score
7172
data.hits.hits.map ((value, index) => {
7273
listOfSuggestions.push(`${value._source.tags} (score -- ${Math.round((value._score/maxScore)*100)})`)
73-
if (index === 0) { firstResponseValue = value._source.tags} // for do you mean
74+
if (index === 0 || index === 1 || index === 2) { firstResponseValue.push(value._source.tags)} // for do you mean
7475
})
7576
return [listOfSuggestions, firstResponseValue]
7677
}
@@ -90,7 +91,7 @@ class AutoSuggest extends React.Component {
9091
showResults: false,
9192
searchResult: [],
9293
searchValue: '',
93-
firstResponseValue: ''
94+
firstResponseValue: []
9495
};
9596
}
9697

@@ -104,14 +105,15 @@ class AutoSuggest extends React.Component {
104105
// You already implemented this logic above, so just use it.
105106
onSuggestionsFetchRequested = ({ value }) => {
106107
this.setState({searchValue: value})
107-
var suggestion = getSuggestionOnSearch(value)
108-
// value.length >= 1 ? suggestion = getSuggestionOnSearch(value) : null
108+
var suggestion = null //getSuggestionOnSearch(value)
109+
value.length >= 1 ? suggestion = getSuggestionOnSearch(value) : null
109110
if (suggestion !== null) {
110-
axios.post(serverUrl, suggestionJson)
111+
axios.post(suggestionsUrl, suggestionJson)
111112
.then((response)=>{
112-
// console.log(response.data.suggest)
113-
// var data = renderData(response.data.suggest)
114-
var data = renderDataNew(suggest)
113+
console.log(response.data)
114+
if (response.data.hits.length === 0 ) return
115+
var data = renderDataNew(response.data)
116+
// var data = renderDataNew(suggest)
115117
if (data !== undefined) {
116118
this.setState({
117119
suggestions: data[0],
@@ -134,25 +136,25 @@ class AutoSuggest extends React.Component {
134136
// api call to get final suggestions
135137
var searchQuery = {
136138
"query": {
137-
"multi_match": {
138-
"query": "plastic card",
139-
"fields": ["ParameterValueForSearch.trigram", "NodeName.keywordstring", "NodeTitle.keywordstring"]
140-
}
141-
},
142-
"highlight" : {
143-
"fields" : {
144-
"ParameterValueForSearch" : {},
145-
"NodeName": {},
146-
"NodeTitle": {}
147-
}
148-
}
149-
}
139+
"multi_match": {
140+
"query":"movies",
141+
"fields":["ParameterValueForSearch.ngram","NodeTitle.ngram","NodeName.ngram"]
142+
}
143+
},
144+
"highlight" : {
145+
"fields" : {
146+
"ParameterValueForSearch.ngram" : {},
147+
"NodeTitle.ngram" : {},
148+
"NodeName.ngram": {}
149+
}
150+
}
151+
}
150152
searchQuery.query.multi_match.query = this.state.value
151153
// console.log(searchQuery)
152-
axios.post(serverUrl, searchQuery )
154+
axios.post(searchUrl, searchQuery )
153155
.then((response)=>{
154156
console.log(response.data.hits)
155-
const searchResult = response.data.hits.hits
157+
const searchResult = response.data
156158
this.setState({searchResult, showResults: true})
157159
})
158160
event.preventDefault()

src/ListIndex.js

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import React, {Fragment, Component} from 'react'
22
// import ReactHtmlParser from 'react-html-parser';
33
import axios from 'axios';
4-
import {serverUrl, localUrl} from './url'
4+
import {searchUrl} from './url'
55
import RenderNode from './RenderNode'
66

77

@@ -37,7 +37,7 @@ class ListIndex extends Component {
3737
}
3838
}
3939
descriptionQuery.query.constant_score.filter.term.NodeId = nodeId
40-
axios.post(serverUrl, descriptionQuery)
40+
axios.post(searchUrl, descriptionQuery)
4141
.then((response) => {
4242
console.log(response.data.hits.hits)
4343
const nodeDescription = response.data.hits.hits
@@ -51,22 +51,25 @@ class ListIndex extends Component {
5151
}
5252

5353
render() {
54-
const {searchValue, firstResponseValue} = this.props
55-
console.log(this.props, searchValue, firstResponseValue)
54+
const {searchResult, searchValue, firstResponseValue} = this.props
55+
const maxScore = searchResult.hits.max_score
56+
console.log(searchResult, searchValue, maxScore)
5657
return (
5758
<Fragment>
5859
{
5960
!this.state.showDescription
6061
? <div style={{padding:'2px 10px', margin:'1% 10%'}}>
6162
{
62-
searchValue !== firstResponseValue
63+
searchValue !== firstResponseValue[0]
6364
? <span style={{display: "flex", justifyContent: "center", color: 'red'}}>
64-
Do you mean: <a href="" style={{color: "blue"}}> {firstResponseValue}</a>
65+
Do you mean: <a href="" style={{color: "blue"}}>
66+
{` ${firstResponseValue[0]}, ${firstResponseValue[1]}, ${firstResponseValue[2]}`}
67+
</a>
6568
</span>
6669
: null
6770
}
6871
{
69-
this.props.searchResult.map((value, index) => (
72+
searchResult.hits.hits.map((value, index) => (
7073
<div key={index} style={contentStyle}>
7174
{
7275
// ReactHtmlParser(value._source.ParameterValue)
@@ -83,6 +86,14 @@ class ListIndex extends Component {
8386
<strong style={{color: 'blue'}}>Node Id: </strong>
8487
{value._source.NodeId}
8588
</span>
89+
<span style={{display: 'block'}}>
90+
<strong style={{color: 'blue'}}>Parameter Display Name: </strong>
91+
{value._source.ParameterDisplayName}
92+
</span>
93+
<span style={{display: 'block'}}>
94+
<strong style={{color: 'blue'}}>Score: </strong>
95+
{Math.round((value._score/maxScore)*100)}
96+
</span>
8697
</div>
8798
}
8899
</div>

src/url.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
1-
export const serverUrl = 'http://139.59.40.161:9200/nodes/node_details/_search'
2-
export const localUrl = 'http://localhost:9200/nodes/node_details/_search'
1+
export const serverUrl = 'http://139.59.40.161:9200'
2+
export const localUrl = 'http://localhost:9200'
3+
export const suggestionsUrl = 'http://139.59.40.161:9200/suggest/phrase_suggestions/_search'
4+
export const searchUrl = 'http://139.59.40.161:9200/nodes/node_details/_search'

0 commit comments

Comments
 (0)