Closed tommoholmes closed 9 years ago
Hard to tell what's wrong. Can you post a complete example, including the mapping and the content of the twitter
index?
Maybe it's even enough if you could post the output of this query?
curl -is -XGET 'localhost:9200/twitter/_search?pretty' -d '{"query":{"term":{"title":"Asus"}}}'
when i use curl i get value,, here my code
package main
import (
"github.com/olivere/elastic"
"encoding/json"
"fmt"
"reflect"
)
type Product struct {
Image string `json:"image"`
Desc string `json:"description"`
Title string `json:"title"`
Price string `json:"price"`
Suggest *elastic.SuggestField `json:"suggest_field,omitempty"`
}
func main() {
client, err := elastic.NewClient(
elastic.SetHealthcheck(false),
elastic.SetSniff(false),
elastic.SetURL("http://127.0.0.1:9200"),
)
if err != nil {
panic(err)
}
exists, err := client.IndexExists("t").Do()
if err != nil {
panic(err)
}
if !exists {
createIndex, err := client.CreateIndex("t").Do()
if err != nil {
panic(err)
}
if !createIndex.Acknowledged {
}
}
data := Product{Image: " https://www.static-src.com/wcsstore/Indraprastha/images/catalog/full/bursa-notebook_msi-ge70-20e-243xid-apache-pro-hitam-laptop-8-gb-intel-17-3-inch_full01.jpg", Title: "MSI GE70 20E-243XID Apache Pro Hitam Laptop [8 GB/Intel/17.3 Inch]", Price: "Rp 17,899,000", Desc: "MSI GE70 20E-243XID Apache Pro Hitam Laptop [8 GB/Intel/17.3 Inch], hadir dengan fitur yang dirancang khusus untuk memenuhi kebutuhan Anda saat bermain game. Tampil dengan layar cukup besar berukuran 17.3 Inch, laptop ini dibekali dengan prosesor Intel i5 serta memori grafis nVidia yang akan memanjakan Anda dengan tampilan layar yang jernih. Rasakan pengalaman baru saat bermain game dengan perangkat laptop dari MSI ini."}
inn, err := client.Index().
Index("t").
Type("tweet").
BodyJson(data).
Do()
if err != nil {
panic(err)
}
fmt.Printf("Indexed tweet %s to index %s, type %s\n", inn.Id, inn.Index, inn.Type)
data2 := Product{Image: " https://www.static-src.com/wcsstore/Indraprastha/images/catalog/full/bursa-notebook_msi-ge70-20e-243xid-apache-pro-hitam-laptop-8-gb-intel-17-3-inch_full01.jpg", Title: "MSI GE70 20E-243XID Apache Pro Hitam Laptop [8 GB/Intel/17.3 Inch]", Price: "Rp 17,899,000", Desc: "MSI GE70 20E-243XID Apache Pro Hitam Laptop [8 GB/Intel/17.3 Inch], hadir dengan fitur yang dirancang khusus untuk memenuhi kebutuhan Anda saat bermain game. Tampil dengan layar cukup besar berukuran 17.3 Inch, laptop ini dibekali dengan prosesor Intel i5 serta memori grafis nVidia yang akan memanjakan Anda dengan tampilan layar yang jernih. Rasakan pengalaman baru saat bermain game dengan perangkat laptop dari MSI ini."}
inn1, err := client.Index().
Index("t").
Type("tweet").
BodyJson(data2).
Do()
if err != nil {
panic(err)
}
fmt.Printf("Indexed tweet %s to index %s, type %s\n", inn1.Id, inn1.Index, inn1.Type)
_, err = client.Flush().Index("t").Do()
if err != nil {
panic(err)
}
termQuery := elastic.NewTermQuery("title", "Asus")
searchResult, err := client.Search().
Index("t").
Query(&termQuery).
Sort("title", true).
From(0).Size(10).
Pretty(true).
Do()
if err != nil {
panic(err)
}
fmt.Printf("Query took %d milliseconds\n", searchResult.TookInMillis)
var ttyp Product
for _, item := range searchResult.Each(reflect.TypeOf(ttyp)) {
if t, ok := item.(Product); ok {
fmt.Printf("Tweet by %s: %s\n", t.Price, t.Title)
}
}
fmt.Printf("a Found a total of %d tweets\n", searchResult.TotalHits())
if searchResult.Hits != nil {
fmt.Printf("a Found a total of %d tweets\n", searchResult.Hits.TotalHits)
for _, hit := range searchResult.Hits.Hits {
var t Product
err := json.Unmarshal(*hit.Source, &t)
if err != nil {
}
fmt.Printf("Tweet by %s: %s\n", t.Price, t.Title)
}
} else {
fmt.Print("Found no tweets\n")
}
}
Hmm... if you use a TermQuery
is should be looking for the exact value matches in the given field. In other words, it should only return results when there is a product with "title":"Asus"
(see TermQuery). It seems you are looking for a full-text query, e.g. MatchQuery.
Or am I missing something?
hey olivere i get trouble
in my browser i can look my value, but when i use curl -is -xPOST 'localhost:9200/t/_search?pretty' -d '{"query":{"term":{"title":"Asus"}}}'
i cannot see my value
Hi there... it's -XGET
not -XPOST
...
sorry,, I use curl -is -XGET 'localhost:9200/t/_search?pretty' -d '{"query":{"term":{"title":"Asus"}}}'
And what's the output?
HTTP/1.1 200 OK Content-Type: application/json; charset=UTF-8 Content-Length: 193
{ "took" : 2, "timed_out" : false, "_shards" : { "total" : 5, "successful" : 5, "failed" : 0 }, "hits" : { "total" : 0, "max_score" : null, "hits" : [ ] } }
Okay. That looks good to me so Elasticsearch and Elastic successfully returned zero results to you ;-)
Don't get me wrong, but are you sure you understand your query? I believe you want full-text search but what you did was an exact-match query. The Definite Guide has an in-depth view into structured-queries vs. full-text queries. It's really well-written and worth the read.
how to use full-text search with Elastic olivere
There's plenty of examples in the tests, e.g. here for MatchAllQuery.
Elastic implements e.g. all the different match queries. The tests are your friend, e.g. here's how to set up all the different match queries with Elastic.
The best approach is probably to first understand the Definite Guide, then look up the documentation for the queries you think you'll need, and finally looking into the Elastic tests to find out how to do it in Go.
HTH
okee thanks, but when i run all := NewMatchAllQuery() searchResult, err := client.Search().Index("witch").Query(&all).Do()
undefined: NewMatchAllQuery
If you're a consumer of Elastic, you should have imported it with import "github.com/olivere/elastic"
so you should use q := elastic.NewMatchAllQuery()
etc.
I have imported import "github.com/olivere/elastic"
Please re-open if you still have problems.
its my code:
and here my Struct
when i run with
Query took 2 milliseconds a Found a total of 0 tweets a Found a total of 0 tweets