2016-09-14 25 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