2016-09-14 48 views
3

我正在開發iOS和Android應用程序與Xamarin 6.1 Xamarin.Forms NEST和我使用Xamarin.Forms 2.3.1 該應用程序的Guid ID比較掃描QR使用ZXing.Net.Mobile.Forms 2.1.4包含Guid Id並將其保存爲一個字符串到我的ElasticSearch中。要使用ElasticSearch進行連接我正在使用NEST 2.x使用不工作

問題是,當我再次掃描相同的QR(當我確定它已經被索引時),它被檢測爲新值,甚至是值是一樣的(都比較字符串)。但是,我試圖刪除破折號( - )從存儲或比較它們之前,它的工作原理。

這是我的模型:

public class Box 
{ 
    [String(Analyzer = "null")] 
    public string id { get; set; } 
    public string lastUpdate { get; set; } 
} 

result.Text就是我從QR讓我知道肯定是一個字符串,這是我怎麼索引呢:

scannedQR = result.Text; 

// INDEXING 
var timeStamp = GetTimestamp(DateTime.Now); 
var customBox = new Box { 
          id= scannedQR, 
          lastUpdate = timeStamp 
         }; 
var res = client.Index(customBox, p => p 
         .Index("airstorage") 
         .Type("boxes") 
         .Id(scannedQR) 
         .Refresh() 
        ); 

這就是我如何檢查是否QR已存在:

var resSearch = client.Search<Box>(s => s 
            .Index("airstorage") 
            .Type("boxes") 
            .From(0) 
            .Size(10) 
            .Query(q => q 
              .Term(p => p.id, scannedQR) 
             ) 
            ); 

if (resSearch.Documents.Count() > 0) { 
    Console.WriteLine("EXISTING"); 
} 
else { 
    Console.WriteLine("NEW BOX"); 
} 

我還嘗試將該屬性設置爲.NotAnalyse創建時的ElasticSearch索引,如here中所示,但仍然無效。

任何任何想法?一切都歡迎!

謝謝!

回答

3

我會設置你的Box POCO的id場沒有分析

public class Box 
{ 
    [String(Index = FieldIndexOption.NotAnalyzed)] 
    public string id { get; set; } 
    public string lastUpdate { get; set; } 
} 

id屬性將被編入索引,但不會進行分析,所以會被逐字索引。

我也使用

var existsResponse = client.DocumentExists<Box>("document-id", e => e.Index("airstorage").Type("boxes")); 

if (!existsResponse.Exists) 
{ 
    Console.WriteLine("document exists") 
} 

檢查該文件的存在,其實我覺得你想要的是use optimistic concurrency control在指數認購文檔創建即如果文件沒有按然後索引它,但如果它確實存在,則返回一個409衝突無效響應。 OpType.Create可用於此

var indexResponse = client.Index(box, i => i 
    .OpType(OpType.Create) 
    .Index("airstorage") 
    .Type("boxes")); 

if (!indexResponse.IsValid) 
{ 
    if (indexResponse.ApiCall.HttpStatusCode == 409) 
    { 
     Console.WriteLine("document exists"); 
    } 
    else 
    { 
     Console.WriteLine($"error indexing document: {indexResponse.DebugInformation}"); 
    } 
} 
+0

感謝@RussCam!我將POCO添加到'id'聲明和'.OpType(OpType.Create)',現在它工作了! – svicente3

+0

你能幫我一下嗎? ^^'http://stackoverflow.com/questions/39931708/how-to-store-a-c-sharp-list-of-objects-into-elasticsearch-with-nest-2-x/39932064#39932064 – svicente3