Full text search not correct in sort


(Quanghaisoft) #1
{ 
  search(func: has(product_category), first:10 )  
  @filter( 
      anyoftext(product_productname,"iphone 4")  
  )
  {  
    product_productname
  }  
}

and the result is

 {
        "product_productname": "Siemens Xelibri 4",
        "uid": "0x6268d"
      },
      {
        "product_productname": "iPhone 2G",
        "uid": "0x62ac8"
      },
      {
        "product_productname": "iPhone 3G",
        "uid": "0x62dde"
      },
      {
        "product_productname": "iPhone 3GS 8GB",
        "uid": "0x62ff9"
      },
      {
        "product_productname": "iPhone 4 16GB",
        "uid": "0x6336f"
      },
      {
        "product_productname": "Samsung B7350 Omnia PRO 4",
        "uid": "0x633d0"
      },
      {
        "product_productname": "Tai nghe Iphone TGDD",
        "uid": "0x6345d"
      },
      {
        "product_productname": "Cáp Iphone TGDD",
        "uid": "0x63482"
      },
      {
        "product_productname": "iPod Shuffle gen 4 2G",
        "uid": "0x6407b"
      }

the text “Siemens Xelibri 4”, is at 1 position,
text " iPhone 4 16GB" is at 5 position

Why is it??? and how to fix it, thanks you


(Daniel Mai) #2

The default ordering is by UID. In the query response you shared, you’ll notice that the results are ordered by UID in ascending order (0x6268d, 0x62ac8, and so on).

To sort the results by the values of a predicate, you can use orderasc or orderdesc (reference: sorting docs).

{ 
  search(func: anyoftext(product_productname,"iphone 4"), orderasc: product_productname, first: 10)
     @filter(has(product_category)) {  
    product_productname
  }  
}

As a side note, you can expect quicker queries when using an indexed search function for the root func. i.e., you can swap has() with anyoftext(). The latter will utilize the full-text index.


(Quanghaisoft) #3

Order by name, or any predicate is not my solution, because we want result with best score at first, like elaticsearch, we use dgraph replace elasticsearch in my project.So we want a score field for sort it , it dgraph has it , i very thanks you and wating in new version.


(Quanghaisoft) #4

thanks you very much


(Quanghaisoft) #5

Very bad if i can not order by score, the result search too bad if have to order by uid.Any one can help me ???
All my work have to stop if i can not resolve this proplem .


(Michel Conrado) #6

Yes, you can.

1- Do a var block to gather and compute the values. And set the result to a var
2 - Do the actual query. eg:

  q(func: uid(G), orderdesc: val(Score)) {
    uid
    Score : val(Score)
    OtherPred
  }